use File::Basename qw/fileparse/;
use File::Temp qw/tempdir/;
+use JSON qw/decode_json encode_json/;
use Sys::Hostname qw/hostname/;
use Time::HiRes qw/time/;
use Try::Tiny;
##################################################
+my $db = Gruntmaster::Data->connect('dbi:Pg:');
my $ua = LWP::UserAgent->new;
my @purge_hosts = exists $ENV{PURGE_HOSTS} ? split ' ', $ENV{PURGE_HOSTS} : ();
}
sub process{
- my $job = shift;
+ my ($job, $jobr) = @_;
my @results;
my @full_results = ();
- my $meta = {};
+ my $meta;
our $errors = '';
try {
- $meta = job_inmeta $job;
- if (job_problem $job) {
- local $_ = job_problem $job;
- my $pbmeta = problem_meta;
- my %files = exists $meta->{files} ? %{$meta->{files}} : ();
- $meta = {
- %$meta,
- problem => $_,
- (defined problem_generator() ? (generator => problem_generator) : ()),
- (defined problem_runner() ? (runner => problem_runner) : ()),
- (defined problem_judge() ? (judge => problem_judge) : ()),
- (defined problem_testcnt() ? (testcnt => problem_testcnt) : ()),
- (defined problem_timeout() ? (timeout => problem_timeout) : ()),
- (defined problem_olimit() ? (olimit => problem_olimit) : ()),
- %$pbmeta
- };
- $meta->{files} = {%files, %{$pbmeta->{files}}} if exists $pbmeta->{files};
- }
+ $meta = {
+ problem => $jobr->problem->id,
+ files => {
+ prog => {
+ name => 'prog.' . $jobr->extension,
+ format => $jobr->format,
+ content => $jobr->source,
+ },
+ },
+ map { $_ => $jobr->problem->get_column($_) } qw/generator runner judge testcnt timeout olimit/
+ };
+ $meta->{tests} = decode_json $jobr->problem->tests if $meta->{runner} eq 'File';
+
+ $meta->{ver} = {
+ name => 'ver.cpp',
+ format => $jobr->problem->verformat,
+ content => $jobr->problem->versource,
+ } if $jobr->problem->verformat;
prepare_files $meta;
chomp $errors;
};
get_logger->info("Job result: " . $meta->{result_text});
- set_job_result $job, $meta->{result};
- set_job_result_text $job, $meta->{result_text};
- set_job_results $job, \@full_results if scalar @full_results;
- set_job_errors $job, $errors;
-
- my $log = $Gruntmaster::Data::contest ? "ct/$Gruntmaster::Data::contest/log" : 'log';
-
- PUBLISH gensrc => ($Gruntmaster::Data::contest // '') . ".$job";
- PUBLISH genpage => "$log/job/$job.html";
- PUBLISH genpage => "$log/index.html";
- PUBLISH genpage => "$log/st.html";
+ $jobr->update({
+ result => $meta->{result},
+ result_text => $meta->{result_text},
+ results => encode_json \@full_results,
+ $errors ? (errors => $errors) : ()
+ });
+
+ my $log = $jobr->contest ? 'ct/' . $jobr->contest . '/log' : 'log';
my $page = ($job + PAGE_SIZE - 1) / PAGE_SIZE;
- PUBLISH genpage => "$log/@{[$page - 1]}.html";
- PUBLISH genpage => "$log/$page.html";
- PUBLISH genpage => "$log/@{[$page + 1]}.html";
- purge "/$log/job/$job";
+ purge "/$log/$job";
purge "/$log/";
purge "/$log/st";
- purge "/$log/$_" for $page - 1, $page, $page + 1;
+ purge "/$log/page/$_" for $page - 1, $page, $page + 1;
}
sub got_job{
- $_[0] =~ /^(\w*)\.(\d+)$/;
- my $job = $2;
- local $Gruntmaster::Data::contest = $1 if $1;
- get_logger->debug("Taking job $job@{[defined $1 ? \" of contest $1\" : '']}...");
- if (set_job_daemon $job, hostname . ":$$") {
- get_logger->debug("Succesfully taken job $job");
- process $job;
- get_logger->debug("Job $job done");
+ my $job = $_[0];
+ my $id = $job->id;
+ get_logger->debug("Taking job $id...");
+ my $daemon = hostname . ":$$";
+ $job->update({daemon => $daemon});
+ #if (set_job_daemon $job, hostname . ":$$") {
+ if (1) {
+ get_logger->debug("Succesfully taken job $id");
+ process $id, $job;
+ get_logger->debug("Job $id done");
} else {
- get_logger->debug("Job $job already taken");
+ get_logger->debug("Job $id already taken");
}
}
Log::Log4perl->init('/etc/gruntmasterd/gruntmasterd-log.conf');
get_logger->info("gruntmasterd $VERSION started");
chdir tempdir 'gruntmasterd.XXXX', CLEANUP => 1, TMPDIR => 1;
- SUBSCRIBE jobs => \&got_job;
- WAIT_FOR_MESSAGES 86400 while 1
+ while (1) {
+ my $job = $db->jobs->search({daemon => undef}, {rows => 1})->first;
+ got_job $job if defined $job;
+ sleep 2 unless defined $job;
+ }
}
1;