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