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