hydra/src/runner.pl

95 lines
2.4 KiB
Perl
Raw Normal View History

2008-11-10 13:33:12 +00:00
#! @perl@ -w
use strict;
2008-11-11 14:45:33 +00:00
use Cwd;
use POSIX qw(dup2);
2008-11-10 13:33:12 +00:00
use HydraFrontend::Schema;
my $db = HydraFrontend::Schema->connect("dbi:SQLite:dbname=hydra.sqlite", "", "", {});
$db->storage->dbh->do("PRAGMA synchronous = OFF;");
2008-11-10 13:33:12 +00:00
# Unlock jobs whose building process has died.
$db->txn_do(sub {
2008-11-11 12:54:37 +00:00
my @jobs = $db->resultset('Builds')->search(
{finished => 0, busy => 1}, {join => 'schedulingInfo'});
2008-11-10 13:33:12 +00:00
foreach my $job (@jobs) {
2008-11-11 12:54:37 +00:00
my $pid = $job->schedulingInfo->locker;
2008-11-10 13:33:12 +00:00
if (kill(0, $pid) != 1) { # see if we can signal the process
print "job ", $job->id, " pid $pid died, unlocking\n";
2008-11-11 12:54:37 +00:00
$job->schedulingInfo->busy(0);
$job->schedulingInfo->locker("");
$job->schedulingInfo->update;
2008-11-10 13:33:12 +00:00
}
}
});
sub checkJobs {
2008-11-10 13:33:12 +00:00
print "looking for runnable jobs...\n";
my $job;
2008-11-11 14:45:33 +00:00
my $logfile;
2008-11-10 13:33:12 +00:00
$db->txn_do(sub {
2008-11-11 12:54:37 +00:00
my @jobs = $db->resultset('Builds')->search(
{finished => 0, busy => 0},
{join => 'schedulingInfo', order_by => ["priority", "timestamp"]});
2008-11-10 13:33:12 +00:00
print "# of available jobs: ", scalar(@jobs), "\n";
if (scalar @jobs > 0) {
$job = $jobs[0];
2008-11-11 14:45:33 +00:00
$logfile = getcwd . "/logs/" . $job->id;
unlink $logfile;
2008-11-11 12:54:37 +00:00
$job->schedulingInfo->busy(1);
$job->schedulingInfo->locker($$);
2008-11-11 14:45:33 +00:00
$job->schedulingInfo->logfile($logfile);
2008-11-11 12:54:37 +00:00
$job->schedulingInfo->update;
2008-11-10 13:33:12 +00:00
}
});
# Start the job. We need to do this outside the transaction in
# case it aborts or something.
if (defined $job) {
my $id = $job->id;
print "starting job $id\n";
2008-11-10 13:33:12 +00:00
eval {
my $child = fork();
die unless defined $child;
if ($child == 0) {
2008-11-11 14:45:33 +00:00
open LOG, ">$logfile" or die;
POSIX::dup2(fileno(LOG), 1) or die;
POSIX::dup2(fileno(LOG), 2) or die;
exec("perl", "-IHydraFrontend/lib", "-w",
"./build.pl", $id);
warn "cannot start job " . $id;
_exit(1);
}
2008-11-10 13:33:12 +00:00
};
if ($@) {
warn $@;
$db->txn_do(sub {
2008-11-11 12:54:37 +00:00
$job->schedulingInfo->busy(0);
$job->schedulingInfo->locker($$);
$job->schedulingInfo->update;
2008-11-10 13:33:12 +00:00
});
}
}
}
while (1) {
eval {
checkJobs;
};
warn $@ if $@;
2008-11-10 13:33:12 +00:00
print "sleeping...\n";
2008-11-12 11:09:21 +00:00
sleep(5);
2008-11-10 13:33:12 +00:00
}