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