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