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