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