]> iEval git - gruntmaster-daemon.git/blob - lib/Gruntmaster/Daemon.pm
Make perlcritic happy
[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_004';
8
9 use Gruntmaster::Daemon::Constants qw/ERR/;
10 use Gruntmaster::Daemon::Format qw/prepare_files 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 Time::HiRes qw/time/;
17 use Try::Tiny;
18 use Log::Log4perl qw/get_logger/;
19
20 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 };
35
36 ##################################################
37
38 sub safe_can {
39 my ($type, $sub, $name) = @_;
40
41 get_logger->logdie("Invalid \l$type: '$name'") unless $name =~ /^\w+$/s;
42 my $pkg = __PACKAGE__ . "::${type}::${name}";
43 eval "require $pkg" or get_logger->warn("Error while requiring $pkg: $@");
44 $pkg->can($sub) or get_logger->logdie("No such \l$type: '$name'");
45 }
46
47 sub process{
48 my ($meta) = @_;
49
50 my @results;
51 my @full_results = ();
52 our $errors = '';
53 try {
54 prepare_files $meta;
55 chomp $errors;
56
57 my ($files, $generator, $runner, $judge, $testcnt) = map { $meta->{$_} or die "Required parameter missing: $_\n"} qw/files generator runner judge testcnt/;
58
59 $generator = safe_can Generator => generate => $generator;
60 $runner = safe_can Runner => run => $runner;
61 $judge = safe_can Judge => judge => $judge;
62
63 for my $test (1 .. $testcnt) {
64 my $start_time = time;
65 my $result;
66 unlink 'result';
67 try {
68 $generator->($test, $meta);
69 $result = $runner->($test, $meta);
70 } catch {
71 $result = $_;
72 unless (ref $result) {
73 chomp $result;
74 $result = [ERR, $result];
75 }
76 };
77
78 if (ref $result) {
79 my $result_text = $result->[1];
80 $result_text .= ': ' . read_file 'result' if -s 'result';
81 get_logger->trace("Test $test result is " . $result_text);
82 push @full_results, {id => $test, result => $result->[0], result_text => $result_text, time => time - $start_time}
83 } else {
84 get_logger->trace("Test $test result is $result");
85 push @full_results, {id => $test, result => 0, result_text => $result, time => time - $start_time}
86 }
87 push @results, $result;
88 last if $meta->{judge} eq 'Absolute' && ref $result
89 }
90
91 my %results = $judge->(@results);
92 $meta->{$_} = $results{$_} for keys %results;
93 $meta->{results} = \@full_results
94 } catch {
95 s/(.*) at .*/$1/s;
96 chomp;
97 $meta->{result} = -1;
98 $meta->{result_text} = $_;
99 };
100 stopvms;
101 $meta->{errors} = $errors;
102
103 get_logger->info('Job result: ' . $meta->{result_text});
104 }
105
106 sub process_job {
107 my ($job) = @_;
108 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 map { $_ => $pb->{$_} } qw/generator runner judge precnt testcnt timeout olimit/
119 };
120 my $timeout_override = db()->query('SELECT timeout FROM limits WHERE problem=? AND format=?', $job->{problem}, $job->{format})->list;
121 $meta->{timeout} = $timeout_override if defined $timeout_override;
122 $meta->{tests} = decode_json $pb->{tests} if $meta->{runner} eq 'File';
123 $job->{contest} &&= contest_entry($job->{contest});
124 delete $meta->{precnt} unless $job->{contest} && $job->{contest}{started} && !$job->{contest}{finished}; ## no critic (ProhibitNegativeExpressionsInUnlessAndUntilConditions)
125 $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 } if $pb->{verformat};
132
133 process $meta;
134
135 $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 $meta->{errors} ? (errors => $meta->{errors}) : ());
141 }
142
143 sub got_job{
144 my $job = $_[0];
145 my $id = $job->{id};
146 get_logger->debug("Processing job $id...");
147 process_job $job;
148 get_logger->debug("Job $id done");
149 }
150
151 my $daemon = hostname . ":$$";
152
153 sub run{
154 require Gruntmaster::Data;
155 Gruntmaster::Data->import;
156 dbinit($ENV{GRUNTMASTER_DSN} // 'dbi:Pg:');
157 Log::Log4perl->init('/etc/gruntmasterd/gruntmasterd-log.conf');
158 get_logger->info("gruntmasterd $VERSION started ($daemon)");
159 my $dir = tempdir 'gruntmasterd.XXXX', CLEANUP => 1, TMPDIR => 1;
160 chmod 0711, $dir;
161 chdir $dir;
162 while (1) {
163 my $job = take_job($daemon);
164 got_job $job if defined $job;
165 sleep 2 unless defined $job;
166 }
167 }
168
169 1;
170 __END__
171
172 =head1 NAME
173
174 Gruntmaster::Daemon - Gruntmaster 6000 Online Judge -- daemon
175
176 =head1 SYNOPSIS
177
178 use Gruntmaster::Daemon;
179 Gruntmaster::Daemon->run;
180
181 =head1 DESCRIPTION
182
183 Gruntmaster::Daemon is the daemon component of the Gruntmaster 6000 online judge.
184
185 =head1 AUTHOR
186
187 Marius Gavrilescu E<lt>marius@ieval.roE<gt>
188
189 =head1 COPYRIGHT AND LICENSE
190
191 Copyright (C) 2014 by Marius Gavrilescu
192
193 This library is free software: you can redistribute it and/or modify
194 it under the terms of the GNU Affero General Public License as published by
195 the Free Software Foundation, either version 3 of the License, or
196 (at your option) any later version.
197
198
199 =cut
This page took 0.068955 seconds and 4 git commands to generate.