Do not load Gruntmaster::Data if not needed
[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_004';
8
9 use Gruntmaster::Daemon::Constants qw/ERR/;
10 use Gruntmaster::Daemon::Format qw/prepare_files/;
11
12 use File::Basename qw/fileparse/;
13 use File::Temp qw/tempdir/;
14 use JSON qw/decode_json encode_json/;
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 use constant FORMAT_EXTENSION => {
22 C => 'c',
23 CPP => 'cpp',
24 GCCGO => 'go',
25 GOLANG => 'go',
26 HASKELL => 'hs',
27 MONO => 'cs',
28 JAVA => 'java',
29 PASCAL => 'pas',
30 PERL => 'pl',
31 PYTHON => 'py',
32 };
33
34 ##################################################
35
36 sub purge;
37
38 my $db;
39
40 sub safe_can_nodie {
41 my ($type, $sub, $name) = @_;
42
43 return unless $name =~ /^\w+$/;
44 no strict 'refs';
45 my $pkg = __PACKAGE__ . "::${type}::${name}";
46 eval "require $pkg" or get_logger->warn("Error while requiring $pkg: $@");
47 $pkg->can($sub);
48 }
49
50 sub safe_can {
51 my ($type, $sub, $name) = @_;
52
53 safe_can_nodie @_ or get_logger->logdie("No such \l$type: '$name'");
54 }
55
56 sub process{
57 my ($job, $jobr) = @_;
58
59 my @results;
60 my @full_results = ();
61 my $meta;
62 our $errors = '';
63 try {
64 if (ref $job) {
65 $meta = $job;
66 } else {
67 $meta = {
68 problem => $jobr->problem->id,
69 files => {
70 prog => {
71 name => 'prog.' . $jobr->extension,
72 format => $jobr->format,
73 content => $jobr->source,
74 },
75 },
76 map { $_ => $jobr->problem->get_column($_) } qw/generator runner judge testcnt timeout olimit/
77 };
78 $meta->{tests} = decode_json $jobr->problem->tests if $meta->{runner} eq 'File';
79
80 $meta->{files}{ver} = {
81 name => 'ver.' . FORMAT_EXTENSION->{$jobr->problem->verformat},
82 format => $jobr->problem->verformat,
83 content => $jobr->problem->versource,
84 } if $jobr->problem->verformat;
85 }
86
87 prepare_files $meta;
88 chomp $errors;
89
90 my ($files, $generator, $runner, $judge, $testcnt) = map { $meta->{$_} or die "Required parameter missing: $_"} qw/files generator runner judge testcnt/;
91
92 $generator = safe_can Generator => generate => $generator;
93 $runner = safe_can Runner => run => $runner;
94 $judge = safe_can Judge => judge => $judge;
95
96 for my $test (1 .. $testcnt) {
97 my $start_time = time;
98 my $result;
99 try {
100 $generator->($test, $meta);
101 $result = $runner->($test, $meta);
102 } catch {
103 $result = $_;
104 unless (ref $result) {
105 chomp $result;
106 $result = [ERR, $result];
107 }
108 };
109
110 if (ref $result) {
111 get_logger->trace("Test $test result is " . $result->[1]);
112 push @full_results, {id => $test, result => $result->[0], result_text => $result->[1], time => time - $start_time}
113 } else {
114 get_logger->trace("Test $test result is $result");
115 push @full_results, {id => $test, result => 0, result_text => $result, time => time - $start_time}
116 }
117 push @results, $result;
118 last if $meta->{judge} eq 'Absolute' && ref $result
119 }
120
121 my %results = $judge->(@results);
122 $meta->{$_} = $results{$_} for keys %results;
123 $meta->{results} = \@full_results
124 } catch {
125 s,(.*) at .*,$1,;
126 chomp;
127 $meta->{result} = -1;
128 $meta->{result_text} = $_;
129 };
130
131 get_logger->info("Job result: " . $meta->{result_text});
132 return unless $jobr;
133 $jobr->update({
134 result => $meta->{result},
135 result_text => $meta->{result_text},
136 ($meta->{results} ? (results => encode_json $meta->{results}) : ()),
137 $errors ? (errors => $errors) : ()
138 });
139
140 my $solved = $meta->{result} ? 0 : 1;
141 my $status = $db->problem_statuses->find($jobr->owner, $jobr->problem);
142 $db->problem_statuses->update_or_create({
143 problem => $jobr->problem,
144 owner => $jobr->owner,
145 job => $jobr->id,
146 solved => $solved});
147
148 purge '/log/';
149 purge "/log/$job";
150 purge '/st/' . $jobr->contest if $jobr->contest;
151 purge '/us/';
152 purge '/us/' . $jobr->owner;
153 }
154
155 sub got_job{
156 my $job = $_[0];
157 my $id = $job->id;
158 get_logger->debug("Taking job $id...");
159 my $daemon = hostname . ":$$";
160 $job->update({daemon => $daemon});
161 #if (set_job_daemon $job, hostname . ":$$") {
162 if (1) {
163 get_logger->debug("Succesfully taken job $id");
164 process $id, $job;
165 get_logger->debug("Job $id done");
166 } else {
167 get_logger->debug("Job $id already taken");
168 }
169 }
170
171 sub run{
172 require Gruntmaster::Data;
173 $db = Gruntmaster::Data->connect('dbi:Pg:');
174 Log::Log4perl->init('/etc/gruntmasterd/gruntmasterd-log.conf');
175 get_logger->info("gruntmasterd $VERSION started");
176 chdir tempdir 'gruntmasterd.XXXX', CLEANUP => 1, TMPDIR => 1;
177 while (1) {
178 my $job = $db->jobs->search({daemon => undef}, {rows => 1})->first;
179 got_job $job if defined $job;
180 sleep 2 unless defined $job;
181 }
182 }
183
184 1;
185 __END__
186
187 =head1 NAME
188
189 Gruntmaster::Daemon - Gruntmaster 6000 Online Judge -- daemon
190
191 =head1 SYNOPSIS
192
193 use Gruntmaster::Daemon;
194 Gruntmaster::Daemon->run;
195
196 =head1 DESCRIPTION
197
198 Gruntmaster::Daemon is the daemon component of the Gruntmaster 6000 online judge.
199
200 =head1 AUTHOR
201
202 Marius Gavrilescu E<lt>marius@ieval.roE<gt>
203
204 =head1 COPYRIGHT AND LICENSE
205
206 Copyright (C) 2014 by Marius Gavrilescu
207
208 This library is free software: you can redistribute it and/or modify
209 it under the terms of the GNU Affero General Public License as published by
210 the Free Software Foundation, either version 3 of the License, or
211 (at your option) any later version.
212
213
214 =cut
This page took 0.068389 seconds and 4 git commands to generate.