]> iEval git - gruntmaster-daemon.git/blame - lib/Gruntmaster/Daemon.pm
Some test fixing on os x
[gruntmaster-daemon.git] / lib / Gruntmaster / Daemon.pm
CommitLineData
5c5cd38a
MG
1package Gruntmaster::Daemon;
2
3use 5.014000;
4use strict;
5use warnings;
6
3fa65372 7our $VERSION = '5999.000_005';
5c5cd38a
MG
8
9use Gruntmaster::Daemon::Constants qw/ERR/;
fdb99417 10use Gruntmaster::Daemon::Format qw/prepare_files stopvms/;
f58c979a 11use Gruntmaster::SendResults qw/send_results_request/;
5c5cd38a 12
ac964998 13use File::Slurp qw/read_file/;
ab436d78 14use File::Temp qw/tempdir/;
be3e5b32 15use JSON qw/decode_json encode_json/;
ab436d78 16use Sys::Hostname qw/hostname/;
5c5cd38a 17use Try::Tiny;
5c5cd38a
MG
18use Log::Log4perl qw/get_logger/;
19
6bf57d23 20use constant FORMAT_EXTENSION => {
fb85e61f 21 BRAINFUCK => 'bf',
6bf57d23
MG
22 C => 'c',
23 CPP => 'cpp',
49b65824 24 D => 'd',
5a4392d5
MG
25 GCCGO => 'go',
26 GOLANG => 'go',
f8b954a9 27 GOLFSCRIPT => 'gs',
5a4392d5 28 HASKELL => 'hs',
6bf57d23 29 JAVA => 'java',
6d1f2d94
S
30 JULIA => 'jl',
31 MONO => 'cs',
0220f51a 32 OBERON => 'm',
34e16771 33 OCAML => 'ml',
6bf57d23
MG
34 PASCAL => 'pas',
35 PERL => 'pl',
8473542d 36 PHP => 'php',
6bf57d23 37 PYTHON => 'py',
ab4d4022 38 PYTHON3 => 'py',
c4d90e6f 39 RUBY => 'rb',
27be8560 40 RUST => 'rs',
c88da74d 41 SBCL => 'l',
6bf57d23 42};
adb44605 43
5c5cd38a
MG
44##################################################
45
b4ac22ff 46sub safe_can {
a722431b 47 my ($type, $sub, $name) = @_;
5c5cd38a 48
fcfcfb93 49 get_logger->logdie("Invalid \l$type: '$name'") unless $name =~ /^\w+$/s;
a722431b
MG
50 my $pkg = __PACKAGE__ . "::${type}::${name}";
51 eval "require $pkg" or get_logger->warn("Error while requiring $pkg: $@");
b4ac22ff 52 $pkg->can($sub) or get_logger->logdie("No such \l$type: '$name'");
5c5cd38a
MG
53}
54
ab436d78 55sub process{
867a9dad 56 my ($meta) = @_;
a722431b
MG
57
58 my @results;
59 my @full_results = ();
a722431b
MG
60 our $errors = '';
61 try {
a722431b
MG
62 prepare_files $meta;
63 chomp $errors;
64
99d37110 65 my ($files, $generator, $runner, $judge, $testcnt) = map { $meta->{$_} or die "Required parameter missing: $_\n"} qw/files generator runner judge testcnt/;
a722431b
MG
66
67 $generator = safe_can Generator => generate => $generator;
68 $runner = safe_can Runner => run => $runner;
69 $judge = safe_can Judge => judge => $judge;
70
71 for my $test (1 .. $testcnt) {
a722431b 72 my $result;
ac964998 73 unlink 'result';
a722431b
MG
74 try {
75 $generator->($test, $meta);
76 $result = $runner->($test, $meta);
77 } catch {
78 $result = $_;
79 unless (ref $result) {
80 chomp $result;
81 $result = [ERR, $result];
82 }
83 };
84
34cc90c9
MG
85 my $time = '?';
86 $time = (read_file 'time-stop-prog') - (read_file 'time-start-prog') if -f 'time-stop-prog' && -f 'time-start-prog';
87 unlink 'time-start-prog', 'time-stop-prog';
a722431b 88 if (ref $result) {
ac964998
MG
89 my $result_text = $result->[1];
90 $result_text .= ': ' . read_file 'result' if -s 'result';
91 get_logger->trace("Test $test result is " . $result_text);
34cc90c9 92 push @full_results, {id => $test, result => $result->[0], result_text => $result_text, time => $time}
a722431b
MG
93 } else {
94 get_logger->trace("Test $test result is $result");
34cc90c9 95 push @full_results, {id => $test, result => 0, result_text => $result, time => $time}
a722431b
MG
96 }
97 push @results, $result;
98 last if $meta->{judge} eq 'Absolute' && ref $result
5c5cd38a 99 }
5c5cd38a 100
a722431b
MG
101 my %results = $judge->(@results);
102 $meta->{$_} = $results{$_} for keys %results;
7be5f054 103 $meta->{results} = \@full_results
a722431b 104 } catch {
99d37110 105 s/(.*) at .*/$1/s;
a722431b
MG
106 chomp;
107 $meta->{result} = -1;
108 $meta->{result_text} = $_;
109 };
fdb99417 110 stopvms;
867a9dad 111 $meta->{errors} = $errors;
a722431b 112
99d37110 113 get_logger->info('Job result: ' . $meta->{result_text});
f58c979a 114 send_results_request($meta->{job_id}, $meta->{result}, $meta->{result_text});
867a9dad
MG
115}
116
117sub process_job {
118 my ($job) = @_;
fb7e1b7c 119 my $pb = db()->select('problems', '*', {id => $job->{problem}})->hash;
867a9dad 120 my $meta = {
6101c7ad 121 problem => $job->{problem},
867a9dad
MG
122 files => {
123 prog => {
6101c7ad
MG
124 name => 'prog.' . $job->{extension},
125 format => $job->{format},
126 content => $job->{source},
867a9dad
MG
127 },
128 },
6101c7ad 129 map { $_ => $pb->{$_} } qw/generator runner judge precnt testcnt timeout olimit/
867a9dad 130 };
6101c7ad
MG
131 my $timeout_override = db()->query('SELECT timeout FROM limits WHERE problem=? AND format=?', $job->{problem}, $job->{format})->list;
132 $meta->{timeout} = $timeout_override if defined $timeout_override;
133 $meta->{tests} = decode_json $pb->{tests} if $meta->{runner} eq 'File';
e172b8d9
MG
134 my $contest = $job->{contest} && contest_entry($job->{contest});
135 delete $meta->{precnt} unless $contest && $contest->{started} && !$contest->{finished}; ## no critic (ProhibitNegativeExpressionsInUnlessAndUntilConditions)
da822e3c 136 $meta->{testcnt} = $meta->{precnt} if $meta->{precnt};
867a9dad
MG
137
138 $meta->{files}{ver} = {
6101c7ad
MG
139 name => 'ver.' . FORMAT_EXTENSION->{$pb->{verformat}},
140 format => $pb->{verformat},
141 content => $pb->{versource},
142 } if $pb->{verformat};
f58c979a 143 $meta->{job_id} = $job->{id};
867a9dad
MG
144
145 process $meta;
146
da822e3c 147 $meta->{result_text} .= ' (pretests)' if $meta->{precnt};
6101c7ad
MG
148 finish_job($job, $job->{private} || $pb->{private},
149 result => $meta->{result},
150 result_text => $meta->{result_text},
151 ($meta->{results} ? (results => encode_json $meta->{results}) : ()),
152 $meta->{errors} ? (errors => $meta->{errors}) : ());
ab436d78
MG
153}
154
155sub got_job{
be3e5b32 156 my $job = $_[0];
6101c7ad
MG
157 my $id = $job->{id};
158 get_logger->debug("Processing job $id...");
159 process_job $job;
160 get_logger->debug("Job $id done");
5c5cd38a
MG
161}
162
6101c7ad
MG
163my $daemon = hostname . ":$$";
164
5c5cd38a 165sub run{
55e61ae8 166 require Gruntmaster::Data;
6101c7ad 167 Gruntmaster::Data->import;
074cbf76 168 dbinit($ENV{GRUNTMASTER_DSN} // 'dbi:Pg:');
a722431b 169 Log::Log4perl->init('/etc/gruntmasterd/gruntmasterd-log.conf');
6101c7ad 170 get_logger->info("gruntmasterd $VERSION started ($daemon)");
01d59515
MG
171 my $dir = tempdir 'gruntmasterd.XXXX', CLEANUP => 1, TMPDIR => 1;
172 chmod 0711, $dir;
173 chdir $dir;
be3e5b32 174 while (1) {
6101c7ad 175 my $job = take_job($daemon);
be3e5b32
MG
176 got_job $job if defined $job;
177 sleep 2 unless defined $job;
178 }
5c5cd38a
MG
179}
180
1811;
182__END__
5c5cd38a
MG
183
184=head1 NAME
185
bc372959 186Gruntmaster::Daemon - Gruntmaster 6000 Online Judge -- daemon
5c5cd38a
MG
187
188=head1 SYNOPSIS
189
190 use Gruntmaster::Daemon;
bc372959 191 Gruntmaster::Daemon->run;
5c5cd38a
MG
192
193=head1 DESCRIPTION
194
bc372959 195Gruntmaster::Daemon is the daemon component of the Gruntmaster 6000 online judge.
5c5cd38a
MG
196
197=head1 AUTHOR
198
bc372959 199Marius Gavrilescu E<lt>marius@ieval.roE<gt>
5c5cd38a
MG
200
201=head1 COPYRIGHT AND LICENSE
202
bc372959 203Copyright (C) 2014 by Marius Gavrilescu
5c5cd38a 204
bc372959
MG
205This library is free software: you can redistribute it and/or modify
206it under the terms of the GNU Affero General Public License as published by
207the Free Software Foundation, either version 3 of the License, or
208(at your option) any later version.
5c5cd38a
MG
209
210
211=cut
This page took 0.087433 seconds and 4 git commands to generate.