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