]>
Commit | Line | Data |
---|---|---|
5c5cd38a MG |
1 | package Gruntmaster::Daemon; |
2 | ||
3 | use 5.014000; | |
4 | use strict; | |
5 | use warnings; | |
6 | ||
3e7fd903 | 7 | our $VERSION = '5999.000_004'; |
5c5cd38a MG |
8 | |
9 | use Gruntmaster::Daemon::Constants qw/ERR/; | |
fdb99417 | 10 | use Gruntmaster::Daemon::Format qw/prepare_files stopvms/; |
5c5cd38a | 11 | |
ab436d78 | 12 | use File::Temp qw/tempdir/; |
be3e5b32 | 13 | use JSON qw/decode_json encode_json/; |
ab436d78 | 14 | use Sys::Hostname qw/hostname/; |
5c5cd38a MG |
15 | use Time::HiRes qw/time/; |
16 | use Try::Tiny; | |
5c5cd38a MG |
17 | use Log::Log4perl qw/get_logger/; |
18 | ||
6bf57d23 MG |
19 | use constant FORMAT_EXTENSION => { |
20 | C => 'c', | |
21 | CPP => 'cpp', | |
5a4392d5 MG |
22 | GCCGO => 'go', |
23 | GOLANG => 'go', | |
24 | HASKELL => 'hs', | |
6bf57d23 MG |
25 | MONO => 'cs', |
26 | JAVA => 'java', | |
27 | PASCAL => 'pas', | |
28 | PERL => 'pl', | |
29 | PYTHON => 'py', | |
30 | }; | |
adb44605 | 31 | |
5c5cd38a MG |
32 | ################################################## |
33 | ||
7be5f054 | 34 | my $db; |
064332cd | 35 | |
b4ac22ff | 36 | sub safe_can { |
a722431b | 37 | my ($type, $sub, $name) = @_; |
5c5cd38a | 38 | |
fcfcfb93 | 39 | get_logger->logdie("Invalid \l$type: '$name'") unless $name =~ /^\w+$/s; |
a722431b MG |
40 | my $pkg = __PACKAGE__ . "::${type}::${name}"; |
41 | eval "require $pkg" or get_logger->warn("Error while requiring $pkg: $@"); | |
b4ac22ff | 42 | $pkg->can($sub) or get_logger->logdie("No such \l$type: '$name'"); |
5c5cd38a MG |
43 | } |
44 | ||
ab436d78 | 45 | sub process{ |
867a9dad | 46 | my ($meta) = @_; |
a722431b MG |
47 | |
48 | my @results; | |
49 | my @full_results = (); | |
a722431b MG |
50 | our $errors = ''; |
51 | try { | |
a722431b MG |
52 | prepare_files $meta; |
53 | chomp $errors; | |
54 | ||
99d37110 | 55 | my ($files, $generator, $runner, $judge, $testcnt) = map { $meta->{$_} or die "Required parameter missing: $_\n"} qw/files generator runner judge testcnt/; |
a722431b MG |
56 | |
57 | $generator = safe_can Generator => generate => $generator; | |
58 | $runner = safe_can Runner => run => $runner; | |
59 | $judge = safe_can Judge => judge => $judge; | |
60 | ||
61 | for my $test (1 .. $testcnt) { | |
62 | my $start_time = time; | |
63 | my $result; | |
64 | try { | |
65 | $generator->($test, $meta); | |
66 | $result = $runner->($test, $meta); | |
67 | } catch { | |
68 | $result = $_; | |
69 | unless (ref $result) { | |
70 | chomp $result; | |
71 | $result = [ERR, $result]; | |
72 | } | |
73 | }; | |
74 | ||
75 | if (ref $result) { | |
76 | get_logger->trace("Test $test result is " . $result->[1]); | |
77 | push @full_results, {id => $test, result => $result->[0], result_text => $result->[1], time => time - $start_time} | |
78 | } else { | |
79 | get_logger->trace("Test $test result is $result"); | |
80 | push @full_results, {id => $test, result => 0, result_text => $result, time => time - $start_time} | |
81 | } | |
82 | push @results, $result; | |
83 | last if $meta->{judge} eq 'Absolute' && ref $result | |
5c5cd38a | 84 | } |
5c5cd38a | 85 | |
a722431b MG |
86 | my %results = $judge->(@results); |
87 | $meta->{$_} = $results{$_} for keys %results; | |
7be5f054 | 88 | $meta->{results} = \@full_results |
a722431b | 89 | } catch { |
99d37110 | 90 | s/(.*) at .*/$1/s; |
a722431b MG |
91 | chomp; |
92 | $meta->{result} = -1; | |
93 | $meta->{result_text} = $_; | |
94 | }; | |
fdb99417 | 95 | stopvms; |
867a9dad | 96 | $meta->{errors} = $errors; |
a722431b | 97 | |
99d37110 | 98 | get_logger->info('Job result: ' . $meta->{result_text}); |
867a9dad MG |
99 | } |
100 | ||
101 | sub process_job { | |
102 | my ($job) = @_; | |
103 | my $meta = { | |
104 | problem => $job->problem->id, | |
105 | files => { | |
106 | prog => { | |
107 | name => 'prog.' . $job->extension, | |
108 | format => $job->format, | |
109 | content => $job->source, | |
110 | }, | |
111 | }, | |
da822e3c | 112 | map { $_ => $job->problem->get_column($_) } qw/generator runner judge precnt testcnt timeout olimit/ |
867a9dad MG |
113 | }; |
114 | $meta->{tests} = decode_json $job->problem->tests if $meta->{runner} eq 'File'; | |
da822e3c MG |
115 | delete $meta->{precnt} if $job->problem->is_in_archive; |
116 | $meta->{testcnt} = $meta->{precnt} if $meta->{precnt}; | |
867a9dad MG |
117 | |
118 | $meta->{files}{ver} = { | |
119 | name => 'ver.' . FORMAT_EXTENSION->{$job->problem->verformat}, | |
120 | format => $job->problem->verformat, | |
121 | content => $job->problem->versource, | |
122 | } if $job->problem->verformat; | |
123 | ||
124 | process $meta; | |
125 | ||
da822e3c | 126 | $meta->{result_text} .= ' (pretests)' if $meta->{precnt}; |
867a9dad | 127 | $job->update({ |
be3e5b32 MG |
128 | result => $meta->{result}, |
129 | result_text => $meta->{result_text}, | |
6555bf80 | 130 | ($meta->{results} ? (results => encode_json $meta->{results}) : ()), |
867a9dad | 131 | $meta->{errors} ? (errors => $meta->{errors}) : () |
be3e5b32 MG |
132 | }); |
133 | ||
b335c4bb | 134 | my $solved = $meta->{result} ? 0 : 1; |
867a9dad | 135 | my $status = $db->problem_statuses->find($job->owner, $job->problem); |
b335c4bb | 136 | $db->problem_statuses->update_or_create({ |
867a9dad MG |
137 | problem => $job->problem, |
138 | owner => $job->owner, | |
139 | job => $job->id, | |
140 | solved => $solved}) if !$job->problem->is_private; | |
ab436d78 MG |
141 | } |
142 | ||
143 | sub got_job{ | |
be3e5b32 MG |
144 | my $job = $_[0]; |
145 | my $id = $job->id; | |
146 | get_logger->debug("Taking job $id..."); | |
147 | my $daemon = hostname . ":$$"; | |
19f0465b MG |
148 | $job->update({daemon => $daemon}, {where => {daemon => undef}})->discard_changes; |
149 | if ($job->daemon eq $daemon) { | |
be3e5b32 | 150 | get_logger->debug("Succesfully taken job $id"); |
867a9dad | 151 | process_job $job; |
be3e5b32 | 152 | get_logger->debug("Job $id done"); |
ab436d78 | 153 | } else { |
19f0465b | 154 | get_logger->debug("Job $id already taken by " . $job->daemon); |
ab436d78 | 155 | } |
5c5cd38a MG |
156 | } |
157 | ||
158 | sub run{ | |
55e61ae8 | 159 | require Gruntmaster::Data; |
9a69bbad | 160 | $db = Gruntmaster::Data->connect($ENV{GRUNTMASTER_DSN} // 'dbi:Pg:'); |
a722431b MG |
161 | Log::Log4perl->init('/etc/gruntmasterd/gruntmasterd-log.conf'); |
162 | get_logger->info("gruntmasterd $VERSION started"); | |
01d59515 MG |
163 | my $dir = tempdir 'gruntmasterd.XXXX', CLEANUP => 1, TMPDIR => 1; |
164 | chmod 0711, $dir; | |
165 | chdir $dir; | |
be3e5b32 MG |
166 | while (1) { |
167 | my $job = $db->jobs->search({daemon => undef}, {rows => 1})->first; | |
168 | got_job $job if defined $job; | |
169 | sleep 2 unless defined $job; | |
170 | } | |
5c5cd38a MG |
171 | } |
172 | ||
173 | 1; | |
174 | __END__ | |
5c5cd38a MG |
175 | |
176 | =head1 NAME | |
177 | ||
bc372959 | 178 | Gruntmaster::Daemon - Gruntmaster 6000 Online Judge -- daemon |
5c5cd38a MG |
179 | |
180 | =head1 SYNOPSIS | |
181 | ||
182 | use Gruntmaster::Daemon; | |
bc372959 | 183 | Gruntmaster::Daemon->run; |
5c5cd38a MG |
184 | |
185 | =head1 DESCRIPTION | |
186 | ||
bc372959 | 187 | Gruntmaster::Daemon is the daemon component of the Gruntmaster 6000 online judge. |
5c5cd38a MG |
188 | |
189 | =head1 AUTHOR | |
190 | ||
bc372959 | 191 | Marius Gavrilescu E<lt>marius@ieval.roE<gt> |
5c5cd38a MG |
192 | |
193 | =head1 COPYRIGHT AND LICENSE | |
194 | ||
bc372959 | 195 | Copyright (C) 2014 by Marius Gavrilescu |
5c5cd38a | 196 | |
bc372959 MG |
197 | This library is free software: you can redistribute it and/or modify |
198 | it under the terms of the GNU Affero General Public License as published by | |
199 | the Free Software Foundation, either version 3 of the License, or | |
200 | (at your option) any later version. | |
5c5cd38a MG |
201 | |
202 | ||
203 | =cut |