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