From 867a9dad51e008cbab98f178cfa28013aa3b5b12 Mon Sep 17 00:00:00 2001 From: Marius Gavrilescu Date: Sun, 8 Feb 2015 16:23:17 +0200 Subject: [PATCH] Split database-related work from process to another sub --- lib/Gruntmaster/Daemon.pm | 69 ++++++++++++++++++++------------------- 1 file changed, 35 insertions(+), 34 deletions(-) diff --git a/lib/Gruntmaster/Daemon.pm b/lib/Gruntmaster/Daemon.pm index 1c58550..3a83692 100644 --- a/lib/Gruntmaster/Daemon.pm +++ b/lib/Gruntmaster/Daemon.pm @@ -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"); -- 2.39.2