Update gruntmasterd to use postgres
authorMarius Gavrilescu <marius@ieval.ro>
Mon, 24 Mar 2014 19:06:34 +0000 (21:06 +0200)
committerMarius Gavrilescu <marius@ieval.ro>
Mon, 24 Mar 2014 19:06:34 +0000 (21:06 +0200)
lib/Gruntmaster/Daemon.pm
lib/Gruntmaster/Daemon/Generator/File.pm

index f2c4a11..4712ed0 100644 (file)
@@ -12,6 +12,7 @@ use Gruntmaster::Data;
 
 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;
@@ -22,6 +23,7 @@ use constant PAGE_SIZE => 10;
 
 ##################################################
 
+my $db = Gruntmaster::Data->connect('dbi:Pg:');
 my $ua = LWP::UserAgent->new;
 my @purge_hosts = exists $ENV{PURGE_HOSTS} ? split ' ', $ENV{PURGE_HOSTS} : ();
 
@@ -49,31 +51,31 @@ sub purge {
 }
 
 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;
@@ -119,39 +121,35 @@ sub process{
        };
 
        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");
        }
 }
 
@@ -159,8 +157,11 @@ sub run{
        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;
index acadf58..877eb43 100644 (file)
@@ -18,8 +18,7 @@ sub generate{
        if (exists $meta->{infile}) {
                write_file 'input', $meta->{infile}[$test - 1]
        } else {
-               my $ct = defined $Gruntmaster::Data::contest ? "ct/$Gruntmaster::Data::contest" : '';
-               copy "/var/lib/gruntmasterd/$ct/pb/$meta->{problem}/$test.in", 'input'
+               copy "/var/lib/gruntmasterd/pb/$meta->{problem}/$test.in", 'input'
        }
 }
 
This page took 0.040616 seconds and 4 git commands to generate.