Fix purging of standings
[gruntmaster-daemon.git] / lib / Gruntmaster / Daemon.pm
1 package Gruntmaster::Daemon;
2
3 use 5.014000;
4 use strict;
5 use warnings;
6
7 our $VERSION = '5999.000_005';
8
9 use Gruntmaster::Daemon::Constants qw/ERR/;
10 use Gruntmaster::Daemon::Format qw/prepare_files stopvms/;
11
12 use File::Slurp qw/read_file/;
13 use File::Temp qw/tempdir/;
14 use JSON qw/decode_json encode_json/;
15 use Sys::Hostname qw/hostname/;
16 use Try::Tiny;
17 use Log::Log4perl qw/get_logger/;
18
19 use constant FORMAT_EXTENSION => {
20 BRAINFUCK => 'bf',
21 C => 'c',
22 CPP => 'cpp',
23 D => 'd',
24 GCCGO => 'go',
25 GOLANG => 'go',
26 GOLFSCRIPT => 'gs',
27 HASKELL => 'hs',
28 JAVA => 'java',
29 JULIA => 'jl',
30 MONO => 'cs',
31 OBERON => 'm',
32 OCAML => 'ml',
33 PASCAL => 'pas',
34 PERL => 'pl',
35 PHP => 'php',
36 PYTHON => 'py',
37 PYTHON3 => 'py',
38 RUBY => 'rb',
39 RUST => 'rs',
40 SBCL => 'l',
41 };
42
43 ##################################################
44
45 sub safe_can {
46 my ($type, $sub, $name) = @_;
47
48 get_logger->logdie("Invalid \l$type: '$name'") unless $name =~ /^\w+$/s;
49 my $pkg = __PACKAGE__ . "::${type}::${name}";
50 eval "require $pkg" or get_logger->warn("Error while requiring $pkg: $@");
51 $pkg->can($sub) or get_logger->logdie("No such \l$type: '$name'");
52 }
53
54 sub process{
55 my ($meta) = @_;
56
57 my @results;
58 my @full_results = ();
59 our $errors = '';
60 try {
61 prepare_files $meta;
62 chomp $errors;
63
64 my ($files, $generator, $runner, $judge, $testcnt) = map { $meta->{$_} or die "Required parameter missing: $_\n"} qw/files generator runner judge testcnt/;
65
66 $generator = safe_can Generator => generate => $generator;
67 $runner = safe_can Runner => run => $runner;
68 $judge = safe_can Judge => judge => $judge;
69
70 for my $test (1 .. $testcnt) {
71 my $result;
72 unlink 'result';
73 try {
74 $generator->($test, $meta);
75 $result = $runner->($test, $meta);
76 } catch {
77 $result = $_;
78 unless (ref $result) {
79 chomp $result;
80 $result = [ERR, $result];
81 }
82 };
83
84 my $time = '?';
85 $time = (read_file 'time-stop-prog') - (read_file 'time-start-prog') if -f 'time-stop-prog' && -f 'time-start-prog';
86 unlink 'time-start-prog', 'time-stop-prog';
87 if (ref $result) {
88 my $result_text = $result->[1];
89 $result_text .= ': ' . read_file 'result' if -s 'result';
90 get_logger->trace("Test $test result is " . $result_text);
91 push @full_results, {id => $test, result => $result->[0], result_text => $result_text, time => $time}
92 } else {
93 get_logger->trace("Test $test result is $result");
94 push @full_results, {id => $test, result => 0, result_text => $result, time => $time}
95 }
96 push @results, $result;
97 last if $meta->{judge} eq 'Absolute' && ref $result
98 }
99
100 my %results = $judge->(@results);
101 $meta->{$_} = $results{$_} for keys %results;
102 $meta->{results} = \@full_results
103 } catch {
104 s/(.*) at .*/$1/s;
105 chomp;
106 $meta->{result} = -1;
107 $meta->{result_text} = $_;
108 };
109 stopvms;
110 $meta->{errors} = $errors;
111
112 get_logger->info('Job result: ' . $meta->{result_text});
113 }
114
115 sub process_job {
116 my ($job) = @_;
117 my $pb = db()->select('problems', '*', {id => $job->{problem}})->hash;
118 my $meta = {
119 problem => $job->{problem},
120 files => {
121 prog => {
122 name => 'prog.' . $job->{extension},
123 format => $job->{format},
124 content => $job->{source},
125 },
126 },
127 map { $_ => $pb->{$_} } qw/generator runner judge precnt testcnt timeout olimit/
128 };
129 my $timeout_override = db()->query('SELECT timeout FROM limits WHERE problem=? AND format=?', $job->{problem}, $job->{format})->list;
130 $meta->{timeout} = $timeout_override if defined $timeout_override;
131 $meta->{tests} = decode_json $pb->{tests} if $meta->{runner} eq 'File';
132 my $contest = $job->{contest} && contest_entry($job->{contest});
133 delete $meta->{precnt} unless $contest && $contest->{started} && !$contest->{finished}; ## no critic (ProhibitNegativeExpressionsInUnlessAndUntilConditions)
134 $meta->{testcnt} = $meta->{precnt} if $meta->{precnt};
135
136 $meta->{files}{ver} = {
137 name => 'ver.' . FORMAT_EXTENSION->{$pb->{verformat}},
138 format => $pb->{verformat},
139 content => $pb->{versource},
140 } if $pb->{verformat};
141
142 process $meta;
143
144 $meta->{result_text} .= ' (pretests)' if $meta->{precnt};
145 finish_job($job, $job->{private} || $pb->{private},
146 result => $meta->{result},
147 result_text => $meta->{result_text},
148 ($meta->{results} ? (results => encode_json $meta->{results}) : ()),
149 $meta->{errors} ? (errors => $meta->{errors}) : ());
150 }
151
152 sub got_job{
153 my $job = $_[0];
154 my $id = $job->{id};
155 get_logger->debug("Processing job $id...");
156 process_job $job;
157 get_logger->debug("Job $id done");
158 }
159
160 my $daemon = hostname . ":$$";
161
162 sub run{
163 require Gruntmaster::Data;
164 Gruntmaster::Data->import;
165 dbinit($ENV{GRUNTMASTER_DSN} // 'dbi:Pg:');
166 Log::Log4perl->init('/etc/gruntmasterd/gruntmasterd-log.conf');
167 get_logger->info("gruntmasterd $VERSION started ($daemon)");
168 my $dir = tempdir 'gruntmasterd.XXXX', CLEANUP => 1, TMPDIR => 1;
169 chmod 0711, $dir;
170 chdir $dir;
171 while (1) {
172 my $job = take_job($daemon);
173 got_job $job if defined $job;
174 sleep 2 unless defined $job;
175 }
176 }
177
178 1;
179 __END__
180
181 =head1 NAME
182
183 Gruntmaster::Daemon - Gruntmaster 6000 Online Judge -- daemon
184
185 =head1 SYNOPSIS
186
187 use Gruntmaster::Daemon;
188 Gruntmaster::Daemon->run;
189
190 =head1 DESCRIPTION
191
192 Gruntmaster::Daemon is the daemon component of the Gruntmaster 6000 online judge.
193
194 =head1 AUTHOR
195
196 Marius Gavrilescu E<lt>marius@ieval.roE<gt>
197
198 =head1 COPYRIGHT AND LICENSE
199
200 Copyright (C) 2014 by Marius Gavrilescu
201
202 This library is free software: you can redistribute it and/or modify
203 it under the terms of the GNU Affero General Public License as published by
204 the Free Software Foundation, either version 3 of the License, or
205 (at your option) any later version.
206
207
208 =cut
This page took 0.072809 seconds and 5 git commands to generate.