]> iEval git - gruntmaster-daemon.git/blame - lib/Gruntmaster/Daemon.pm
Increase time limit of increment test problem
[gruntmaster-daemon.git] / lib / Gruntmaster / Daemon.pm
CommitLineData
5c5cd38a
MG
1package Gruntmaster::Daemon;
2
3use 5.014000;
4use strict;
5use warnings;
6
3e7fd903 7our $VERSION = '5999.000_004';
5c5cd38a
MG
8
9use Gruntmaster::Daemon::Constants qw/ERR/;
fdb99417 10use Gruntmaster::Daemon::Format qw/prepare_files stopvms/;
5c5cd38a 11
ab436d78 12use File::Basename qw/fileparse/;
ab436d78 13use File::Temp qw/tempdir/;
be3e5b32 14use JSON qw/decode_json encode_json/;
ab436d78 15use Sys::Hostname qw/hostname/;
5c5cd38a
MG
16use Time::HiRes qw/time/;
17use Try::Tiny;
5c5cd38a
MG
18use Log::Log4perl qw/get_logger/;
19
adb44605 20use constant PAGE_SIZE => 10;
6bf57d23
MG
21use constant FORMAT_EXTENSION => {
22 C => 'c',
23 CPP => 'cpp',
5a4392d5
MG
24 GCCGO => 'go',
25 GOLANG => 'go',
26 HASKELL => 'hs',
6bf57d23
MG
27 MONO => 'cs',
28 JAVA => 'java',
29 PASCAL => 'pas',
30 PERL => 'pl',
31 PYTHON => 'py',
32};
adb44605 33
5c5cd38a
MG
34##################################################
35
55e61ae8
MG
36sub purge;
37
7be5f054 38my $db;
064332cd 39
5c5cd38a 40sub safe_can_nodie {
a722431b 41 my ($type, $sub, $name) = @_;
5c5cd38a 42
a722431b
MG
43 return unless $name =~ /^\w+$/;
44 no strict 'refs';
45 my $pkg = __PACKAGE__ . "::${type}::${name}";
46 eval "require $pkg" or get_logger->warn("Error while requiring $pkg: $@");
47 $pkg->can($sub);
5c5cd38a
MG
48}
49
50sub safe_can {
a722431b 51 my ($type, $sub, $name) = @_;
5c5cd38a 52
a722431b 53 safe_can_nodie @_ or get_logger->logdie("No such \l$type: '$name'");
5c5cd38a
MG
54}
55
ab436d78 56sub process{
be3e5b32 57 my ($job, $jobr) = @_;
a722431b
MG
58
59 my @results;
60 my @full_results = ();
be3e5b32 61 my $meta;
a722431b
MG
62 our $errors = '';
63 try {
7be5f054
MG
64 if (ref $job) {
65 $meta = $job;
66 } else {
67 $meta = {
68 problem => $jobr->problem->id,
69 files => {
70 prog => {
71 name => 'prog.' . $jobr->extension,
72 format => $jobr->format,
73 content => $jobr->source,
74 },
be3e5b32 75 },
7be5f054
MG
76 map { $_ => $jobr->problem->get_column($_) } qw/generator runner judge testcnt timeout olimit/
77 };
78 $meta->{tests} = decode_json $jobr->problem->tests if $meta->{runner} eq 'File';
be3e5b32 79
7be5f054
MG
80 $meta->{files}{ver} = {
81 name => 'ver.' . FORMAT_EXTENSION->{$jobr->problem->verformat},
82 format => $jobr->problem->verformat,
83 content => $jobr->problem->versource,
84 } if $jobr->problem->verformat;
85 }
5c5cd38a 86
a722431b
MG
87 prepare_files $meta;
88 chomp $errors;
89
90 my ($files, $generator, $runner, $judge, $testcnt) = map { $meta->{$_} or die "Required parameter missing: $_"} qw/files generator runner judge testcnt/;
91
92 $generator = safe_can Generator => generate => $generator;
93 $runner = safe_can Runner => run => $runner;
94 $judge = safe_can Judge => judge => $judge;
95
96 for my $test (1 .. $testcnt) {
97 my $start_time = time;
98 my $result;
99 try {
100 $generator->($test, $meta);
101 $result = $runner->($test, $meta);
102 } catch {
103 $result = $_;
104 unless (ref $result) {
105 chomp $result;
106 $result = [ERR, $result];
107 }
108 };
109
110 if (ref $result) {
111 get_logger->trace("Test $test result is " . $result->[1]);
112 push @full_results, {id => $test, result => $result->[0], result_text => $result->[1], time => time - $start_time}
113 } else {
114 get_logger->trace("Test $test result is $result");
115 push @full_results, {id => $test, result => 0, result_text => $result, time => time - $start_time}
116 }
117 push @results, $result;
118 last if $meta->{judge} eq 'Absolute' && ref $result
5c5cd38a 119 }
5c5cd38a 120
a722431b
MG
121 my %results = $judge->(@results);
122 $meta->{$_} = $results{$_} for keys %results;
7be5f054 123 $meta->{results} = \@full_results
a722431b
MG
124 } catch {
125 s,(.*) at .*,$1,;
126 chomp;
127 $meta->{result} = -1;
128 $meta->{result_text} = $_;
129 };
fdb99417 130 stopvms;
a722431b
MG
131
132 get_logger->info("Job result: " . $meta->{result_text});
7be5f054 133 return unless $jobr;
be3e5b32
MG
134 $jobr->update({
135 result => $meta->{result},
136 result_text => $meta->{result_text},
6555bf80 137 ($meta->{results} ? (results => encode_json $meta->{results}) : ()),
be3e5b32
MG
138 $errors ? (errors => $errors) : ()
139 });
140
b335c4bb
MG
141 my $solved = $meta->{result} ? 0 : 1;
142 my $status = $db->problem_statuses->find($jobr->owner, $jobr->problem);
143 $db->problem_statuses->update_or_create({
144 problem => $jobr->problem,
145 owner => $jobr->owner,
146 job => $jobr->id,
147 solved => $solved});
148
a5d85109
MG
149 purge '/log/';
150 purge "/log/$job";
151 purge '/st/' . $jobr->contest if $jobr->contest;
152 purge '/us/';
153 purge '/us/' . $jobr->owner;
ab436d78
MG
154}
155
156sub got_job{
be3e5b32
MG
157 my $job = $_[0];
158 my $id = $job->id;
159 get_logger->debug("Taking job $id...");
160 my $daemon = hostname . ":$$";
161 $job->update({daemon => $daemon});
162 #if (set_job_daemon $job, hostname . ":$$") {
163 if (1) {
164 get_logger->debug("Succesfully taken job $id");
165 process $id, $job;
166 get_logger->debug("Job $id done");
ab436d78 167 } else {
be3e5b32 168 get_logger->debug("Job $id already taken");
ab436d78 169 }
5c5cd38a
MG
170}
171
172sub run{
fdb99417 173 warn "No GRUNTMASTER_VM environment variable. Running without a VM is a security risk.\n" unless $ENV{GRUNTMASTER_VM};
55e61ae8 174 require Gruntmaster::Data;
7be5f054 175 $db = Gruntmaster::Data->connect('dbi:Pg:');
a722431b
MG
176 Log::Log4perl->init('/etc/gruntmasterd/gruntmasterd-log.conf');
177 get_logger->info("gruntmasterd $VERSION started");
178 chdir tempdir 'gruntmasterd.XXXX', CLEANUP => 1, TMPDIR => 1;
be3e5b32
MG
179 while (1) {
180 my $job = $db->jobs->search({daemon => undef}, {rows => 1})->first;
181 got_job $job if defined $job;
182 sleep 2 unless defined $job;
183 }
5c5cd38a
MG
184}
185
1861;
187__END__
5c5cd38a
MG
188
189=head1 NAME
190
bc372959 191Gruntmaster::Daemon - Gruntmaster 6000 Online Judge -- daemon
5c5cd38a
MG
192
193=head1 SYNOPSIS
194
195 use Gruntmaster::Daemon;
bc372959 196 Gruntmaster::Daemon->run;
5c5cd38a
MG
197
198=head1 DESCRIPTION
199
bc372959 200Gruntmaster::Daemon is the daemon component of the Gruntmaster 6000 online judge.
5c5cd38a
MG
201
202=head1 AUTHOR
203
bc372959 204Marius Gavrilescu E<lt>marius@ieval.roE<gt>
5c5cd38a
MG
205
206=head1 COPYRIGHT AND LICENSE
207
bc372959 208Copyright (C) 2014 by Marius Gavrilescu
5c5cd38a 209
bc372959
MG
210This library is free software: you can redistribute it and/or modify
211it under the terms of the GNU Affero General Public License as published by
212the Free Software Foundation, either version 3 of the License, or
213(at your option) any later version.
5c5cd38a
MG
214
215
216=cut
This page took 0.072142 seconds and 4 git commands to generate.