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