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