Support new purge interface
[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_001';
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 Sys::Hostname qw/hostname/;
16 use Time::HiRes qw/time/;
17 use Try::Tiny;
18 use Log::Log4perl qw/get_logger/;
19 use LWP::UserAgent;
20
21 use constant PAGE_SIZE => 10;
22
23 ##################################################
24
25 my $ua = LWP::UserAgent->new;
26
27 sub safe_can_nodie {
28 my ($type, $sub, $name) = @_;
29
30 return unless $name =~ /^\w+$/;
31 no strict 'refs';
32 my $pkg = __PACKAGE__ . "::${type}::${name}";
33 eval "require $pkg" or get_logger->warn("Error while requiring $pkg: $@");
34 $pkg->can($sub);
35 }
36
37 sub safe_can {
38 my ($type, $sub, $name) = @_;
39
40 safe_can_nodie @_ or get_logger->logdie("No such \l$type: '$name'");
41 }
42
43 sub purge {
44 return unless $ENV{PURGE_HOST};
45 my $req = HTTP::Request->new(PURGE => "http://$ENV{PURGE_HOST}$_[0]");
46 $ua->request($req)
47 }
48
49 sub process{
50 my $job = shift;
51
52 my @results;
53 my @full_results = ();
54 my $meta = {};
55 our $errors = '';
56 try {
57 $meta = job_inmeta $job;
58 if (job_problem $job) {
59 my $pbmeta = problem_meta job_problem $job;
60 my %files = exists $meta->{files} ? %{$meta->{files}} : ();
61 $meta = {%$meta, %$pbmeta};
62 $meta->{files} = {%files, %{$pbmeta->{files}}} if exists $pbmeta->{files};
63 }
64
65 prepare_files $meta;
66 chomp $errors;
67
68 my ($files, $generator, $runner, $judge, $testcnt) = map { $meta->{$_} or die "Required parameter missing: $_"} qw/files generator runner judge testcnt/;
69
70 $generator = safe_can Generator => generate => $generator;
71 $runner = safe_can Runner => run => $runner;
72 $judge = safe_can Judge => judge => $judge;
73
74 for my $test (1 .. $testcnt) {
75 my $start_time = time;
76 my $result;
77 try {
78 $generator->($test, $meta);
79 $result = $runner->($test, $meta);
80 } catch {
81 $result = $_;
82 unless (ref $result) {
83 chomp $result;
84 $result = [ERR, $result];
85 }
86 };
87
88 if (ref $result) {
89 get_logger->trace("Test $test result is " . $result->[1]);
90 push @full_results, {id => $test, result => $result->[0], result_text => $result->[1], time => time - $start_time}
91 } else {
92 get_logger->trace("Test $test result is $result");
93 push @full_results, {id => $test, result => 0, result_text => $result, time => time - $start_time}
94 }
95 push @results, $result;
96 last if $meta->{judge} eq 'Absolute' && ref $result
97 }
98
99 my %results = $judge->(@results);
100 $meta->{$_} = $results{$_} for keys %results;
101 } catch {
102 s,(.*) at .*,$1,;
103 chomp;
104 $meta->{result} = -1;
105 $meta->{result_text} = $_;
106 };
107
108 get_logger->info("Job result: " . $meta->{result_text});
109 set_job_result $job, $meta->{result};
110 set_job_result_text $job, $meta->{result_text};
111 set_job_results $job, \@full_results if scalar @full_results;
112 set_job_errors $job, $errors;
113
114 my $log = $Gruntmaster::Data::contest ? "ct/$Gruntmaster::Data::contest/log" : 'log';
115
116 PUBLISH gensrc => ($Gruntmaster::Data::contest // '') . ".$job";
117 PUBLISH genpage => "$log/job/$job.html";
118 PUBLISH genpage => "$log/index.html";
119 PUBLISH genpage => "$log/st.html";
120 my $page = ($job + PAGE_SIZE - 1) / PAGE_SIZE;
121 PUBLISH genpage => "$log/@{[$page - 1]}.html";
122 PUBLISH genpage => "$log/$page.html";
123 PUBLISH genpage => "$log/@{[$page + 1]}.html";
124
125 purge "/$log/job/$job";
126 purge "/$log/";
127 purge "/$log/st";
128 purge "/$log/$_" for $page - 1, $page, $page + 1;
129 }
130
131 sub got_job{
132 $_[0] =~ /^(\w*)\.(\d+)$/;
133 my $job = $2;
134 local $Gruntmaster::Data::contest = $1 if $1;
135 get_logger->debug("Taking job $job@{[defined $1 ? \" of contest $1\" : '']}...");
136 if (set_job_daemon $job, hostname . ":$$") {
137 get_logger->debug("Succesfully taken job $job");
138 process $job;
139 get_logger->debug("Job $job done");
140 } else {
141 get_logger->debug("Job $job already taken");
142 }
143 }
144
145 sub run{
146 Log::Log4perl->init('/etc/gruntmasterd/gruntmasterd-log.conf');
147 get_logger->info("gruntmasterd $VERSION started");
148 chdir tempdir 'gruntmasterd.XXXX', CLEANUP => 1, TMPDIR => 1;
149 SUBSCRIBE jobs => \&got_job;
150 WAIT_FOR_MESSAGES 86400 while 1
151 }
152
153 1;
154 __END__
155
156 =head1 NAME
157
158 Gruntmaster::Daemon - Gruntmaster 6000 Online Judge -- daemon
159
160 =head1 SYNOPSIS
161
162 use Gruntmaster::Daemon;
163 Gruntmaster::Daemon->run;
164
165 =head1 DESCRIPTION
166
167 Gruntmaster::Daemon is the daemon component of the Gruntmaster 6000 online judge.
168
169 =head1 AUTHOR
170
171 Marius Gavrilescu E<lt>marius@ieval.roE<gt>
172
173 =head1 COPYRIGHT AND LICENSE
174
175 Copyright (C) 2014 by Marius Gavrilescu
176
177 This library is free software: you can redistribute it and/or modify
178 it under the terms of the GNU Affero General Public License as published by
179 the Free Software Foundation, either version 3 of the License, or
180 (at your option) any later version.
181
182
183 =cut
This page took 0.029771 seconds and 4 git commands to generate.