Purge integer pages in Daemon.pm
[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 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 = Gruntmaster::Data->connect('dbi:Pg:');
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 $meta = {
72 problem => $jobr->problem->id,
73 files => {
74 prog => {
75 name => 'prog.' . $jobr->extension,
76 format => $jobr->format,
77 content => $jobr->source,
78 },
79 },
80 map { $_ => $jobr->problem->get_column($_) } qw/generator runner judge testcnt timeout olimit/
81 };
82 $meta->{tests} = decode_json $jobr->problem->tests if $meta->{runner} eq 'File';
83
84 $meta->{files}{ver} = {
85 name => 'ver.' . FORMAT_EXTENSION->{$jobr->problem->verformat},
86 format => $jobr->problem->verformat,
87 content => $jobr->problem->versource,
88 } if $jobr->problem->verformat;
89
90 prepare_files $meta;
91 chomp $errors;
92
93 my ($files, $generator, $runner, $judge, $testcnt) = map { $meta->{$_} or die "Required parameter missing: $_"} qw/files generator runner judge testcnt/;
94
95 $generator = safe_can Generator => generate => $generator;
96 $runner = safe_can Runner => run => $runner;
97 $judge = safe_can Judge => judge => $judge;
98
99 for my $test (1 .. $testcnt) {
100 my $start_time = time;
101 my $result;
102 try {
103 $generator->($test, $meta);
104 $result = $runner->($test, $meta);
105 } catch {
106 $result = $_;
107 unless (ref $result) {
108 chomp $result;
109 $result = [ERR, $result];
110 }
111 };
112
113 if (ref $result) {
114 get_logger->trace("Test $test result is " . $result->[1]);
115 push @full_results, {id => $test, result => $result->[0], result_text => $result->[1], time => time - $start_time}
116 } else {
117 get_logger->trace("Test $test result is $result");
118 push @full_results, {id => $test, result => 0, result_text => $result, time => time - $start_time}
119 }
120 push @results, $result;
121 last if $meta->{judge} eq 'Absolute' && ref $result
122 }
123
124 my %results = $judge->(@results);
125 $meta->{$_} = $results{$_} for keys %results;
126 } catch {
127 s,(.*) at .*,$1,;
128 chomp;
129 $meta->{result} = -1;
130 $meta->{result_text} = $_;
131 };
132
133 get_logger->info("Job result: " . $meta->{result_text});
134 $jobr->update({
135 result => $meta->{result},
136 result_text => $meta->{result_text},
137 results => encode_json \@full_results,
138 $errors ? (errors => $errors) : ()
139 });
140
141 my $log = $jobr->contest ? 'ct/' . $jobr->contest->id . '/log' : 'log';
142 my $page = int (($job + PAGE_SIZE - 1) / PAGE_SIZE);
143
144 purge "/$log/$job";
145 purge "/$log/";
146 purge "/$log/st";
147 purge "/$log/page/$_" for $page - 1, $page, $page + 1;
148 }
149
150 sub got_job{
151 my $job = $_[0];
152 my $id = $job->id;
153 get_logger->debug("Taking job $id...");
154 my $daemon = hostname . ":$$";
155 $job->update({daemon => $daemon});
156 #if (set_job_daemon $job, hostname . ":$$") {
157 if (1) {
158 get_logger->debug("Succesfully taken job $id");
159 process $id, $job;
160 get_logger->debug("Job $id done");
161 } else {
162 get_logger->debug("Job $id already taken");
163 }
164 }
165
166 sub run{
167 Log::Log4perl->init('/etc/gruntmasterd/gruntmasterd-log.conf');
168 get_logger->info("gruntmasterd $VERSION started");
169 chdir tempdir 'gruntmasterd.XXXX', CLEANUP => 1, TMPDIR => 1;
170 while (1) {
171 my $job = $db->jobs->search({daemon => undef}, {rows => 1})->first;
172 got_job $job if defined $job;
173 sleep 2 unless defined $job;
174 }
175 }
176
177 1;
178 __END__
179
180 =head1 NAME
181
182 Gruntmaster::Daemon - Gruntmaster 6000 Online Judge -- daemon
183
184 =head1 SYNOPSIS
185
186 use Gruntmaster::Daemon;
187 Gruntmaster::Daemon->run;
188
189 =head1 DESCRIPTION
190
191 Gruntmaster::Daemon is the daemon component of the Gruntmaster 6000 online judge.
192
193 =head1 AUTHOR
194
195 Marius Gavrilescu E<lt>marius@ieval.roE<gt>
196
197 =head1 COPYRIGHT AND LICENSE
198
199 Copyright (C) 2014 by Marius Gavrilescu
200
201 This library is free software: you can redistribute it and/or modify
202 it under the terms of the GNU Affero General Public License as published by
203 the Free Software Foundation, either version 3 of the License, or
204 (at your option) any later version.
205
206
207 =cut
This page took 0.075794 seconds and 5 git commands to generate.