Support problem parameters outside meta
[gruntmaster-daemon.git] / lib / Gruntmaster / Daemon.pm
CommitLineData
5c5cd38a
MG
1package Gruntmaster::Daemon;
2
3use 5.014000;
4use strict;
5use warnings;
6
a5522656 7our $VERSION = '5999.000_001';
5c5cd38a
MG
8
9use Gruntmaster::Daemon::Constants qw/ERR/;
ad77b7d3 10use Gruntmaster::Daemon::Format qw/prepare_files/;
4dc6495c 11use Gruntmaster::Data;
5c5cd38a 12
ab436d78 13use File::Basename qw/fileparse/;
ab436d78 14use File::Temp qw/tempdir/;
ab436d78 15use Sys::Hostname qw/hostname/;
5c5cd38a
MG
16use Time::HiRes qw/time/;
17use Try::Tiny;
5c5cd38a 18use Log::Log4perl qw/get_logger/;
064332cd 19use LWP::UserAgent;
5c5cd38a 20
adb44605
MG
21use constant PAGE_SIZE => 10;
22
5c5cd38a
MG
23##################################################
24
064332cd 25my $ua = LWP::UserAgent->new;
82657f57 26my @purge_hosts = exists $ENV{PURGE_HOSTS} ? split ' ', $ENV{PURGE_HOSTS} : ();
064332cd 27
5c5cd38a
MG
28sub 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
38sub safe_can {
39 my ($type, $sub, $name) = @_;
40
41 safe_can_nodie @_ or get_logger->logdie("No such \l$type: '$name'");
42}
43
064332cd 44sub purge {
82657f57
MG
45 for my $host (@purge_hosts) {
46 my $req = HTTP::Request->new(PURGE => "http://$host$_[0]");
47 $ua->request($req)
48 }
064332cd
MG
49}
50
ab436d78
MG
51sub process{
52 my $job = shift;
5c5cd38a
MG
53
54 my @results;
55 my @full_results = ();
56 my $meta = {};
22e9a940 57 our $errors = '';
5c5cd38a 58 try {
ab436d78
MG
59 $meta = job_inmeta $job;
60 if (job_problem $job) {
6d590c60
MG
61 local $_ = job_problem $job;
62 my $pbmeta = problem_meta;
adb44605 63 my %files = exists $meta->{files} ? %{$meta->{files}} : ();
6d590c60
MG
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 };
ab436d78 74 $meta->{files} = {%files, %{$pbmeta->{files}}} if exists $pbmeta->{files};
5c5cd38a
MG
75 }
76
ab436d78 77 prepare_files $meta;
22e9a940 78 chomp $errors;
ab436d78 79
5c5cd38a
MG
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
5c5cd38a
MG
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
5c5cd38a 120 get_logger->info("Job result: " . $meta->{result_text});
ab436d78
MG
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;
22e9a940 124 set_job_errors $job, $errors;
ab436d78 125
0e0e340d 126 my $log = $Gruntmaster::Data::contest ? "ct/$Gruntmaster::Data::contest/log" : 'log';
adb44605
MG
127
128 PUBLISH gensrc => ($Gruntmaster::Data::contest // '') . ".$job";
95ea982c 129 PUBLISH genpage => "$log/job/$job.html";
7ddb70a9 130 PUBLISH genpage => "$log/index.html";
4dc6495c 131 PUBLISH genpage => "$log/st.html";
adb44605 132 my $page = ($job + PAGE_SIZE - 1) / PAGE_SIZE;
4dc6495c
MG
133 PUBLISH genpage => "$log/@{[$page - 1]}.html";
134 PUBLISH genpage => "$log/$page.html";
135 PUBLISH genpage => "$log/@{[$page + 1]}.html";
064332cd
MG
136
137 purge "/$log/job/$job";
138 purge "/$log/";
139 purge "/$log/st";
140 purge "/$log/$_" for $page - 1, $page, $page + 1;
ab436d78
MG
141}
142
143sub got_job{
cfb728e8
MG
144 $_[0] =~ /^(\w*)\.(\d+)$/;
145 my $job = $2;
0890dd25 146 local $Gruntmaster::Data::contest = $1 if $1;
cfb728e8 147 get_logger->debug("Taking job $job@{[defined $1 ? \" of contest $1\" : '']}...");
ab436d78
MG
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 }
5c5cd38a
MG
155}
156
157sub run{
bd084fbe 158 Log::Log4perl->init('/etc/gruntmasterd/gruntmasterd-log.conf');
ab436d78 159 get_logger->info("gruntmasterd $VERSION started");
f3ad9692
MG
160 chdir tempdir 'gruntmasterd.XXXX', CLEANUP => 1, TMPDIR => 1;
161 SUBSCRIBE jobs => \&got_job;
162 WAIT_FOR_MESSAGES 86400 while 1
5c5cd38a
MG
163}
164
1651;
166__END__
5c5cd38a
MG
167
168=head1 NAME
169
bc372959 170Gruntmaster::Daemon - Gruntmaster 6000 Online Judge -- daemon
5c5cd38a
MG
171
172=head1 SYNOPSIS
173
174 use Gruntmaster::Daemon;
bc372959 175 Gruntmaster::Daemon->run;
5c5cd38a
MG
176
177=head1 DESCRIPTION
178
bc372959 179Gruntmaster::Daemon is the daemon component of the Gruntmaster 6000 online judge.
5c5cd38a
MG
180
181=head1 AUTHOR
182
bc372959 183Marius Gavrilescu E<lt>marius@ieval.roE<gt>
5c5cd38a
MG
184
185=head1 COPYRIGHT AND LICENSE
186
bc372959 187Copyright (C) 2014 by Marius Gavrilescu
5c5cd38a 188
bc372959
MG
189This library is free software: you can redistribute it and/or modify
190it under the terms of the GNU Affero General Public License as published by
191the Free Software Foundation, either version 3 of the License, or
192(at your option) any later version.
5c5cd38a
MG
193
194
195=cut
This page took 0.027928 seconds and 4 git commands to generate.