]> iEval git - gruntmaster-daemon.git/blob - lib/Gruntmaster/Daemon.pm
ef24364184c6ef403ae898acee7d2cf7046c2af3
[gruntmaster-daemon.git] / lib / Gruntmaster / Daemon.pm
1 package Gruntmaster::Daemon;
2
3 use 5.014000;
4 use strict;
5 use warnings;
6
7 our $VERSION = '5999.000_005';
8
9 use Gruntmaster::Daemon::Constants qw/ERR/;
10 use Gruntmaster::Daemon::Format qw/prepare_files stopvms/;
11 use Gruntmaster::SendResults qw/send_results_request/;
12
13 use File::Slurp qw/read_file/;
14 use File::Temp qw/tempdir/;
15 use JSON qw/decode_json encode_json/;
16 use Sys::Hostname qw/hostname/;
17 use Try::Tiny;
18 use Log::Log4perl qw/get_logger/;
19
20 use constant FORMAT_EXTENSION => {
21 BRAINFUCK => 'bf',
22 C => 'c',
23 CPP => 'cpp',
24 D => 'd',
25 GCCGO => 'go',
26 GOLANG => 'go',
27 GOLFSCRIPT => 'gs',
28 HASKELL => 'hs',
29 JAVA => 'java',
30 JULIA => 'jl',
31 MONO => 'cs',
32 OBERON => 'm',
33 OCAML => 'ml',
34 PASCAL => 'pas',
35 PERL => 'pl',
36 PHP => 'php',
37 PYTHON => 'py',
38 PYTHON3 => 'py',
39 RUBY => 'rb',
40 RUST => 'rs',
41 SBCL => 'l',
42 };
43
44 ##################################################
45
46 sub safe_can {
47 my ($type, $sub, $name) = @_;
48
49 get_logger->logdie("Invalid \l$type: '$name'") unless $name =~ /^\w+$/s;
50 my $pkg = __PACKAGE__ . "::${type}::${name}";
51 eval "require $pkg" or get_logger->warn("Error while requiring $pkg: $@");
52 $pkg->can($sub) or get_logger->logdie("No such \l$type: '$name'");
53 }
54
55 sub process{
56 my ($meta) = @_;
57
58 my @results;
59 my @full_results = ();
60 our $errors = '';
61 try {
62 prepare_files $meta;
63 chomp $errors;
64
65 my ($files, $generator, $runner, $judge, $testcnt) = map { $meta->{$_} or die "Required parameter missing: $_\n"} qw/files generator runner judge testcnt/;
66
67 $generator = safe_can Generator => generate => $generator;
68 $runner = safe_can Runner => run => $runner;
69 $judge = safe_can Judge => judge => $judge;
70
71 for my $test (1 .. $testcnt) {
72 my $result;
73 unlink 'result';
74 try {
75 $generator->($test, $meta);
76 $result = $runner->($test, $meta);
77 } catch {
78 $result = $_;
79 unless (ref $result) {
80 chomp $result;
81 $result = [ERR, $result];
82 }
83 };
84
85 my $time = '?';
86 $time = (read_file 'time-stop-prog') - (read_file 'time-start-prog') if -f 'time-stop-prog' && -f 'time-start-prog';
87 unlink 'time-start-prog', 'time-stop-prog';
88 if (ref $result) {
89 my $result_text = $result->[1];
90 $result_text .= ': ' . read_file 'result' if -s 'result';
91 get_logger->trace("Test $test result is " . $result_text);
92 push @full_results, {id => $test, result => $result->[0], result_text => $result_text, time => $time}
93 } else {
94 get_logger->trace("Test $test result is $result");
95 push @full_results, {id => $test, result => 0, result_text => $result, time => $time}
96 }
97 push @results, $result;
98 last if $meta->{judge} eq 'Absolute' && ref $result
99 }
100
101 my %results = $judge->(@results);
102 $meta->{$_} = $results{$_} for keys %results;
103 $meta->{results} = \@full_results
104 } catch {
105 s/(.*) at .*/$1/s;
106 chomp;
107 $meta->{result} = -1;
108 $meta->{result_text} = $_;
109 };
110 stopvms;
111 $meta->{errors} = $errors;
112
113 get_logger->info('Job result: ' . $meta->{result_text});
114 send_results_request($meta->{job_id}, $meta->{result}, $meta->{result_text});
115 }
116
117 sub process_job {
118 my ($job) = @_;
119 my $pb = db()->select('problems', '*', {id => $job->{problem}})->hash;
120 my $meta = {
121 problem => $job->{problem},
122 files => {
123 prog => {
124 name => 'prog.' . $job->{extension},
125 format => $job->{format},
126 content => $job->{source},
127 },
128 },
129 map { $_ => $pb->{$_} } qw/generator runner judge precnt testcnt timeout olimit/
130 };
131 my $timeout_override = db()->query('SELECT timeout FROM limits WHERE problem=? AND format=?', $job->{problem}, $job->{format})->list;
132 $meta->{timeout} = $timeout_override if defined $timeout_override;
133 $meta->{tests} = decode_json $pb->{tests} if $meta->{runner} eq 'File';
134 my $contest = $job->{contest} && contest_entry($job->{contest});
135 delete $meta->{precnt} unless $contest && $contest->{started} && !$contest->{finished}; ## no critic (ProhibitNegativeExpressionsInUnlessAndUntilConditions)
136 $meta->{testcnt} = $meta->{precnt} if $meta->{precnt};
137
138 $meta->{files}{ver} = {
139 name => 'ver.' . FORMAT_EXTENSION->{$pb->{verformat}},
140 format => $pb->{verformat},
141 content => $pb->{versource},
142 } if $pb->{verformat};
143 $meta->{job_id} = $job->{id};
144
145 process $meta;
146
147 $meta->{result_text} .= ' (pretests)' if $meta->{precnt};
148 finish_job($job, $job->{private} || $pb->{private},
149 result => $meta->{result},
150 result_text => $meta->{result_text},
151 ($meta->{results} ? (results => encode_json $meta->{results}) : ()),
152 $meta->{errors} ? (errors => $meta->{errors}) : ());
153 }
154
155 sub got_job{
156 my $job = $_[0];
157 my $id = $job->{id};
158 get_logger->debug("Processing job $id...");
159 process_job $job;
160 get_logger->debug("Job $id done");
161 }
162
163 my $daemon = hostname . ":$$";
164
165 sub run{
166 require Gruntmaster::Data;
167 Gruntmaster::Data->import;
168 dbinit($ENV{GRUNTMASTER_DSN} // 'dbi:Pg:');
169 Log::Log4perl->init('/etc/gruntmasterd/gruntmasterd-log.conf');
170 get_logger->info("gruntmasterd $VERSION started ($daemon)");
171 my $dir = tempdir 'gruntmasterd.XXXX', CLEANUP => 1, TMPDIR => 1;
172 chmod 0711, $dir;
173 chdir $dir;
174 while (1) {
175 my $job = take_job($daemon);
176 got_job $job if defined $job;
177 sleep 2 unless defined $job;
178 }
179 }
180
181 1;
182 __END__
183
184 =head1 NAME
185
186 Gruntmaster::Daemon - Gruntmaster 6000 Online Judge -- daemon
187
188 =head1 SYNOPSIS
189
190 use Gruntmaster::Daemon;
191 Gruntmaster::Daemon->run;
192
193 =head1 DESCRIPTION
194
195 Gruntmaster::Daemon is the daemon component of the Gruntmaster 6000 online judge.
196
197 =head1 AUTHOR
198
199 Marius Gavrilescu E<lt>marius@ieval.roE<gt>
200
201 =head1 COPYRIGHT AND LICENSE
202
203 Copyright (C) 2014 by Marius Gavrilescu
204
205 This library is free software: you can redistribute it and/or modify
206 it under the terms of the GNU Affero General Public License as published by
207 the Free Software Foundation, either version 3 of the License, or
208 (at your option) any later version.
209
210
211 =cut
This page took 0.061341 seconds and 4 git commands to generate.