From: Marius Gavrilescu Date: Tue, 16 Jun 2015 14:29:45 +0000 (+0300) Subject: Add some functions for Gruntmaster::Daemon X-Git-Tag: 5999.000_014~19 X-Git-Url: http://git.ieval.ro/?a=commitdiff_plain;h=9dc335bd50640a567127057a9e9cf8e2a7552573;p=gruntmaster-data.git Add some functions for Gruntmaster::Daemon --- diff --git a/lib/Gruntmaster/Data.pm b/lib/Gruntmaster/Data.pm index ea4fa3b..8a0a723 100644 --- a/lib/Gruntmaster/Data.pm +++ b/lib/Gruntmaster/Data.pm @@ -4,7 +4,7 @@ use warnings; use parent qw/Exporter/; our $VERSION = '5999.000_013'; -our @EXPORT = qw/purge db user_list user_entry problem_list problem_entry contest_list contest_entry contest_full contest_has_problem job_list job_entry job_full create_job standings update_status rerun_job/; ## no critic (ProhibitAutomaticExportation) +our @EXPORT = qw/purge db user_list user_entry problem_list problem_entry problem_full contest_list contest_entry contest_full contest_has_problem job_list job_entry job_full create_job standings update_status rerun_job take_job finish_job/; ## no critic (ProhibitAutomaticExportation) use JSON::MaybeXS qw/decode_json/; use HTTP::Tiny; @@ -33,6 +33,7 @@ my %statements = ( opens_sth => 'SELECT problem,owner,time FROM opens WHERE contest = ?', problem_entry_sth => 'SELECT ' . (join ',', @{PROBLEM_PUBLIC_COLUMNS()}, 'statement', 'solution') . ' FROM problems WHERE id = ?', + problem_full_sth => 'SELECT * FROM problems WHERE id = ?', limits_sth => 'SELECT format,timeout FROM limits WHERE problem = ?', problem_values_sth => 'SELECT id,value FROM problems', @@ -40,6 +41,7 @@ my %statements = ( job_full_sth => 'SELECT * FROM jobs WHERE id = ?', rerun_job_sth => 'UPDATE jobs SET daemon=NULL,result=-2,result_text=NULL,results=NULL,errors=NULL WHERE id = ?', + take_job_sth => 'UPDATE jobs SET daemon=? WHERE id = (SELECT id FROM jobs WHERE daemon IS NULL LIMIT 1 FOR UPDATE) RETURNING id', ); our $db; @@ -135,6 +137,11 @@ sub problem_entry { $ret } +sub problem_full { + my ($id) = @_; + scalar query(problem_full_sth => $id)->hash; +} + sub contest_list { my $ret = add_names query('contest_list_sth')->hashes; @@ -292,6 +299,27 @@ sub rerun_job { purge "/log/$id"; } +sub take_job { + my ($daemon) = @_; + my $id = query(take_job_sth => $daemon)->list; + return $id ? job_full $id : undef; +} + +sub finish_job { + my ($job, $private, %args) = @_; + db->update(jobs => \%args, {id => $job->{id}}); + return if $private; + my $status = { + problem => $job->{problem}, + owner => $job->{owner}, + job => $job->{id}, + solved => ($args{result} ? 0 : 1), + }; + eval { + db->insert(problem_status => $status) + } or db->update(problem_status => $status, {owner => $job->{owner}, problem => $job->{problem}}); +} + my @PURGE_HOSTS = exists $ENV{PURGE_HOSTS} ? split ' ', $ENV{PURGE_HOSTS} : (); my $ht = HTTP::Tiny->new;