Support problem parameters outside meta
[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 local $_ = job_problem $job;
62 my $pbmeta = problem_meta;
63 my %files = exists $meta->{files} ? %{$meta->{files}} : ();
64 $meta = {
65 %$meta,
66 (defined problem_generator() ? (generator => problem_generator) : ()),
67 (defined problem_runner() ? (runner => problem_runner) : ()),
68 (defined problem_judge() ? (judge => problem_judge) : ()),
69 (defined problem_testcnt() ? (testcnt => problem_testcnt) : ()),
70 (defined problem_timeout() ? (timeout => problem_timeout) : ()),
71 (defined problem_olimit() ? (olimit => problem_olimit) : ()),
72 %$pbmeta
73 };
74 $meta->{files} = {%files, %{$pbmeta->{files}}} if exists $pbmeta->{files};
75 }
76
77 prepare_files $meta;
78 chomp $errors;
79
80 my ($files, $generator, $runner, $judge, $testcnt) = map { $meta->{$_} or die "Required parameter missing: $_"} qw/files generator runner judge testcnt/;
81
82 $generator = safe_can Generator => generate => $generator;
83 $runner = safe_can Runner => run => $runner;
84 $judge = safe_can Judge => judge => $judge;
85
86 for my $test (1 .. $testcnt) {
87 my $start_time = time;
88 my $result;
89 try {
90 $generator->($test, $meta);
91 $result = $runner->($test, $meta);
92 } catch {
93 $result = $_;
94 unless (ref $result) {
95 chomp $result;
96 $result = [ERR, $result];
97 }
98 };
99
100 if (ref $result) {
101 get_logger->trace("Test $test result is " . $result->[1]);
102 push @full_results, {id => $test, result => $result->[0], result_text => $result->[1], time => time - $start_time}
103 } else {
104 get_logger->trace("Test $test result is $result");
105 push @full_results, {id => $test, result => 0, result_text => $result, time => time - $start_time}
106 }
107 push @results, $result;
108 last if $meta->{judge} eq 'Absolute' && ref $result
109 }
110
111 my %results = $judge->(@results);
112 $meta->{$_} = $results{$_} for keys %results;
113 } catch {
114 s,(.*) at .*,$1,;
115 chomp;
116 $meta->{result} = -1;
117 $meta->{result_text} = $_;
118 };
119
120 get_logger->info("Job result: " . $meta->{result_text});
121 set_job_result $job, $meta->{result};
122 set_job_result_text $job, $meta->{result_text};
123 set_job_results $job, \@full_results if scalar @full_results;
124 set_job_errors $job, $errors;
125
126 my $log = $Gruntmaster::Data::contest ? "ct/$Gruntmaster::Data::contest/log" : 'log';
127
128 PUBLISH gensrc => ($Gruntmaster::Data::contest // '') . ".$job";
129 PUBLISH genpage => "$log/job/$job.html";
130 PUBLISH genpage => "$log/index.html";
131 PUBLISH genpage => "$log/st.html";
132 my $page = ($job + PAGE_SIZE - 1) / PAGE_SIZE;
133 PUBLISH genpage => "$log/@{[$page - 1]}.html";
134 PUBLISH genpage => "$log/$page.html";
135 PUBLISH genpage => "$log/@{[$page + 1]}.html";
136
137 purge "/$log/job/$job";
138 purge "/$log/";
139 purge "/$log/st";
140 purge "/$log/$_" for $page - 1, $page, $page + 1;
141 }
142
143 sub got_job{
144 $_[0] =~ /^(\w*)\.(\d+)$/;
145 my $job = $2;
146 local $Gruntmaster::Data::contest = $1 if $1;
147 get_logger->debug("Taking job $job@{[defined $1 ? \" of contest $1\" : '']}...");
148 if (set_job_daemon $job, hostname . ":$$") {
149 get_logger->debug("Succesfully taken job $job");
150 process $job;
151 get_logger->debug("Job $job done");
152 } else {
153 get_logger->debug("Job $job already taken");
154 }
155 }
156
157 sub run{
158 Log::Log4perl->init('/etc/gruntmasterd/gruntmasterd-log.conf');
159 get_logger->info("gruntmasterd $VERSION started");
160 chdir tempdir 'gruntmasterd.XXXX', CLEANUP => 1, TMPDIR => 1;
161 SUBSCRIBE jobs => \&got_job;
162 WAIT_FOR_MESSAGES 86400 while 1
163 }
164
165 1;
166 __END__
167
168 =head1 NAME
169
170 Gruntmaster::Daemon - Gruntmaster 6000 Online Judge -- daemon
171
172 =head1 SYNOPSIS
173
174 use Gruntmaster::Daemon;
175 Gruntmaster::Daemon->run;
176
177 =head1 DESCRIPTION
178
179 Gruntmaster::Daemon is the daemon component of the Gruntmaster 6000 online judge.
180
181 =head1 AUTHOR
182
183 Marius Gavrilescu E<lt>marius@ieval.roE<gt>
184
185 =head1 COPYRIGHT AND LICENSE
186
187 Copyright (C) 2014 by Marius Gavrilescu
188
189 This library is free software: you can redistribute it and/or modify
190 it under the terms of the GNU Affero General Public License as published by
191 the Free Software Foundation, either version 3 of the License, or
192 (at your option) any later version.
193
194
195 =cut
This page took 0.032273 seconds and 4 git commands to generate.