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