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