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