Split database-related work from process to another sub
authorMarius Gavrilescu <marius@ieval.ro>
Sun, 8 Feb 2015 14:23:17 +0000 (16:23 +0200)
committerMarius Gavrilescu <marius@ieval.ro>
Sun, 8 Feb 2015 14:23:17 +0000 (16:23 +0200)
lib/Gruntmaster/Daemon.pm

index 1c58550..3a83692 100644 (file)
@@ -45,36 +45,12 @@ sub safe_can {
 }
 
 sub process{
-       my ($job, $jobr) = @_;
+       my ($meta) = @_;
 
        my @results;
        my @full_results = ();
-       my $meta;
        our $errors = '';
        try {
-               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';
-
-                       $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;
 
@@ -119,23 +95,48 @@ sub process{
                $meta->{result_text} = $_;
        };
        stopvms;
+       $meta->{errors} = $errors;
 
        get_logger->info('Job result: ' . $meta->{result_text});
-       return unless $jobr;
-       $jobr->update({
+}
+
+sub process_job {
+       my ($job) = @_;
+       my $meta = {
+               problem => $job->problem->id,
+               files => {
+                       prog => {
+                               name => 'prog.' . $job->extension,
+                               format => $job->format,
+                               content => $job->source,
+                       },
+               },
+               map { $_ => $job->problem->get_column($_) } qw/generator runner judge testcnt timeout olimit/
+       };
+       $meta->{tests} = decode_json $job->problem->tests if $meta->{runner} eq 'File';
+
+       $meta->{files}{ver} = {
+               name => 'ver.' . FORMAT_EXTENSION->{$job->problem->verformat},
+               format => $job->problem->verformat,
+               content => $job->problem->versource,
+       } if $job->problem->verformat;
+
+       process $meta;
+
+       $job->update({
                result => $meta->{result},
                result_text => $meta->{result_text},
                ($meta->{results} ? (results => encode_json $meta->{results}) : ()),
-               $errors ? (errors => $errors) : ()
+               $meta->{errors} ? (errors => $meta->{errors}) : ()
        });
 
        my $solved = $meta->{result} ? 0 : 1;
-       my $status = $db->problem_statuses->find($jobr->owner, $jobr->problem);
+       my $status = $db->problem_statuses->find($job->owner, $job->problem);
        $db->problem_statuses->update_or_create({
-               problem => $jobr->problem,
-               owner   => $jobr->owner,
-               job     => $jobr->id,
-               solved  => $solved}) if !$jobr->problem->is_private;
+               problem => $job->problem,
+               owner   => $job->owner,
+               job     => $job->id,
+               solved  => $solved}) if !$job->problem->is_private;
 }
 
 sub got_job{
@@ -147,7 +148,7 @@ sub got_job{
        #if (set_job_daemon $job, hostname . ":$$") {
        if (1) {
                get_logger->debug("Succesfully taken job $id");
-               process $id, $job;
+               process_job $job;
                get_logger->debug("Job $id done");
        } else {
                get_logger->debug("Job $id already taken");
This page took 0.038526 seconds and 4 git commands to generate.