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;
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',
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;
$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;
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;