Get rid of safe_can_nodie
[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
7be5f054 36my $db;
064332cd 37
b4ac22ff 38sub safe_can {
a722431b 39 my ($type, $sub, $name) = @_;
5c5cd38a 40
99d37110 41 return unless $name =~ /^\w+$/s;
a722431b
MG
42 my $pkg = __PACKAGE__ . "::${type}::${name}";
43 eval "require $pkg" or get_logger->warn("Error while requiring $pkg: $@");
b4ac22ff 44 $pkg->can($sub) or get_logger->logdie("No such \l$type: '$name'");
5c5cd38a
MG
45}
46
ab436d78 47sub process{
be3e5b32 48 my ($job, $jobr) = @_;
a722431b
MG
49
50 my @results;
51 my @full_results = ();
be3e5b32 52 my $meta;
a722431b
MG
53 our $errors = '';
54 try {
7be5f054
MG
55 if (ref $job) {
56 $meta = $job;
57 } else {
58 $meta = {
59 problem => $jobr->problem->id,
60 files => {
61 prog => {
62 name => 'prog.' . $jobr->extension,
63 format => $jobr->format,
64 content => $jobr->source,
65 },
be3e5b32 66 },
7be5f054
MG
67 map { $_ => $jobr->problem->get_column($_) } qw/generator runner judge testcnt timeout olimit/
68 };
69 $meta->{tests} = decode_json $jobr->problem->tests if $meta->{runner} eq 'File';
be3e5b32 70
7be5f054
MG
71 $meta->{files}{ver} = {
72 name => 'ver.' . FORMAT_EXTENSION->{$jobr->problem->verformat},
73 format => $jobr->problem->verformat,
74 content => $jobr->problem->versource,
75 } if $jobr->problem->verformat;
76 }
5c5cd38a 77
a722431b
MG
78 prepare_files $meta;
79 chomp $errors;
80
99d37110 81 my ($files, $generator, $runner, $judge, $testcnt) = map { $meta->{$_} or die "Required parameter missing: $_\n"} qw/files generator runner judge testcnt/;
a722431b
MG
82
83 $generator = safe_can Generator => generate => $generator;
84 $runner = safe_can Runner => run => $runner;
85 $judge = safe_can Judge => judge => $judge;
86
87 for my $test (1 .. $testcnt) {
88 my $start_time = time;
89 my $result;
90 try {
91 $generator->($test, $meta);
92 $result = $runner->($test, $meta);
93 } catch {
94 $result = $_;
95 unless (ref $result) {
96 chomp $result;
97 $result = [ERR, $result];
98 }
99 };
100
101 if (ref $result) {
102 get_logger->trace("Test $test result is " . $result->[1]);
103 push @full_results, {id => $test, result => $result->[0], result_text => $result->[1], time => time - $start_time}
104 } else {
105 get_logger->trace("Test $test result is $result");
106 push @full_results, {id => $test, result => 0, result_text => $result, time => time - $start_time}
107 }
108 push @results, $result;
109 last if $meta->{judge} eq 'Absolute' && ref $result
5c5cd38a 110 }
5c5cd38a 111
a722431b
MG
112 my %results = $judge->(@results);
113 $meta->{$_} = $results{$_} for keys %results;
7be5f054 114 $meta->{results} = \@full_results
a722431b 115 } catch {
99d37110 116 s/(.*) at .*/$1/s;
a722431b
MG
117 chomp;
118 $meta->{result} = -1;
119 $meta->{result_text} = $_;
120 };
fdb99417 121 stopvms;
a722431b 122
99d37110 123 get_logger->info('Job result: ' . $meta->{result_text});
7be5f054 124 return unless $jobr;
be3e5b32
MG
125 $jobr->update({
126 result => $meta->{result},
127 result_text => $meta->{result_text},
6555bf80 128 ($meta->{results} ? (results => encode_json $meta->{results}) : ()),
be3e5b32
MG
129 $errors ? (errors => $errors) : ()
130 });
131
b335c4bb
MG
132 my $solved = $meta->{result} ? 0 : 1;
133 my $status = $db->problem_statuses->find($jobr->owner, $jobr->problem);
134 $db->problem_statuses->update_or_create({
135 problem => $jobr->problem,
136 owner => $jobr->owner,
137 job => $jobr->id,
c1379ae8 138 solved => $solved}) if !$jobr->problem->is_private;
ab436d78
MG
139}
140
141sub got_job{
be3e5b32
MG
142 my $job = $_[0];
143 my $id = $job->id;
144 get_logger->debug("Taking job $id...");
145 my $daemon = hostname . ":$$";
146 $job->update({daemon => $daemon});
147 #if (set_job_daemon $job, hostname . ":$$") {
148 if (1) {
149 get_logger->debug("Succesfully taken job $id");
150 process $id, $job;
151 get_logger->debug("Job $id done");
ab436d78 152 } else {
be3e5b32 153 get_logger->debug("Job $id already taken");
ab436d78 154 }
5c5cd38a
MG
155}
156
157sub run{
55e61ae8 158 require Gruntmaster::Data;
9a69bbad 159 $db = Gruntmaster::Data->connect($ENV{GRUNTMASTER_DSN} // 'dbi:Pg:');
a722431b
MG
160 Log::Log4perl->init('/etc/gruntmasterd/gruntmasterd-log.conf');
161 get_logger->info("gruntmasterd $VERSION started");
01d59515
MG
162 my $dir = tempdir 'gruntmasterd.XXXX', CLEANUP => 1, TMPDIR => 1;
163 chmod 0711, $dir;
164 chdir $dir;
be3e5b32
MG
165 while (1) {
166 my $job = $db->jobs->search({daemon => undef}, {rows => 1})->first;
167 got_job $job if defined $job;
168 sleep 2 unless defined $job;
169 }
5c5cd38a
MG
170}
171
1721;
173__END__
5c5cd38a
MG
174
175=head1 NAME
176
bc372959 177Gruntmaster::Daemon - Gruntmaster 6000 Online Judge -- daemon
5c5cd38a
MG
178
179=head1 SYNOPSIS
180
181 use Gruntmaster::Daemon;
bc372959 182 Gruntmaster::Daemon->run;
5c5cd38a
MG
183
184=head1 DESCRIPTION
185
bc372959 186Gruntmaster::Daemon is the daemon component of the Gruntmaster 6000 online judge.
5c5cd38a
MG
187
188=head1 AUTHOR
189
bc372959 190Marius Gavrilescu E<lt>marius@ieval.roE<gt>
5c5cd38a
MG
191
192=head1 COPYRIGHT AND LICENSE
193
bc372959 194Copyright (C) 2014 by Marius Gavrilescu
5c5cd38a 195
bc372959
MG
196This library is free software: you can redistribute it and/or modify
197it under the terms of the GNU Affero General Public License as published by
198the Free Software Foundation, either version 3 of the License, or
199(at your option) any later version.
5c5cd38a
MG
200
201
202=cut
This page took 0.10362 seconds and 4 git commands to generate.