line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Gruntmaster::Daemon; |
2
|
|
|
|
|
|
|
|
3
|
14
|
|
|
14
|
|
28906
|
use 5.014000; |
|
14
|
|
|
|
|
43
|
|
4
|
14
|
|
|
14
|
|
71
|
use strict; |
|
14
|
|
|
|
|
14
|
|
|
14
|
|
|
|
|
268
|
|
5
|
14
|
|
|
14
|
|
56
|
use warnings; |
|
14
|
|
|
|
|
19
|
|
|
14
|
|
|
|
|
583
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
our $VERSION = '5999.000_005'; |
8
|
|
|
|
|
|
|
|
9
|
14
|
|
|
14
|
|
6987
|
use Gruntmaster::Daemon::Constants qw/ERR/; |
|
14
|
|
|
|
|
29
|
|
|
14
|
|
|
|
|
895
|
|
10
|
14
|
|
|
14
|
|
7335
|
use Gruntmaster::Daemon::Format qw/prepare_files stopvms/; |
|
14
|
|
|
|
|
54
|
|
|
14
|
|
|
|
|
971
|
|
11
|
|
|
|
|
|
|
|
12
|
14
|
|
|
14
|
|
98
|
use File::Slurp qw/read_file/; |
|
14
|
|
|
|
|
27
|
|
|
14
|
|
|
|
|
617
|
|
13
|
14
|
|
|
14
|
|
16605
|
use File::Temp qw/tempdir/; |
|
14
|
|
|
|
|
130540
|
|
|
14
|
|
|
|
|
909
|
|
14
|
14
|
|
|
14
|
|
14373
|
use JSON qw/decode_json encode_json/; |
|
14
|
|
|
|
|
177541
|
|
|
14
|
|
|
|
|
96
|
|
15
|
14
|
|
|
14
|
|
2124
|
use Sys::Hostname qw/hostname/; |
|
14
|
|
|
|
|
28
|
|
|
14
|
|
|
|
|
842
|
|
16
|
14
|
|
|
14
|
|
278
|
use Time::HiRes qw/time/; |
|
14
|
|
|
|
|
29
|
|
|
14
|
|
|
|
|
114
|
|
17
|
14
|
|
|
14
|
|
1836
|
use Try::Tiny; |
|
14
|
|
|
|
|
15
|
|
|
14
|
|
|
|
|
770
|
|
18
|
14
|
|
|
14
|
|
136
|
use Log::Log4perl qw/get_logger/; |
|
14
|
|
|
|
|
28
|
|
|
14
|
|
|
|
|
113
|
|
19
|
|
|
|
|
|
|
|
20
|
14
|
|
|
|
|
18032
|
use constant FORMAT_EXTENSION => { |
21
|
|
|
|
|
|
|
C => 'c', |
22
|
|
|
|
|
|
|
CPP => 'cpp', |
23
|
|
|
|
|
|
|
GCCGO => 'go', |
24
|
|
|
|
|
|
|
GOLANG => 'go', |
25
|
|
|
|
|
|
|
GOLFSCRIPT => 'gs', |
26
|
|
|
|
|
|
|
HASKELL => 'hs', |
27
|
|
|
|
|
|
|
MONO => 'cs', |
28
|
|
|
|
|
|
|
JAVA => 'java', |
29
|
|
|
|
|
|
|
PASCAL => 'pas', |
30
|
|
|
|
|
|
|
PERL => 'pl', |
31
|
|
|
|
|
|
|
PYTHON => 'py', |
32
|
|
|
|
|
|
|
RUBY => 'rb', |
33
|
|
|
|
|
|
|
SBCL => 'l', |
34
|
14
|
|
|
14
|
|
1086
|
}; |
|
14
|
|
|
|
|
28
|
|
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
################################################## |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
sub safe_can { |
39
|
12
|
|
|
12
|
0
|
30
|
my ($type, $sub, $name) = @_; |
40
|
|
|
|
|
|
|
|
41
|
12
|
100
|
|
|
|
84
|
get_logger->logdie("Invalid \l$type: '$name'") unless $name =~ /^\w+$/s; |
42
|
9
|
|
|
|
|
33
|
my $pkg = __PACKAGE__ . "::${type}::${name}"; |
43
|
9
|
100
|
|
|
|
651
|
eval "require $pkg" or get_logger->warn("Error while requiring $pkg: $@"); |
44
|
9
|
100
|
|
|
|
285
|
$pkg->can($sub) or get_logger->logdie("No such \l$type: '$name'"); |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
sub process{ |
48
|
99
|
|
|
99
|
0
|
1074504
|
my ($meta) = @_; |
49
|
|
|
|
|
|
|
|
50
|
99
|
|
|
|
|
241
|
my @results; |
51
|
99
|
|
|
|
|
262
|
my @full_results = (); |
52
|
99
|
|
|
|
|
411
|
our $errors = ''; |
53
|
|
|
|
|
|
|
try { |
54
|
99
|
|
|
99
|
|
4597
|
prepare_files $meta; |
55
|
9
|
|
|
|
|
33
|
chomp $errors; |
56
|
|
|
|
|
|
|
|
57
|
9
|
100
|
|
|
|
27
|
my ($files, $generator, $runner, $judge, $testcnt) = map { $meta->{$_} or die "Required parameter missing: $_\n"} qw/files generator runner judge testcnt/; |
|
39
|
|
|
|
|
168
|
|
58
|
|
|
|
|
|
|
|
59
|
6
|
|
|
|
|
30
|
$generator = safe_can Generator => generate => $generator; |
60
|
6
|
|
|
|
|
27
|
$runner = safe_can Runner => run => $runner; |
61
|
0
|
|
|
|
|
0
|
$judge = safe_can Judge => judge => $judge; |
62
|
|
|
|
|
|
|
|
63
|
0
|
|
|
|
|
0
|
for my $test (1 .. $testcnt) { |
64
|
0
|
|
|
|
|
0
|
my $start_time = time; |
65
|
0
|
|
|
|
|
0
|
my $result; |
66
|
0
|
|
|
|
|
0
|
unlink 'result'; |
67
|
|
|
|
|
|
|
try { |
68
|
0
|
|
|
|
|
0
|
$generator->($test, $meta); |
69
|
0
|
|
|
|
|
0
|
$result = $runner->($test, $meta); |
70
|
|
|
|
|
|
|
} catch { |
71
|
0
|
|
|
|
|
0
|
$result = $_; |
72
|
0
|
0
|
|
|
|
0
|
unless (ref $result) { |
73
|
0
|
|
|
|
|
0
|
chomp $result; |
74
|
0
|
|
|
|
|
0
|
$result = [ERR, $result]; |
75
|
|
|
|
|
|
|
} |
76
|
0
|
|
|
|
|
0
|
}; |
77
|
|
|
|
|
|
|
|
78
|
0
|
0
|
|
|
|
0
|
if (ref $result) { |
79
|
0
|
|
|
|
|
0
|
my $result_text = $result->[1]; |
80
|
0
|
0
|
|
|
|
0
|
$result_text .= ': ' . read_file 'result' if -s 'result'; |
81
|
0
|
|
|
|
|
0
|
get_logger->trace("Test $test result is " . $result_text); |
82
|
0
|
|
|
|
|
0
|
push @full_results, {id => $test, result => $result->[0], result_text => $result_text, time => time - $start_time} |
83
|
|
|
|
|
|
|
} else { |
84
|
0
|
|
|
|
|
0
|
get_logger->trace("Test $test result is $result"); |
85
|
0
|
|
|
|
|
0
|
push @full_results, {id => $test, result => 0, result_text => $result, time => time - $start_time} |
86
|
|
|
|
|
|
|
} |
87
|
0
|
|
|
|
|
0
|
push @results, $result; |
88
|
0
|
0
|
0
|
|
|
0
|
last if $meta->{judge} eq 'Absolute' && ref $result |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
|
91
|
0
|
|
|
|
|
0
|
my %results = $judge->(@results); |
92
|
0
|
|
|
|
|
0
|
$meta->{$_} = $results{$_} for keys %results; |
93
|
|
|
|
|
|
|
$meta->{results} = \@full_results |
94
|
0
|
|
|
|
|
0
|
} catch { |
95
|
87
|
|
|
87
|
|
66070
|
s/(.*) at .*/$1/s; |
96
|
87
|
|
|
|
|
358
|
chomp; |
97
|
87
|
|
|
|
|
624
|
$meta->{result} = -1; |
98
|
87
|
|
|
|
|
962
|
$meta->{result_text} = $_; |
99
|
99
|
|
|
|
|
1581
|
}; |
100
|
87
|
|
|
|
|
4350
|
stopvms; |
101
|
87
|
|
|
|
|
487
|
$meta->{errors} = $errors; |
102
|
|
|
|
|
|
|
|
103
|
87
|
|
|
|
|
1069
|
get_logger->info('Job result: ' . $meta->{result_text}); |
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
sub process_job { |
107
|
0
|
|
|
0
|
0
|
|
my ($job) = @_; |
108
|
0
|
|
|
|
|
|
my $pb = db()->select('problems', '*', {id => $job->{problem}})->hash; |
109
|
|
|
|
|
|
|
my $meta = { |
110
|
|
|
|
|
|
|
problem => $job->{problem}, |
111
|
|
|
|
|
|
|
files => { |
112
|
|
|
|
|
|
|
prog => { |
113
|
|
|
|
|
|
|
name => 'prog.' . $job->{extension}, |
114
|
|
|
|
|
|
|
format => $job->{format}, |
115
|
|
|
|
|
|
|
content => $job->{source}, |
116
|
|
|
|
|
|
|
}, |
117
|
|
|
|
|
|
|
}, |
118
|
0
|
|
|
|
|
|
map { $_ => $pb->{$_} } qw/generator runner judge precnt testcnt timeout olimit/ |
|
0
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
}; |
120
|
0
|
|
|
|
|
|
my $timeout_override = db()->query('SELECT timeout FROM limits WHERE problem=? AND format=?', $job->{problem}, $job->{format})->list; |
121
|
0
|
0
|
|
|
|
|
$meta->{timeout} = $timeout_override if defined $timeout_override; |
122
|
0
|
0
|
|
|
|
|
$meta->{tests} = decode_json $pb->{tests} if $meta->{runner} eq 'File'; |
123
|
0
|
|
0
|
|
|
|
$job->{contest} &&= contest_entry($job->{contest}); |
124
|
0
|
0
|
0
|
|
|
|
delete $meta->{precnt} unless $job->{contest} && $job->{contest}{started} && !$job->{contest}{finished}; ## no critic (ProhibitNegativeExpressionsInUnlessAndUntilConditions) |
|
|
|
0
|
|
|
|
|
125
|
0
|
0
|
|
|
|
|
$meta->{testcnt} = $meta->{precnt} if $meta->{precnt}; |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
$meta->{files}{ver} = { |
128
|
|
|
|
|
|
|
name => 'ver.' . FORMAT_EXTENSION->{$pb->{verformat}}, |
129
|
|
|
|
|
|
|
format => $pb->{verformat}, |
130
|
|
|
|
|
|
|
content => $pb->{versource}, |
131
|
0
|
0
|
|
|
|
|
} if $pb->{verformat}; |
132
|
|
|
|
|
|
|
|
133
|
0
|
|
|
|
|
|
process $meta; |
134
|
|
|
|
|
|
|
|
135
|
0
|
0
|
|
|
|
|
$meta->{result_text} .= ' (pretests)' if $meta->{precnt}; |
136
|
|
|
|
|
|
|
finish_job($job, $job->{private} || $pb->{private}, |
137
|
|
|
|
|
|
|
result => $meta->{result}, |
138
|
|
|
|
|
|
|
result_text => $meta->{result_text}, |
139
|
|
|
|
|
|
|
($meta->{results} ? (results => encode_json $meta->{results}) : ()), |
140
|
0
|
0
|
0
|
|
|
|
$meta->{errors} ? (errors => $meta->{errors}) : ()); |
|
|
0
|
|
|
|
|
|
141
|
|
|
|
|
|
|
} |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
sub got_job{ |
144
|
0
|
|
|
0
|
0
|
|
my $job = $_[0]; |
145
|
0
|
|
|
|
|
|
my $id = $job->{id}; |
146
|
0
|
|
|
|
|
|
get_logger->debug("Processing job $id..."); |
147
|
0
|
|
|
|
|
|
process_job $job; |
148
|
0
|
|
|
|
|
|
get_logger->debug("Job $id done"); |
149
|
|
|
|
|
|
|
} |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
my $daemon = hostname . ":$$"; |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
sub run{ |
154
|
0
|
|
|
0
|
0
|
|
require Gruntmaster::Data; |
155
|
0
|
|
|
|
|
|
Gruntmaster::Data->import; |
156
|
0
|
|
0
|
|
|
|
dbinit($ENV{GRUNTMASTER_DSN} // 'dbi:Pg:'); |
157
|
0
|
|
|
|
|
|
Log::Log4perl->init('/etc/gruntmasterd/gruntmasterd-log.conf'); |
158
|
0
|
|
|
|
|
|
get_logger->info("gruntmasterd $VERSION started ($daemon)"); |
159
|
0
|
|
|
|
|
|
my $dir = tempdir 'gruntmasterd.XXXX', CLEANUP => 1, TMPDIR => 1; |
160
|
0
|
|
|
|
|
|
chmod 0711, $dir; |
161
|
0
|
|
|
|
|
|
chdir $dir; |
162
|
0
|
|
|
|
|
|
while (1) { |
163
|
0
|
|
|
|
|
|
my $job = take_job($daemon); |
164
|
0
|
0
|
|
|
|
|
got_job $job if defined $job; |
165
|
0
|
0
|
|
|
|
|
sleep 2 unless defined $job; |
166
|
|
|
|
|
|
|
} |
167
|
|
|
|
|
|
|
} |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
1; |
170
|
|
|
|
|
|
|
__END__ |