Add perlcritic test and make code comply
[gruntmaster-daemon.git] / lib / Gruntmaster / Daemon.pm
CommitLineData
5c5cd38a
MG
1package Gruntmaster::Daemon;
2
3use 5.014000;
4use strict;
5use warnings;
6
3e7fd903 7our $VERSION = '5999.000_004';
5c5cd38a
MG
8
9use Gruntmaster::Daemon::Constants qw/ERR/;
fdb99417 10use Gruntmaster::Daemon::Format qw/prepare_files stopvms/;
5c5cd38a 11
ab436d78 12use File::Basename qw/fileparse/;
ab436d78 13use File::Temp qw/tempdir/;
be3e5b32 14use JSON qw/decode_json encode_json/;
ab436d78 15use Sys::Hostname qw/hostname/;
5c5cd38a
MG
16use Time::HiRes qw/time/;
17use Try::Tiny;
5c5cd38a
MG
18use Log::Log4perl qw/get_logger/;
19
adb44605 20use constant PAGE_SIZE => 10;
6bf57d23
MG
21use constant FORMAT_EXTENSION => {
22 C => 'c',
23 CPP => 'cpp',
5a4392d5
MG
24 GCCGO => 'go',
25 GOLANG => 'go',
26 HASKELL => 'hs',
6bf57d23
MG
27 MONO => 'cs',
28 JAVA => 'java',
29 PASCAL => 'pas',
30 PERL => 'pl',
31 PYTHON => 'py',
32};
adb44605 33
5c5cd38a
MG
34##################################################
35
55e61ae8
MG
36sub purge;
37
7be5f054 38my $db;
064332cd 39
5c5cd38a 40sub safe_can_nodie {
a722431b 41 my ($type, $sub, $name) = @_;
5c5cd38a 42
99d37110 43 return unless $name =~ /^\w+$/s;
a722431b
MG
44 my $pkg = __PACKAGE__ . "::${type}::${name}";
45 eval "require $pkg" or get_logger->warn("Error while requiring $pkg: $@");
46 $pkg->can($sub);
5c5cd38a
MG
47}
48
49sub safe_can {
a722431b 50 my ($type, $sub, $name) = @_;
5c5cd38a 51
a722431b 52 safe_can_nodie @_ or get_logger->logdie("No such \l$type: '$name'");
5c5cd38a
MG
53}
54
ab436d78 55sub process{
be3e5b32 56 my ($job, $jobr) = @_;
a722431b
MG
57
58 my @results;
59 my @full_results = ();
be3e5b32 60 my $meta;
a722431b
MG
61 our $errors = '';
62 try {
7be5f054
MG
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 },
be3e5b32 74 },
7be5f054
MG
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';
be3e5b32 78
7be5f054
MG
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 }
5c5cd38a 85
a722431b
MG
86 prepare_files $meta;
87 chomp $errors;
88
99d37110 89 my ($files, $generator, $runner, $judge, $testcnt) = map { $meta->{$_} or die "Required parameter missing: $_\n"} qw/files generator runner judge testcnt/;
a722431b
MG
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
5c5cd38a 118 }
5c5cd38a 119
a722431b
MG
120 my %results = $judge->(@results);
121 $meta->{$_} = $results{$_} for keys %results;
7be5f054 122 $meta->{results} = \@full_results
a722431b 123 } catch {
99d37110 124 s/(.*) at .*/$1/s;
a722431b
MG
125 chomp;
126 $meta->{result} = -1;
127 $meta->{result_text} = $_;
128 };
fdb99417 129 stopvms;
a722431b 130
99d37110 131 get_logger->info('Job result: ' . $meta->{result_text});
7be5f054 132 return unless $jobr;
be3e5b32
MG
133 $jobr->update({
134 result => $meta->{result},
135 result_text => $meta->{result_text},
6555bf80 136 ($meta->{results} ? (results => encode_json $meta->{results}) : ()),
be3e5b32
MG
137 $errors ? (errors => $errors) : ()
138 });
139
b335c4bb
MG
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
a5d85109
MG
148 purge '/log/';
149 purge "/log/$job";
150 purge '/st/' . $jobr->contest if $jobr->contest;
151 purge '/us/';
152 purge '/us/' . $jobr->owner;
ab436d78
MG
153}
154
155sub got_job{
be3e5b32
MG
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");
ab436d78 166 } else {
be3e5b32 167 get_logger->debug("Job $id already taken");
ab436d78 168 }
5c5cd38a
MG
169}
170
171sub run{
fdb99417 172 warn "No GRUNTMASTER_VM environment variable. Running without a VM is a security risk.\n" unless $ENV{GRUNTMASTER_VM};
55e61ae8 173 require Gruntmaster::Data;
7be5f054 174 $db = Gruntmaster::Data->connect('dbi:Pg:');
a722431b
MG
175 Log::Log4perl->init('/etc/gruntmasterd/gruntmasterd-log.conf');
176 get_logger->info("gruntmasterd $VERSION started");
177 chdir tempdir 'gruntmasterd.XXXX', CLEANUP => 1, TMPDIR => 1;
be3e5b32
MG
178 while (1) {
179 my $job = $db->jobs->search({daemon => undef}, {rows => 1})->first;
180 got_job $job if defined $job;
181 sleep 2 unless defined $job;
182 }
5c5cd38a
MG
183}
184
1851;
186__END__
5c5cd38a
MG
187
188=head1 NAME
189
bc372959 190Gruntmaster::Daemon - Gruntmaster 6000 Online Judge -- daemon
5c5cd38a
MG
191
192=head1 SYNOPSIS
193
194 use Gruntmaster::Daemon;
bc372959 195 Gruntmaster::Daemon->run;
5c5cd38a
MG
196
197=head1 DESCRIPTION
198
bc372959 199Gruntmaster::Daemon is the daemon component of the Gruntmaster 6000 online judge.
5c5cd38a
MG
200
201=head1 AUTHOR
202
bc372959 203Marius Gavrilescu E<lt>marius@ieval.roE<gt>
5c5cd38a
MG
204
205=head1 COPYRIGHT AND LICENSE
206
bc372959 207Copyright (C) 2014 by Marius Gavrilescu
5c5cd38a 208
bc372959
MG
209This library is free software: you can redistribute it and/or modify
210it under the terms of the GNU Affero General Public License as published by
211the Free Software Foundation, either version 3 of the License, or
212(at your option) any later version.
5c5cd38a
MG
213
214
215=cut
This page took 0.094187 seconds and 4 git commands to generate.