[Bio] / MGRASTBackend / mg_compute_figfam_sims.pl Repository:
ViewVC logotype

View of /MGRASTBackend/mg_compute_figfam_sims.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.3 - (download) (as text) (annotate)
Thu Jul 23 22:19:52 2009 UTC (10 years, 3 months ago) by arodri7
Branch: MAIN
CVS Tags: HEAD
Changes since 1.2: +1 -1 lines
fixed errors for sge

#
# Compute one piece of sims work.
#
# Usage: rp_compute_sims sims_job_dir
#
# SGE_TASK_ID is set to the taskid to be computed.
#

use GenomeMeta;
use FIG_Config;
use strict;
use File::Basename;
use FortyEightMeta::SimStatusDB;
use JobStage;
use SGE;

my $STAGE = 'compute_figfam_sims';

@ARGV == 2 or die "Usage: $0 job-dir sims-job-dir\n";

my $hostname = `hostname`;
chomp $hostname;

my $jobdir = shift;
my $sims_jobdir = shift;

-d $jobdir or die "$0: job dir $jobdir does not exist\n";
-d $sims_jobdir or die "$0: job dir $jobdir does not exist\n";

my $task_num = $ENV{SGE_TASK_ID};

my $sims_base = basename($sims_jobdir);

$task_num =~ /^\d+$/ or die "$0: SGE_TASK_ID not numeric\n";

my $stage = new JobStage('Job48', $STAGE, $jobdir);

my $meta = $stage->meta;
my $job_id = basename($jobdir);

my $compute_exe = "$FIG_Config::bin/mg_compute_figfam_sims";
-x $compute_exe or $stage->fatal("Executable missing: $compute_exe");

if ($task_num == 1 and $meta->get_metadata("status.sims") eq 'queued')
{
    $meta->set_metadata("status.sims", "in_progress");
}

open(TL, "<$sims_jobdir/task.list") or die "$0: cannot open tasklist $sims_jobdir/task.list: $!\n";

my ($fasta, $in, $fam_data, $flags, $out, $err);

while (<TL>)
{
    chomp;
    my @a = split(/\t/);
    if ($a[0] == $task_num)
    {
	($in, $flags, $fam_data, $fasta, $out, $err) = @a[1 .. 6];
	next if (!$err);
	last;
    }
}
close(TL);


$in or die "Could not find task"; # $task_num";

$meta->add_log_entry($0, ['running ', $task_num, $in, $fam_data, $fasta, $flags, $out, $err]);
print "Computing on $in\n";
my $t1 = time;

#my $blast_args = "$flags -i $in -d $nr -o $out";

my $status_db = FortyEightMeta::SimStatusDB->new($job_id);

my $rec = {};
$rec->{sim_compute_host} = $hostname;
$rec->{start_time} = $t1;
$rec->{status} = 'in_progress';

$status_db->set_task($sims_base, $task_num, $rec);
$rec = $status_db->get_task($sims_base, $task_num);

my $retries_left = $rec->{blast_retries_left};

#$status_db->disconnect();

open(E, ">$err") or die "Cannot open $err: $!";
open(P, "$FIG_Config::bin/compute_ff_sims $jobdir $fasta $in $out $fam_data 2>&1 |") or die "Cannot run blastall: $!";

while (<P>)
{
    print;
    print E $_;
}

my $rc = close(P);
my $bang = $!;
my $ques = $?;

my $t2 = time;
my $elap = $t2 - $t1;

my $min = int($elap / 60);
my $sec = $elap % 60;

printf E "%d:%02d $t1 $t2 $elap rc=$rc \$!=$bang \$?=$ques\n", $min, $sec;
printf "%d:%02d $t1 $t2 $elap rc=$rc \$!=$bang \$?=$ques\n", $min, $sec;

$status_db->connect();

#=head
my $rec = {};

$rec->{end_time} = $t2;
$rec->{elap_time} = $elap;

if (!$rc)
{
    print "Close err: \$!=$bang \$?=$ques\n";
    my $err;
    if ($bang)
    {
	$err = $bang;
	#$meta->add_log_entry($0, ['blastall close error', $!]);
	print "Error closing blastall: $err\n";
	print E "Error closing blastall: $err\n";
    }
    else
    {
	$err = $ques;
	#$meta->add_log_entry($0, ['blastall nonzero exit', $err]);
	print "Nonzero exit status $err from blastall\n";
	print E "Nonzero exit status $err from blastall\n";
    }

    #
    # Error retry handling.
    # Determine if we have any retries left. If so, save the run information
    # in $rec->{retry}->[num], decrement the retry count, reset the
    # state of the $rec, and resubmit the task.
    #

    if ($retries_left > 0)
    {
	my $sge = new SGE;
	
	$status_db->store_record_as_retry($sims_base, $task_num);

	my @sge_args;
	my $jobname = "m${retries_left}$rec->{abbr}_$job_id";
	
	push(@sge_args, "-N $jobname");
	push(@sge_args, "-v PATH");
	push(@sge_args, "-e $jobdir/sge_output");
	push(@sge_args, "-o $jobdir/sge_output");
	push(@sge_args, "-t $task_num");
	push(@sge_args, "-b yes");
	#push(@sge_args, "-l low");

	my $sge_args = join(" ", @sge_args);
	
	my $sge_id;
	
	eval {
	    $sge_id = $sge->submit_job($meta, $sge_args, "$compute_exe $jobdir $sims_jobdir");
	    #    print "Would submit '$sge_args' '$compute_exe' '$jobdir' '$path'\n";
	};
	
	if ($@)
	{
	    $stage->log($meta, "error resubmitting SGE job $compute_exe $jobdir: $@\n");
	    $rec->{success} = 0;
	    $rec->{exit_code} = $err;
	    $rec->{status} = 'error';
	}
	else
	{
	    $rec->{status} = 'not_started';
	    $rec->{sim_sge_id} = $sge_id;
	    my $mlist = $meta->get_metadata("sims.sge_ids");
	    if ($mlist)
	    {
		if (ref($mlist) eq 'ARRAY')
		{
		    push(@$mlist, $sge_id);
		}
	    }
	    else
	    {
		$mlist = [$sge_id];
	    }
	    $meta->set_metadata("sims.sge_ids");
	    #
	    # Yes, it's a race. Not too worried.
	    #
	    
	}
    }
    else
    {
	$rec->{success} = 0;
	$rec->{exit_code} = $err;
	$rec->{status} = 'error';
    }
}
else
{
    $rec->{success} = 1;
    $rec->{exit_code} = $?;
    $rec->{status} = 'complete';
    
#    $meta->add_log_entry($0, ['blastall success', $elap]);
    print E "SUCCESS\n";
}
close(E);

#=cut
$status_db->set_task($sims_base, $task_num, $rec);
undef $status_db;


MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3