]> iEval git - gruntmaster-daemon.git/blame - lib/Gruntmaster/Daemon.pm
Add trace to purges
[gruntmaster-daemon.git] / lib / Gruntmaster / Daemon.pm
CommitLineData
5c5cd38a
MG
1package Gruntmaster::Daemon;
2
3use 5.014000;
4use strict;
5use warnings;
6
d6a1ae0d 7our $VERSION = '5999.000_002';
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
MG
22use constant PAGE_SIZE => 10;
23
5c5cd38a
MG
24##################################################
25
be3e5b32 26my $db = Gruntmaster::Data->connect('dbi:Pg:');
064332cd 27my $ua = LWP::UserAgent->new;
82657f57 28my @purge_hosts = exists $ENV{PURGE_HOSTS} ? split ' ', $ENV{PURGE_HOSTS} : ();
064332cd 29
5c5cd38a 30sub safe_can_nodie {
a722431b 31 my ($type, $sub, $name) = @_;
5c5cd38a 32
a722431b
MG
33 return unless $name =~ /^\w+$/;
34 no strict 'refs';
35 my $pkg = __PACKAGE__ . "::${type}::${name}";
36 eval "require $pkg" or get_logger->warn("Error while requiring $pkg: $@");
37 $pkg->can($sub);
5c5cd38a
MG
38}
39
40sub safe_can {
a722431b 41 my ($type, $sub, $name) = @_;
5c5cd38a 42
a722431b 43 safe_can_nodie @_ or get_logger->logdie("No such \l$type: '$name'");
5c5cd38a
MG
44}
45
064332cd 46sub purge {
5672831c 47 get_logger->trace("Purging $_[0]");
82657f57
MG
48 for my $host (@purge_hosts) {
49 my $req = HTTP::Request->new(PURGE => "http://$host$_[0]");
50 $ua->request($req)
51 }
064332cd
MG
52}
53
ab436d78 54sub process{
be3e5b32 55 my ($job, $jobr) = @_;
a722431b
MG
56
57 my @results;
58 my @full_results = ();
be3e5b32 59 my $meta;
a722431b
MG
60 our $errors = '';
61 try {
be3e5b32
MG
62 $meta = {
63 problem => $jobr->problem->id,
64 files => {
65 prog => {
66 name => 'prog.' . $jobr->extension,
67 format => $jobr->format,
68 content => $jobr->source,
69 },
70 },
71 map { $_ => $jobr->problem->get_column($_) } qw/generator runner judge testcnt timeout olimit/
72 };
73 $meta->{tests} = decode_json $jobr->problem->tests if $meta->{runner} eq 'File';
74
c8ca69c2 75 $meta->{files}{ver} = {
be3e5b32
MG
76 name => 'ver.cpp',
77 format => $jobr->problem->verformat,
78 content => $jobr->problem->versource,
79 } if $jobr->problem->verformat;
5c5cd38a 80
a722431b
MG
81 prepare_files $meta;
82 chomp $errors;
83
84 my ($files, $generator, $runner, $judge, $testcnt) = map { $meta->{$_} or die "Required parameter missing: $_"} qw/files generator runner judge testcnt/;
85
86 $generator = safe_can Generator => generate => $generator;
87 $runner = safe_can Runner => run => $runner;
88 $judge = safe_can Judge => judge => $judge;
89
90 for my $test (1 .. $testcnt) {
91 my $start_time = time;
92 my $result;
93 try {
94 $generator->($test, $meta);
95 $result = $runner->($test, $meta);
96 } catch {
97 $result = $_;
98 unless (ref $result) {
99 chomp $result;
100 $result = [ERR, $result];
101 }
102 };
103
104 if (ref $result) {
105 get_logger->trace("Test $test result is " . $result->[1]);
106 push @full_results, {id => $test, result => $result->[0], result_text => $result->[1], time => time - $start_time}
107 } else {
108 get_logger->trace("Test $test result is $result");
109 push @full_results, {id => $test, result => 0, result_text => $result, time => time - $start_time}
110 }
111 push @results, $result;
112 last if $meta->{judge} eq 'Absolute' && ref $result
5c5cd38a 113 }
5c5cd38a 114
a722431b
MG
115 my %results = $judge->(@results);
116 $meta->{$_} = $results{$_} for keys %results;
117 } catch {
118 s,(.*) at .*,$1,;
119 chomp;
120 $meta->{result} = -1;
121 $meta->{result_text} = $_;
122 };
123
124 get_logger->info("Job result: " . $meta->{result_text});
be3e5b32
MG
125 $jobr->update({
126 result => $meta->{result},
127 result_text => $meta->{result_text},
128 results => encode_json \@full_results,
129 $errors ? (errors => $errors) : ()
130 });
131
132 my $log = $jobr->contest ? 'ct/' . $jobr->contest . '/log' : 'log';
a722431b 133 my $page = ($job + PAGE_SIZE - 1) / PAGE_SIZE;
a722431b 134
be3e5b32 135 purge "/$log/$job";
a722431b
MG
136 purge "/$log/";
137 purge "/$log/st";
be3e5b32 138 purge "/$log/page/$_" for $page - 1, $page, $page + 1;
ab436d78
MG
139}
140
141sub got_job{
be3e5b32
MG
142 my $job = $_[0];
143 my $id = $job->id;
144 get_logger->debug("Taking job $id...");
145 my $daemon = hostname . ":$$";
146 $job->update({daemon => $daemon});
147 #if (set_job_daemon $job, hostname . ":$$") {
148 if (1) {
149 get_logger->debug("Succesfully taken job $id");
150 process $id, $job;
151 get_logger->debug("Job $id done");
ab436d78 152 } else {
be3e5b32 153 get_logger->debug("Job $id already taken");
ab436d78 154 }
5c5cd38a
MG
155}
156
157sub run{
a722431b
MG
158 Log::Log4perl->init('/etc/gruntmasterd/gruntmasterd-log.conf');
159 get_logger->info("gruntmasterd $VERSION started");
160 chdir tempdir 'gruntmasterd.XXXX', CLEANUP => 1, TMPDIR => 1;
be3e5b32
MG
161 while (1) {
162 my $job = $db->jobs->search({daemon => undef}, {rows => 1})->first;
163 got_job $job if defined $job;
164 sleep 2 unless defined $job;
165 }
5c5cd38a
MG
166}
167
1681;
169__END__
5c5cd38a
MG
170
171=head1 NAME
172
bc372959 173Gruntmaster::Daemon - Gruntmaster 6000 Online Judge -- daemon
5c5cd38a
MG
174
175=head1 SYNOPSIS
176
177 use Gruntmaster::Daemon;
bc372959 178 Gruntmaster::Daemon->run;
5c5cd38a
MG
179
180=head1 DESCRIPTION
181
bc372959 182Gruntmaster::Daemon is the daemon component of the Gruntmaster 6000 online judge.
5c5cd38a
MG
183
184=head1 AUTHOR
185
bc372959 186Marius Gavrilescu E<lt>marius@ieval.roE<gt>
5c5cd38a
MG
187
188=head1 COPYRIGHT AND LICENSE
189
bc372959 190Copyright (C) 2014 by Marius Gavrilescu
5c5cd38a 191
bc372959
MG
192This library is free software: you can redistribute it and/or modify
193it under the terms of the GNU Affero General Public License as published by
194the Free Software Foundation, either version 3 of the License, or
195(at your option) any later version.
5c5cd38a
MG
196
197
198=cut
This page took 0.058923 seconds and 4 git commands to generate.