Use new Gruntmaster::Data
authorMarius Gavrilescu <marius@ieval.ro>
Tue, 16 Jun 2015 14:30:16 +0000 (17:30 +0300)
committerMarius Gavrilescu <marius@ieval.ro>
Mon, 28 Sep 2015 10:21:37 +0000 (13:21 +0300)
lib/Gruntmaster/Daemon.pm

index 048321e..cc4a129 100644 (file)
@@ -35,8 +35,6 @@ use constant FORMAT_EXTENSION => {
 
 ##################################################
 
-my $db;
-
 sub safe_can {
        my ($type, $sub, $name) = @_;
 
@@ -107,73 +105,62 @@ sub process{
 
 sub process_job {
        my ($job) = @_;
+       my $pb = problem_full($job->{problem});
        my $meta = {
-               problem => $job->problem->id,
+               problem => $job->{problem},
                files => {
                        prog => {
-                               name => 'prog.' . $job->extension,
-                               format => $job->format,
-                               content => $job->source,
+                               name => 'prog.' . $job->{extension},
+                               format => $job->{format},
+                               content => $job->{source},
                        },
                },
-               map { $_ => $job->problem->get_column($_) } qw/generator runner judge precnt testcnt timeout olimit/
+               map { $_ => $pb->{$_} } qw/generator runner judge precnt testcnt timeout olimit/
        };
-       my $limit_override = $job->result_source->schema->limits->find($meta->{problem}, $job->format);
-       $meta->{timeout} = $limit_override->timeout if $limit_override;
-       $meta->{tests} = decode_json $job->problem->tests if $meta->{runner} eq 'File';
-       delete $meta->{precnt} unless $job->contest && $job->contest->is_running;
+       my $timeout_override = db()->query('SELECT timeout FROM limits WHERE problem=? AND format=?', $job->{problem}, $job->{format})->list;
+       $meta->{timeout} = $timeout_override if defined $timeout_override;
+       $meta->{tests} = decode_json $pb->{tests} if $meta->{runner} eq 'File';
+       $job->{contest} &&= contest_entry($job->{contest});
+       delete $meta->{precnt} unless $job->{contest} && $job->{contest}{started} && !$job->{contest}{finished};
        $meta->{testcnt} = $meta->{precnt} if $meta->{precnt};
 
        $meta->{files}{ver} = {
-               name => 'ver.' . FORMAT_EXTENSION->{$job->problem->verformat},
-               format => $job->problem->verformat,
-               content => $job->problem->versource,
-       } if $job->problem->verformat;
+               name => 'ver.' . FORMAT_EXTENSION->{$pb->{verformat}},
+               format => $pb->{verformat},
+               content => $pb->{versource},
+       } if $pb->{verformat};
 
        process $meta;
 
        $meta->{result_text} .= ' (pretests)' if $meta->{precnt};
-       $job->update({
-               result => $meta->{result},
-               result_text => $meta->{result_text},
-               ($meta->{results} ? (results => encode_json $meta->{results}) : ()),
-               $meta->{errors} ? (errors => $meta->{errors}) : ()
-       });
-
-       my $solved = $meta->{result} ? 0 : 1;
-       my $status = $db->problem_statuses->find($job->owner, $job->problem);
-       $db->problem_statuses->update_or_create({
-               problem => $job->problem,
-               owner   => $job->owner,
-               job     => $job->id,
-               solved  => $solved}) if !$job->problem->private;
+       finish_job($job, $job->{private} || $pb->{private},
+         result => $meta->{result},
+         result_text => $meta->{result_text},
+         ($meta->{results} ? (results => encode_json $meta->{results}) : ()),
+         $meta->{errors} ? (errors => $meta->{errors}) : ());
 }
 
 sub got_job{
        my $job = $_[0];
-       my $id = $job->id;
-       get_logger->debug("Taking job $id...");
-       my $daemon = hostname . ":$$";
-       $job->update({daemon => $daemon}, {where => {daemon => undef}})->discard_changes;
-       if ($job->daemon eq $daemon) {
-               get_logger->debug("Succesfully taken job $id");
-               process_job $job;
-               get_logger->debug("Job $id done");
-       } else {
-               get_logger->debug("Job $id already taken by " . $job->daemon);
-       }
+       my $id = $job->{id};
+       get_logger->debug("Processing job $id...");
+       process_job $job;
+       get_logger->debug("Job $id done");
 }
 
+my $daemon = hostname . ":$$";
+
 sub run{
        require Gruntmaster::Data;
-       $db = Gruntmaster::Data->connect($ENV{GRUNTMASTER_DSN} // 'dbi:Pg:');
+       Gruntmaster::Data->import;
+       Gruntmaster::Data::init($ENV{GRUNTMASTER_DSN} // 'dbi:Pg:');
        Log::Log4perl->init('/etc/gruntmasterd/gruntmasterd-log.conf');
-       get_logger->info("gruntmasterd $VERSION started");
+       get_logger->info("gruntmasterd $VERSION started ($daemon)");
        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;
+               my $job = take_job($daemon);
                got_job $job if defined $job;
                sleep 2 unless defined $job;
        }
This page took 0.014651 seconds and 4 git commands to generate.