Add PHP
[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
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 BRAINFUCK => 'bf',
22 C => 'c',
23 CPP => 'cpp',
24 GCCGO => 'go',
25 GOLANG => 'go',
26 GOLFSCRIPT => 'gs',
27 HASKELL => 'hs',
28 JAVA => 'java',
29 JULIA => 'jl',
30 MONO => 'cs',
31 PASCAL => 'pas',
32 PERL => 'pl',
33 PHP => 'php',
34 PYTHON => 'py',
35 RUBY => 'rb',
36 RUST => 'rs',
37 SBCL => 'l',
38 };
39
40 ##################################################
41
42 sub safe_can {
43 my ($type, $sub, $name) = @_;
44
45 get_logger->logdie("Invalid \l$type: '$name'") unless $name =~ /^\w+$/s;
46 my $pkg = __PACKAGE__ . "::${type}::${name}";
47 eval "require $pkg" or get_logger->warn("Error while requiring $pkg: $@");
48 $pkg->can($sub) or get_logger->logdie("No such \l$type: '$name'");
49 }
50
51 sub process{
52 my ($meta) = @_;
53
54 my @results;
55 my @full_results = ();
56 our $errors = '';
57 try {
58 prepare_files $meta;
59 chomp $errors;
60
61 my ($files, $generator, $runner, $judge, $testcnt) = map { $meta->{$_} or die "Required parameter missing: $_\n"} qw/files generator runner judge testcnt/;
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;
70 unlink 'result';
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) {
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}
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
93 }
94
95 my %results = $judge->(@results);
96 $meta->{$_} = $results{$_} for keys %results;
97 $meta->{results} = \@full_results
98 } catch {
99 s/(.*) at .*/$1/s;
100 chomp;
101 $meta->{result} = -1;
102 $meta->{result_text} = $_;
103 };
104 stopvms;
105 $meta->{errors} = $errors;
106
107 get_logger->info('Job result: ' . $meta->{result_text});
108 }
109
110 sub process_job {
111 my ($job) = @_;
112 my $pb = db()->select('problems', '*', {id => $job->{problem}})->hash;
113 my $meta = {
114 problem => $job->{problem},
115 files => {
116 prog => {
117 name => 'prog.' . $job->{extension},
118 format => $job->{format},
119 content => $job->{source},
120 },
121 },
122 map { $_ => $pb->{$_} } qw/generator runner judge precnt testcnt timeout olimit/
123 };
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});
128 delete $meta->{precnt} unless $job->{contest} && $job->{contest}{started} && !$job->{contest}{finished}; ## no critic (ProhibitNegativeExpressionsInUnlessAndUntilConditions)
129 $meta->{testcnt} = $meta->{precnt} if $meta->{precnt};
130
131 $meta->{files}{ver} = {
132 name => 'ver.' . FORMAT_EXTENSION->{$pb->{verformat}},
133 format => $pb->{verformat},
134 content => $pb->{versource},
135 } if $pb->{verformat};
136
137 process $meta;
138
139 $meta->{result_text} .= ' (pretests)' if $meta->{precnt};
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}) : ());
145 }
146
147 sub got_job{
148 my $job = $_[0];
149 my $id = $job->{id};
150 get_logger->debug("Processing job $id...");
151 process_job $job;
152 get_logger->debug("Job $id done");
153 }
154
155 my $daemon = hostname . ":$$";
156
157 sub run{
158 require Gruntmaster::Data;
159 Gruntmaster::Data->import;
160 dbinit($ENV{GRUNTMASTER_DSN} // 'dbi:Pg:');
161 Log::Log4perl->init('/etc/gruntmasterd/gruntmasterd-log.conf');
162 get_logger->info("gruntmasterd $VERSION started ($daemon)");
163 my $dir = tempdir 'gruntmasterd.XXXX', CLEANUP => 1, TMPDIR => 1;
164 chmod 0711, $dir;
165 chdir $dir;
166 while (1) {
167 my $job = take_job($daemon);
168 got_job $job if defined $job;
169 sleep 2 unless defined $job;
170 }
171 }
172
173 1;
174 __END__
175
176 =head1 NAME
177
178 Gruntmaster::Daemon - Gruntmaster 6000 Online Judge -- daemon
179
180 =head1 SYNOPSIS
181
182 use Gruntmaster::Daemon;
183 Gruntmaster::Daemon->run;
184
185 =head1 DESCRIPTION
186
187 Gruntmaster::Daemon is the daemon component of the Gruntmaster 6000 online judge.
188
189 =head1 AUTHOR
190
191 Marius Gavrilescu E<lt>marius@ieval.roE<gt>
192
193 =head1 COPYRIGHT AND LICENSE
194
195 Copyright (C) 2014 by Marius Gavrilescu
196
197 This library is free software: you can redistribute it and/or modify
198 it under the terms of the GNU Affero General Public License as published by
199 the Free Software Foundation, either version 3 of the License, or
200 (at your option) any later version.
201
202
203 =cut
This page took 0.070049 seconds and 4 git commands to generate.