Make sure work directory is world-searchable
[gruntmaster-daemon.git] / lib / Gruntmaster / Daemon.pm
index f2c4a11..1e5d8b2 100644 (file)
@@ -4,32 +4,41 @@ use 5.014000;
 use strict;
 use warnings;
 
-our $VERSION = '5999.000_002';
+our $VERSION = '5999.000_004';
 
 use Gruntmaster::Daemon::Constants qw/ERR/;
-use Gruntmaster::Daemon::Format qw/prepare_files/;
-use Gruntmaster::Data;
+use Gruntmaster::Daemon::Format qw/prepare_files stopvms/;
 
 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;
 use Log::Log4perl qw/get_logger/;
-use LWP::UserAgent;
 
 use constant PAGE_SIZE => 10;
+use constant FORMAT_EXTENSION => {
+       C => 'c',
+       CPP => 'cpp',
+       GCCGO => 'go',
+       GOLANG => 'go',
+       HASKELL => 'hs',
+       MONO => 'cs',
+       JAVA => 'java',
+       PASCAL => 'pas',
+       PERL => 'pl',
+       PYTHON => 'py',
+};
 
 ##################################################
 
-my $ua = LWP::UserAgent->new;
-my @purge_hosts = exists $ENV{PURGE_HOSTS} ? split ' ', $ENV{PURGE_HOSTS} : ();
+my $db;
 
 sub safe_can_nodie {
        my ($type, $sub, $name) = @_;
 
-       return unless $name =~ /^\w+$/;
-       no strict 'refs';
+       return unless $name =~ /^\w+$/s;
        my $pkg = __PACKAGE__ . "::${type}::${name}";
        eval "require $pkg" or get_logger->warn("Error while requiring $pkg: $@");
        $pkg->can($sub);
@@ -41,44 +50,41 @@ sub safe_can {
        safe_can_nodie @_ or get_logger->logdie("No such \l$type: '$name'");
 }
 
-sub purge {
-       for my $host (@purge_hosts) {
-               my $req = HTTP::Request->new(PURGE => "http://$host$_[0]");
-               $ua->request($req)
-       }
-}
-
 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}} : ();
+               if (ref $job) {
+                       $meta = $job;
+               } else {
                        $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
+                               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->{files} = {%files, %{$pbmeta->{files}}} if exists $pbmeta->{files};
+                       $meta->{tests} = decode_json $jobr->problem->tests if $meta->{runner} eq 'File';
+
+                       $meta->{files}{ver} = {
+                               name => 'ver.' . FORMAT_EXTENSION->{$jobr->problem->verformat},
+                               format => $jobr->problem->verformat,
+                               content => $jobr->problem->versource,
+                       } if $jobr->problem->verformat;
                }
 
                prepare_files $meta;
                chomp $errors;
 
-               my ($files, $generator, $runner, $judge, $testcnt) = map { $meta->{$_} or die "Required parameter missing: $_"} qw/files generator runner judge testcnt/;
+               my ($files, $generator, $runner, $judge, $testcnt) = map { $meta->{$_} or die "Required parameter missing: $_\n"} qw/files generator runner judge testcnt/;
 
                $generator = safe_can Generator => generate => $generator;
                $runner = safe_can Runner => run => $runner;
@@ -111,56 +117,62 @@ sub process{
 
                my %results = $judge->(@results);
                $meta->{$_} = $results{$_} for keys %results;
+               $meta->{results} = \@full_results
        } catch {
-               s,(.*) at .*,$1,;
+               s/(.*) at .*/$1/s;
                chomp;
                $meta->{result} = -1;
                $meta->{result_text} = $_;
        };
-
-       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";
-       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/";
-       purge "/$log/st";
-       purge "/$log/$_" for $page - 1, $page, $page + 1;
+       stopvms;
+
+       get_logger->info('Job result: ' . $meta->{result_text});
+       return unless $jobr;
+       $jobr->update({
+               result => $meta->{result},
+               result_text => $meta->{result_text},
+               ($meta->{results} ? (results => encode_json $meta->{results}) : ()),
+               $errors ? (errors => $errors) : ()
+       });
+
+       my $solved = $meta->{result} ? 0 : 1;
+       my $status = $db->problem_statuses->find($jobr->owner, $jobr->problem);
+       $db->problem_statuses->update_or_create({
+               problem => $jobr->problem,
+               owner   => $jobr->owner,
+               job     => $jobr->id,
+               solved  => $solved}) if !$jobr->problem->private;
 }
 
 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");
        }
 }
 
 sub run{
+       require Gruntmaster::Data;
+       $db = Gruntmaster::Data->connect($ENV{GRUNTMASTER_DSN} // 'dbi:Pg:');
        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
+       my $dir = tempdir 'gruntmasterd.XXXX', CLEANUP => 1, TMPDIR => 1;
+       chmod 0711, $dir;
+       chdir $dir;
+       while (1) {
+               my $job = $db->jobs->search({daemon => undef}, {rows => 1})->first;
+               got_job $job if defined $job;
+               sleep 2 unless defined $job;
+       }
 }
 
 1;
This page took 0.03487 seconds and 4 git commands to generate.