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