Add some functions for Gruntmaster::Daemon
authorMarius Gavrilescu <marius@ieval.ro>
Tue, 16 Jun 2015 14:29:45 +0000 (17:29 +0300)
committerMarius Gavrilescu <marius@ieval.ro>
Tue, 16 Jun 2015 14:29:45 +0000 (17:29 +0300)
lib/Gruntmaster/Data.pm

index ea4fa3bb351a21cd532d518986c5c4ec014b9a5c..8a0a7239e76359bc78509dffe5b6e74a6a193972 100644 (file)
@@ -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;
 
This page took 0.011474 seconds and 4 git commands to generate.