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