Make sure work directory is world-searchable
[gruntmaster-daemon.git] / lib / Gruntmaster / Daemon.pm
index 7fef7f6..1e5d8b2 100644 (file)
@@ -4,11 +4,10 @@ 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/;
@@ -17,12 +16,14 @@ 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',
@@ -32,15 +33,12 @@ use constant FORMAT_EXTENSION => {
 
 ##################################################
 
-my $db = Gruntmaster::Data->connect('dbi:Pg:');
-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);
@@ -52,14 +50,6 @@ sub safe_can {
        safe_can_nodie @_ or get_logger->logdie("No such \l$type: '$name'");
 }
 
-sub purge {
-       get_logger->trace("Purging $_[0]");
-       for my $host (@purge_hosts) {
-               my $req = HTTP::Request->new(PURGE => "http://$host$_[0]");
-               $ua->request($req)
-       }
-}
-
 sub process{
        my ($job, $jobr) = @_;
 
@@ -68,29 +58,33 @@ sub process{
        my $meta;
        our $errors = '';
        try {
-               $meta = {
-                       problem => $jobr->problem->id,
-                       files => {
-                               prog => {
-                                       name => 'prog.' . $jobr->extension,
-                                       format => $jobr->format,
-                                       content => $jobr->source,
+               if (ref $job) {
+                       $meta = $job;
+               } else {
+                       $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';
+                               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->{files}{ver} = {
-                       name => 'ver.' . FORMAT_EXTENSION->{$jobr->problem->verformat},
-                       format => $jobr->problem->verformat,
-                       content => $jobr->problem->versource,
-               } if $jobr->problem->verformat;
+                       $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;
@@ -123,28 +117,31 @@ 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} = $_;
        };
+       stopvms;
 
-       get_logger->info("Job result: " . $meta->{result_text});
+       get_logger->info('Job result: ' . $meta->{result_text});
+       return unless $jobr;
        $jobr->update({
                result => $meta->{result},
                result_text => $meta->{result_text},
-               results => encode_json \@full_results,
+               ($meta->{results} ? (results => encode_json $meta->{results}) : ()),
                $errors ? (errors => $errors) : ()
        });
 
-       my $log = $jobr->contest ? 'ct/' . $jobr->contest->id . '/log' : 'log';
-       my $page = int (($job + PAGE_SIZE - 1) / PAGE_SIZE);
-
-       purge "/$log/$job";
-       purge "/$log/";
-       purge "/$log/st";
-       purge "/$log/page/$_" for $page - 1, $page, $page + 1;
+       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{
@@ -164,9 +161,13 @@ sub got_job{
 }
 
 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;
+       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;
This page took 0.029938 seconds and 4 git commands to generate.