2008-11-10 13:33:12 +00:00
|
|
|
#! @perl@ -w
|
|
|
|
|
|
|
|
use strict;
|
2008-11-11 14:45:33 +00:00
|
|
|
use Cwd;
|
2008-11-11 10:27:36 +00:00
|
|
|
use POSIX qw(dup2);
|
2008-11-25 11:09:15 +00:00
|
|
|
use Hydra::Schema;
|
2008-11-10 13:33:12 +00:00
|
|
|
|
|
|
|
|
2008-11-25 11:09:15 +00:00
|
|
|
my $db = Hydra::Schema->connect("dbi:SQLite:dbname=hydra.sqlite", "", "", {});
|
2008-11-10 13:33:12 +00:00
|
|
|
|
2008-11-11 10:27:36 +00:00
|
|
|
$db->storage->dbh->do("PRAGMA synchronous = OFF;");
|
|
|
|
|
2008-11-10 13:33:12 +00:00
|
|
|
|
2008-11-28 11:00:55 +00:00
|
|
|
sub unlockDeadJobs {
|
|
|
|
# Unlock jobs whose building process has died.
|
|
|
|
$db->txn_do(sub {
|
|
|
|
my @jobs = $db->resultset('Builds')->search(
|
|
|
|
{finished => 0, busy => 1}, {join => 'schedulingInfo'});
|
|
|
|
foreach my $job (@jobs) {
|
|
|
|
my $pid = $job->schedulingInfo->locker;
|
|
|
|
if (kill(0, $pid) != 1) { # see if we can signal the process
|
|
|
|
print "job ", $job->id, " pid $pid died, unlocking\n";
|
|
|
|
$job->schedulingInfo->busy(0);
|
|
|
|
$job->schedulingInfo->locker("");
|
|
|
|
$job->schedulingInfo->update;
|
|
|
|
}
|
2008-11-10 13:33:12 +00:00
|
|
|
}
|
2008-11-28 11:00:55 +00:00
|
|
|
});
|
|
|
|
}
|
2008-11-10 13:33:12 +00:00
|
|
|
|
|
|
|
|
2008-11-11 10:27:36 +00:00
|
|
|
sub checkJobs {
|
2008-11-10 13:33:12 +00:00
|
|
|
print "looking for runnable jobs...\n";
|
|
|
|
|
2008-11-26 14:20:50 +00:00
|
|
|
my @jobsStarted;
|
2008-11-10 13:33:12 +00:00
|
|
|
|
|
|
|
$db->txn_do(sub {
|
|
|
|
|
2008-11-26 14:20:50 +00:00
|
|
|
# Get the system types for the runnable builds.
|
|
|
|
my @systemTypes = $db->resultset('Builds')->search(
|
|
|
|
{finished => 0, busy => 0},
|
|
|
|
{join => 'schedulingInfo', select => [{distinct => 'system'}], as => ['system']});
|
|
|
|
|
|
|
|
# For each system type, select up to the maximum number of
|
|
|
|
# concurrent build for that system type. Choose the highest
|
|
|
|
# priority builds first, then the oldest builds.
|
|
|
|
foreach my $system (@systemTypes) {
|
|
|
|
# How many builds are already currently executing for this
|
|
|
|
# system type?
|
|
|
|
my $nrActive = $db->resultset('Builds')->search(
|
|
|
|
{finished => 0, busy => 1, system => $system->system},
|
|
|
|
{join => 'schedulingInfo'})->count;
|
|
|
|
|
|
|
|
# How many extra builds can we start?
|
2008-11-26 17:14:27 +00:00
|
|
|
(my $systemTypeInfo) = $db->resultset('Systemtypes')->search({system => $system->system});
|
|
|
|
my $maxConcurrent = defined $systemTypeInfo ? $systemTypeInfo->maxconcurrent : 2;
|
|
|
|
my $extraAllowed = $maxConcurrent - $nrActive;
|
2008-11-26 14:20:50 +00:00
|
|
|
$extraAllowed = 0 if $extraAllowed < 0;
|
|
|
|
|
|
|
|
# Select the highest-priority builds to start.
|
|
|
|
my @jobs = $extraAllowed == 0 ? () : $db->resultset('Builds')->search(
|
|
|
|
{ finished => 0, busy => 0, system => $system->system },
|
|
|
|
{ join => 'schedulingInfo', order_by => ["priority DESC", "timestamp"],
|
|
|
|
rows => $extraAllowed });
|
|
|
|
|
|
|
|
print "system type `", $system->system,
|
2008-11-26 17:14:27 +00:00
|
|
|
"': $nrActive active, $maxConcurrent allowed, ",
|
2008-11-26 14:20:50 +00:00
|
|
|
"starting ", scalar(@jobs), " builds\n";
|
|
|
|
|
|
|
|
foreach my $job (@jobs) {
|
2008-11-27 19:06:11 +00:00
|
|
|
my $logfile = getcwd . "/logs/" . $job->id;
|
|
|
|
unlink($logfile);
|
2008-11-26 14:20:50 +00:00
|
|
|
$job->schedulingInfo->busy(1);
|
|
|
|
$job->schedulingInfo->locker($$);
|
|
|
|
$job->schedulingInfo->logfile($logfile);
|
2008-11-27 02:29:46 +00:00
|
|
|
$job->schedulingInfo->starttime(time);
|
2008-11-26 14:20:50 +00:00
|
|
|
$job->schedulingInfo->update;
|
|
|
|
$job->buildsteps->delete_all;
|
|
|
|
push @jobsStarted, $job;
|
|
|
|
}
|
2008-11-10 13:33:12 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2008-11-26 14:20:50 +00:00
|
|
|
# Actually start the builds we just selected. We need to do this
|
|
|
|
# outside the transaction in case it aborts or something.
|
|
|
|
foreach my $job (@jobsStarted) {
|
2008-11-11 10:27:36 +00:00
|
|
|
my $id = $job->id;
|
2008-11-26 17:14:27 +00:00
|
|
|
print "starting job $id (", $job->project->name, ":", $job->attrname, ") on ", $job->system, "\n";
|
2008-11-10 13:33:12 +00:00
|
|
|
eval {
|
2008-11-27 19:06:11 +00:00
|
|
|
my $logfile = $job->schedulingInfo->logfile;
|
2008-11-11 10:27:36 +00:00
|
|
|
my $child = fork();
|
|
|
|
die unless defined $child;
|
|
|
|
if ($child == 0) {
|
2008-11-11 14:45:33 +00:00
|
|
|
open LOG, ">$logfile" or die;
|
2008-11-11 10:27:36 +00:00
|
|
|
POSIX::dup2(fileno(LOG), 1) or die;
|
|
|
|
POSIX::dup2(fileno(LOG), 2) or die;
|
2008-11-25 11:09:15 +00:00
|
|
|
exec("perl", "-IHydra/lib", "-w",
|
|
|
|
"./Hydra/programs/Build.pl", $id);
|
2008-11-11 10:27:36 +00:00
|
|
|
warn "cannot start job " . $id;
|
2008-11-26 14:20:50 +00:00
|
|
|
POSIX::_exit(1);
|
2008-11-11 10:27:36 +00:00
|
|
|
}
|
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
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2008-11-11 10:27:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
eval {
|
2008-11-28 11:00:55 +00:00
|
|
|
unlockDeadJobs;
|
2008-11-11 10:27:36 +00:00
|
|
|
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
|
|
|
}
|