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