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