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