[Bio] / FigKernelScripts / run_peg_job_worker.pl Repository:
ViewVC logotype

View of /FigKernelScripts/run_peg_job_worker.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1 - (download) (as text) (annotate)
Thu Feb 3 17:22:47 2005 UTC (15 years, 3 months ago) by olson
Branch: MAIN
First cut at a database-based job scheduler.

#
# Worker process.
#
# Pulls pegs from the table, works on them with the given script, writes results back.
#
# usage: run_peg_job_worker tablename
#

use FIG;
use strict;

my($table) = @ARGV;

if ($table eq "")
{
    die "Usage: $0 tablename\n";
}

$table = "pjs_$table";

my %status_values = (AVAIL => 0,
		     TAKEN => 1,
		     DONE => 2
		     );


my $fig = new FIG;
my $fig_dbh = $fig->db_handle;
my $dbh = $fig_dbh->{_dbh};
my $dbms = $fig_dbh->{_dbms};

my $lock_mode;

if ($dbms eq "mysql")
{
    $lock_mode = "for update";
#    $lock_mode = "lock in share mode";
}

my $what = 0;
my $worker = `hostname`;
chomp($worker);
$worker .= ":$$";

while (1)
{
    my $work = get_work($worker);
    my $output = "finished $work $what\n";
    $what++;
    work_done($work, $output);
}

sub get_work
{
    my($worker) = @_;
    my $work;
    local $dbh->{AutoCommit} = 0;
    local $dbh->{RaiseError} = 1;

    eval {
	my $res = $dbh->selectall_arrayref("select * from $table where status = ? limit 1 $lock_mode", undef,
					   $status_values{AVAIL});
	if (not $res or @$res == 0)
	{
	    die "No work found\n";
	}

	my ($peg, $status, $job_taken, $job_finished, $output) = @{$res->[0]};
	print "Found peg=$peg status=$status job info $job_taken $job_finished\n";
	$dbh->do("update $table set status = ?, worker = ?, job_taken = now() where peg = ?", undef,
		 $status_values{TAKEN}, $worker, $peg);

	$dbh->commit();
	$work = $peg;
    };

    if ($@)
    {
	warn "Error in get_work eval: $@\n";
	$dbh->rollback();
	return;
    }
    else
    {
	return $work;
    }
}

sub work_done
{
    my($work, $output) = @_;

    local $dbh->{AutoCommit} = 0;
    local $dbh->{RaiseError} = 1;

    eval {
	$dbh->do("update $table set status = ?, job_finished = now(), output = ? where peg = ?", undef,
		 $status_values{DONE}, $output, $work);

	$dbh->commit();
    };

    if ($@)
    {
	warn "Error in work_done eval: $@\n";
	$dbh->rollback();
	return;
    }
    else
    {
	return 1;
    }
    
}

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3