]> iEval git - gruntmaster-daemon.git/blob - lib/Gruntmaster/Daemon.pm
Only use pretests when contests are running
[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_004';
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 SBCL => 'l',
34 };
35
36 ##################################################
37
38 my $db;
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 $meta = {
111 problem => $job->problem->id,
112 files => {
113 prog => {
114 name => 'prog.' . $job->extension,
115 format => $job->format,
116 content => $job->source,
117 },
118 },
119 map { $_ => $job->problem->get_column($_) } qw/generator runner judge precnt testcnt timeout olimit/
120 };
121 my $limit_override = $job->result_source->schema->limits->find($meta->{problem}, $job->format);
122 $meta->{timeout} = $limit_override->timeout if $limit_override;
123 $meta->{tests} = decode_json $job->problem->tests if $meta->{runner} eq 'File';
124 delete $meta->{precnt} unless $job->contest && $job->contest->is_running;
125 $meta->{testcnt} = $meta->{precnt} if $meta->{precnt};
126
127 $meta->{files}{ver} = {
128 name => 'ver.' . FORMAT_EXTENSION->{$job->problem->verformat},
129 format => $job->problem->verformat,
130 content => $job->problem->versource,
131 } if $job->problem->verformat;
132
133 process $meta;
134
135 $meta->{result_text} .= ' (pretests)' if $meta->{precnt};
136 $job->update({
137 result => $meta->{result},
138 result_text => $meta->{result_text},
139 ($meta->{results} ? (results => encode_json $meta->{results}) : ()),
140 $meta->{errors} ? (errors => $meta->{errors}) : ()
141 });
142
143 my $solved = $meta->{result} ? 0 : 1;
144 my $status = $db->problem_statuses->find($job->owner, $job->problem);
145 $db->problem_statuses->update_or_create({
146 problem => $job->problem,
147 owner => $job->owner,
148 job => $job->id,
149 solved => $solved}) if !$job->problem->private;
150 }
151
152 sub got_job{
153 my $job = $_[0];
154 my $id = $job->id;
155 get_logger->debug("Taking job $id...");
156 my $daemon = hostname . ":$$";
157 $job->update({daemon => $daemon}, {where => {daemon => undef}})->discard_changes;
158 if ($job->daemon eq $daemon) {
159 get_logger->debug("Succesfully taken job $id");
160 process_job $job;
161 get_logger->debug("Job $id done");
162 } else {
163 get_logger->debug("Job $id already taken by " . $job->daemon);
164 }
165 }
166
167 sub run{
168 require Gruntmaster::Data;
169 $db = Gruntmaster::Data->connect($ENV{GRUNTMASTER_DSN} // 'dbi:Pg:');
170 Log::Log4perl->init('/etc/gruntmasterd/gruntmasterd-log.conf');
171 get_logger->info("gruntmasterd $VERSION started");
172 my $dir = tempdir 'gruntmasterd.XXXX', CLEANUP => 1, TMPDIR => 1;
173 chmod 0711, $dir;
174 chdir $dir;
175 while (1) {
176 my $job = $db->jobs->search({daemon => undef}, {rows => 1})->first;
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.072823 seconds and 5 git commands to generate.