]> iEval git - gruntmaster-daemon.git/blame - lib/Gruntmaster/Daemon.pm
Make perlcritic happy
[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',
f8b954a9 25 GOLFSCRIPT => 'gs',
5a4392d5 26 HASKELL => 'hs',
6bf57d23
MG
27 MONO => 'cs',
28 JAVA => 'java',
29 PASCAL => 'pas',
30 PERL => 'pl',
31 PYTHON => 'py',
c4d90e6f 32 RUBY => 'rb',
c88da74d 33 SBCL => 'l',
6bf57d23 34};
adb44605 35
5c5cd38a
MG
36##################################################
37
b4ac22ff 38sub safe_can {
a722431b 39 my ($type, $sub, $name) = @_;
5c5cd38a 40
fcfcfb93 41 get_logger->logdie("Invalid \l$type: '$name'") unless $name =~ /^\w+$/s;
a722431b
MG
42 my $pkg = __PACKAGE__ . "::${type}::${name}";
43 eval "require $pkg" or get_logger->warn("Error while requiring $pkg: $@");
b4ac22ff 44 $pkg->can($sub) or get_logger->logdie("No such \l$type: '$name'");
5c5cd38a
MG
45}
46
ab436d78 47sub process{
867a9dad 48 my ($meta) = @_;
a722431b
MG
49
50 my @results;
51 my @full_results = ();
a722431b
MG
52 our $errors = '';
53 try {
a722431b
MG
54 prepare_files $meta;
55 chomp $errors;
56
99d37110 57 my ($files, $generator, $runner, $judge, $testcnt) = map { $meta->{$_} or die "Required parameter missing: $_\n"} qw/files generator runner judge testcnt/;
a722431b
MG
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;
ac964998 66 unlink 'result';
a722431b
MG
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) {
ac964998
MG
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}
a722431b
MG
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
5c5cd38a 89 }
5c5cd38a 90
a722431b
MG
91 my %results = $judge->(@results);
92 $meta->{$_} = $results{$_} for keys %results;
7be5f054 93 $meta->{results} = \@full_results
a722431b 94 } catch {
99d37110 95 s/(.*) at .*/$1/s;
a722431b
MG
96 chomp;
97 $meta->{result} = -1;
98 $meta->{result_text} = $_;
99 };
fdb99417 100 stopvms;
867a9dad 101 $meta->{errors} = $errors;
a722431b 102
99d37110 103 get_logger->info('Job result: ' . $meta->{result_text});
867a9dad
MG
104}
105
106sub process_job {
107 my ($job) = @_;
fb7e1b7c 108 my $pb = db()->select('problems', '*', {id => $job->{problem}})->hash;
867a9dad 109 my $meta = {
6101c7ad 110 problem => $job->{problem},
867a9dad
MG
111 files => {
112 prog => {
6101c7ad
MG
113 name => 'prog.' . $job->{extension},
114 format => $job->{format},
115 content => $job->{source},
867a9dad
MG
116 },
117 },
6101c7ad 118 map { $_ => $pb->{$_} } qw/generator runner judge precnt testcnt timeout olimit/
867a9dad 119 };
6101c7ad
MG
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});
7e81b08a 124 delete $meta->{precnt} unless $job->{contest} && $job->{contest}{started} && !$job->{contest}{finished}; ## no critic (ProhibitNegativeExpressionsInUnlessAndUntilConditions)
da822e3c 125 $meta->{testcnt} = $meta->{precnt} if $meta->{precnt};
867a9dad
MG
126
127 $meta->{files}{ver} = {
6101c7ad
MG
128 name => 'ver.' . FORMAT_EXTENSION->{$pb->{verformat}},
129 format => $pb->{verformat},
130 content => $pb->{versource},
131 } if $pb->{verformat};
867a9dad
MG
132
133 process $meta;
134
da822e3c 135 $meta->{result_text} .= ' (pretests)' if $meta->{precnt};
6101c7ad
MG
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}) : ());
ab436d78
MG
141}
142
143sub got_job{
be3e5b32 144 my $job = $_[0];
6101c7ad
MG
145 my $id = $job->{id};
146 get_logger->debug("Processing job $id...");
147 process_job $job;
148 get_logger->debug("Job $id done");
5c5cd38a
MG
149}
150
6101c7ad
MG
151my $daemon = hostname . ":$$";
152
5c5cd38a 153sub run{
55e61ae8 154 require Gruntmaster::Data;
6101c7ad 155 Gruntmaster::Data->import;
074cbf76 156 dbinit($ENV{GRUNTMASTER_DSN} // 'dbi:Pg:');
a722431b 157 Log::Log4perl->init('/etc/gruntmasterd/gruntmasterd-log.conf');
6101c7ad 158 get_logger->info("gruntmasterd $VERSION started ($daemon)");
01d59515
MG
159 my $dir = tempdir 'gruntmasterd.XXXX', CLEANUP => 1, TMPDIR => 1;
160 chmod 0711, $dir;
161 chdir $dir;
be3e5b32 162 while (1) {
6101c7ad 163 my $job = take_job($daemon);
be3e5b32
MG
164 got_job $job if defined $job;
165 sleep 2 unless defined $job;
166 }
5c5cd38a
MG
167}
168
1691;
170__END__
5c5cd38a
MG
171
172=head1 NAME
173
bc372959 174Gruntmaster::Daemon - Gruntmaster 6000 Online Judge -- daemon
5c5cd38a
MG
175
176=head1 SYNOPSIS
177
178 use Gruntmaster::Daemon;
bc372959 179 Gruntmaster::Daemon->run;
5c5cd38a
MG
180
181=head1 DESCRIPTION
182
bc372959 183Gruntmaster::Daemon is the daemon component of the Gruntmaster 6000 online judge.
5c5cd38a
MG
184
185=head1 AUTHOR
186
bc372959 187Marius Gavrilescu E<lt>marius@ieval.roE<gt>
5c5cd38a
MG
188
189=head1 COPYRIGHT AND LICENSE
190
bc372959 191Copyright (C) 2014 by Marius Gavrilescu
5c5cd38a 192
bc372959
MG
193This library is free software: you can redistribute it and/or modify
194it under the terms of the GNU Affero General Public License as published by
195the Free Software Foundation, either version 3 of the License, or
196(at your option) any later version.
5c5cd38a
MG
197
198
199=cut
This page took 0.088635 seconds and 4 git commands to generate.