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