156d7435adcff83e3fa81478aee6b7fc2fb69f39
[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
20 use constant PAGE_SIZE => 10;
21
22 ##################################################
23
24 sub 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
34 sub safe_can {
35 my ($type, $sub, $name) = @_;
36
37 safe_can_nodie @_ or get_logger->logdie("No such \l$type: '$name'");
38 }
39
40 sub process{
41 my $job = shift;
42
43 my @results;
44 my @full_results = ();
45 my $meta = {};
46 our $errors = '';
47 try {
48 $meta = job_inmeta $job;
49 if (job_problem $job) {
50 my $pbmeta = problem_meta job_problem $job;
51 my %files = exists $meta->{files} ? %{$meta->{files}} : ();
52 $meta = {%$meta, %$pbmeta};
53 $meta->{files} = {%files, %{$pbmeta->{files}}} if exists $pbmeta->{files};
54 }
55
56 prepare_files $meta;
57 chomp $errors;
58
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
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
99 get_logger->info("Job result: " . $meta->{result_text});
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;
103 set_job_errors $job, $errors;
104
105 my $log = $Gruntmaster::Data::contest ? "ct/$Gruntmaster::Data::contest/log" : 'log';
106
107 PUBLISH gensrc => ($Gruntmaster::Data::contest // '') . ".$job";
108 PUBLISH genpage => "$log/job/$job.html";
109 PUBLISH genpage => "$log/index.html";
110 PUBLISH genpage => "$log/st.html";
111 my $page = ($job + PAGE_SIZE - 1) / PAGE_SIZE;
112 PUBLISH genpage => "$log/@{[$page - 1]}.html";
113 PUBLISH genpage => "$log/$page.html";
114 PUBLISH genpage => "$log/@{[$page + 1]}.html";
115 }
116
117 sub got_job{
118 $_[0] =~ /^(\w*)\.(\d+)$/;
119 my $job = $2;
120 local $Gruntmaster::Data::contest = $1 if $1;
121 get_logger->debug("Taking job $job@{[defined $1 ? \" of contest $1\" : '']}...");
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 }
129 }
130
131 sub run{
132 Log::Log4perl->init('/etc/gruntmasterd/gruntmasterd-log.conf');
133 get_logger->info("gruntmasterd $VERSION started");
134 chdir tempdir 'gruntmasterd.XXXX', CLEANUP => 1, TMPDIR => 1;
135 SUBSCRIBE jobs => \&got_job;
136 WAIT_FOR_MESSAGES 86400 while 1
137 }
138
139 1;
140 __END__
141
142 =head1 NAME
143
144 Gruntmaster::Daemon - Gruntmaster 6000 Online Judge -- daemon
145
146 =head1 SYNOPSIS
147
148 use Gruntmaster::Daemon;
149 Gruntmaster::Daemon->run;
150
151 =head1 DESCRIPTION
152
153 Gruntmaster::Daemon is the daemon component of the Gruntmaster 6000 online judge.
154
155 =head1 AUTHOR
156
157 Marius Gavrilescu E<lt>marius@ieval.roE<gt>
158
159 =head1 COPYRIGHT AND LICENSE
160
161 Copyright (C) 2014 by Marius Gavrilescu
162
163 This library is free software: you can redistribute it and/or modify
164 it under the terms of the GNU Affero General Public License as published by
165 the Free Software Foundation, either version 3 of the License, or
166 (at your option) any later version.
167
168
169 =cut
This page took 0.028313 seconds and 3 git commands to generate.