Bump version and update Changes
[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_003';
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 use 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 };
32
33 ##################################################
34
35 my $db;
36 my $ua = LWP::UserAgent->new;
37 my @purge_hosts = exists $ENV{PURGE_HOSTS} ? split ' ', $ENV{PURGE_HOSTS} : ();
38
39 sub safe_can_nodie {
40 my ($type, $sub, $name) = @_;
41
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);
47 }
48
49 sub safe_can {
50 my ($type, $sub, $name) = @_;
51
52 safe_can_nodie @_ or get_logger->logdie("No such \l$type: '$name'");
53 }
54
55 sub purge {
56 get_logger->trace("Purging $_[0]");
57 for my $host (@purge_hosts) {
58 my $req = HTTP::Request->new(PURGE => "http://$host$_[0]");
59 $ua->request($req)
60 }
61 }
62
63 sub process{
64 my ($job, $jobr) = @_;
65
66 my @results;
67 my @full_results = ();
68 my $meta;
69 our $errors = '';
70 try {
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 },
82 },
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';
86
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 }
93
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
126 }
127
128 my %results = $judge->(@results);
129 $meta->{$_} = $results{$_} for keys %results;
130 $meta->{results} = \@full_results
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});
139 return unless $jobr;
140 $jobr->update({
141 result => $meta->{result},
142 result_text => $meta->{result_text},
143 results => encode_json $meta->{results},
144 $errors ? (errors => $errors) : ()
145 });
146
147 my $log = $jobr->contest ? 'ct/' . $jobr->contest->id . '/log' : 'log';
148 my $page = int (($job + PAGE_SIZE - 1) / PAGE_SIZE);
149
150 purge "/$log/$job";
151 purge "/$log/";
152 purge "/$log/st";
153 purge "/$log/page/$_" for $page - 1, $page, $page + 1;
154 }
155
156 sub got_job{
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");
167 } else {
168 get_logger->debug("Job $id already taken");
169 }
170 }
171
172 sub run{
173 $db = Gruntmaster::Data->connect('dbi:Pg:');
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;
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 }
182 }
183
184 1;
185 __END__
186
187 =head1 NAME
188
189 Gruntmaster::Daemon - Gruntmaster 6000 Online Judge -- daemon
190
191 =head1 SYNOPSIS
192
193 use Gruntmaster::Daemon;
194 Gruntmaster::Daemon->run;
195
196 =head1 DESCRIPTION
197
198 Gruntmaster::Daemon is the daemon component of the Gruntmaster 6000 online judge.
199
200 =head1 AUTHOR
201
202 Marius Gavrilescu E<lt>marius@ieval.roE<gt>
203
204 =head1 COPYRIGHT AND LICENSE
205
206 Copyright (C) 2014 by Marius Gavrilescu
207
208 This library is free software: you can redistribute it and/or modify
209 it under the terms of the GNU Affero General Public License as published by
210 the Free Software Foundation, either version 3 of the License, or
211 (at your option) any later version.
212
213
214 =cut
This page took 0.0789 seconds and 5 git commands to generate.