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