Add Brainfuck
[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/;
5c5cd38a 11
ac964998 12use File::Slurp qw/read_file/;
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
6bf57d23 20use constant FORMAT_EXTENSION => {
fb85e61f 21 BRAINFUCK => 'bf',
6bf57d23
MG
22 C => 'c',
23 CPP => 'cpp',
5a4392d5
MG
24 GCCGO => 'go',
25 GOLANG => 'go',
f8b954a9 26 GOLFSCRIPT => 'gs',
5a4392d5 27 HASKELL => 'hs',
6bf57d23 28 JAVA => 'java',
6d1f2d94
S
29 JULIA => 'jl',
30 MONO => 'cs',
6bf57d23
MG
31 PASCAL => 'pas',
32 PERL => 'pl',
33 PYTHON => 'py',
c4d90e6f 34 RUBY => 'rb',
27be8560 35 RUST => 'rs',
c88da74d 36 SBCL => 'l',
6bf57d23 37};
adb44605 38
5c5cd38a
MG
39##################################################
40
b4ac22ff 41sub safe_can {
a722431b 42 my ($type, $sub, $name) = @_;
5c5cd38a 43
fcfcfb93 44 get_logger->logdie("Invalid \l$type: '$name'") unless $name =~ /^\w+$/s;
a722431b
MG
45 my $pkg = __PACKAGE__ . "::${type}::${name}";
46 eval "require $pkg" or get_logger->warn("Error while requiring $pkg: $@");
b4ac22ff 47 $pkg->can($sub) or get_logger->logdie("No such \l$type: '$name'");
5c5cd38a
MG
48}
49
ab436d78 50sub process{
867a9dad 51 my ($meta) = @_;
a722431b
MG
52
53 my @results;
54 my @full_results = ();
a722431b
MG
55 our $errors = '';
56 try {
a722431b
MG
57 prepare_files $meta;
58 chomp $errors;
59
99d37110 60 my ($files, $generator, $runner, $judge, $testcnt) = map { $meta->{$_} or die "Required parameter missing: $_\n"} qw/files generator runner judge testcnt/;
a722431b
MG
61
62 $generator = safe_can Generator => generate => $generator;
63 $runner = safe_can Runner => run => $runner;
64 $judge = safe_can Judge => judge => $judge;
65
66 for my $test (1 .. $testcnt) {
67 my $start_time = time;
68 my $result;
ac964998 69 unlink 'result';
a722431b
MG
70 try {
71 $generator->($test, $meta);
72 $result = $runner->($test, $meta);
73 } catch {
74 $result = $_;
75 unless (ref $result) {
76 chomp $result;
77 $result = [ERR, $result];
78 }
79 };
80
81 if (ref $result) {
ac964998
MG
82 my $result_text = $result->[1];
83 $result_text .= ': ' . read_file 'result' if -s 'result';
84 get_logger->trace("Test $test result is " . $result_text);
85 push @full_results, {id => $test, result => $result->[0], result_text => $result_text, time => time - $start_time}
a722431b
MG
86 } else {
87 get_logger->trace("Test $test result is $result");
88 push @full_results, {id => $test, result => 0, result_text => $result, time => time - $start_time}
89 }
90 push @results, $result;
91 last if $meta->{judge} eq 'Absolute' && ref $result
5c5cd38a 92 }
5c5cd38a 93
a722431b
MG
94 my %results = $judge->(@results);
95 $meta->{$_} = $results{$_} for keys %results;
7be5f054 96 $meta->{results} = \@full_results
a722431b 97 } catch {
99d37110 98 s/(.*) at .*/$1/s;
a722431b
MG
99 chomp;
100 $meta->{result} = -1;
101 $meta->{result_text} = $_;
102 };
fdb99417 103 stopvms;
867a9dad 104 $meta->{errors} = $errors;
a722431b 105
99d37110 106 get_logger->info('Job result: ' . $meta->{result_text});
867a9dad
MG
107}
108
109sub process_job {
110 my ($job) = @_;
fb7e1b7c 111 my $pb = db()->select('problems', '*', {id => $job->{problem}})->hash;
867a9dad 112 my $meta = {
6101c7ad 113 problem => $job->{problem},
867a9dad
MG
114 files => {
115 prog => {
6101c7ad
MG
116 name => 'prog.' . $job->{extension},
117 format => $job->{format},
118 content => $job->{source},
867a9dad
MG
119 },
120 },
6101c7ad 121 map { $_ => $pb->{$_} } qw/generator runner judge precnt testcnt timeout olimit/
867a9dad 122 };
6101c7ad
MG
123 my $timeout_override = db()->query('SELECT timeout FROM limits WHERE problem=? AND format=?', $job->{problem}, $job->{format})->list;
124 $meta->{timeout} = $timeout_override if defined $timeout_override;
125 $meta->{tests} = decode_json $pb->{tests} if $meta->{runner} eq 'File';
126 $job->{contest} &&= contest_entry($job->{contest});
7e81b08a 127 delete $meta->{precnt} unless $job->{contest} && $job->{contest}{started} && !$job->{contest}{finished}; ## no critic (ProhibitNegativeExpressionsInUnlessAndUntilConditions)
da822e3c 128 $meta->{testcnt} = $meta->{precnt} if $meta->{precnt};
867a9dad
MG
129
130 $meta->{files}{ver} = {
6101c7ad
MG
131 name => 'ver.' . FORMAT_EXTENSION->{$pb->{verformat}},
132 format => $pb->{verformat},
133 content => $pb->{versource},
134 } if $pb->{verformat};
867a9dad
MG
135
136 process $meta;
137
da822e3c 138 $meta->{result_text} .= ' (pretests)' if $meta->{precnt};
6101c7ad
MG
139 finish_job($job, $job->{private} || $pb->{private},
140 result => $meta->{result},
141 result_text => $meta->{result_text},
142 ($meta->{results} ? (results => encode_json $meta->{results}) : ()),
143 $meta->{errors} ? (errors => $meta->{errors}) : ());
ab436d78
MG
144}
145
146sub got_job{
be3e5b32 147 my $job = $_[0];
6101c7ad
MG
148 my $id = $job->{id};
149 get_logger->debug("Processing job $id...");
150 process_job $job;
151 get_logger->debug("Job $id done");
5c5cd38a
MG
152}
153
6101c7ad
MG
154my $daemon = hostname . ":$$";
155
5c5cd38a 156sub run{
55e61ae8 157 require Gruntmaster::Data;
6101c7ad 158 Gruntmaster::Data->import;
074cbf76 159 dbinit($ENV{GRUNTMASTER_DSN} // 'dbi:Pg:');
a722431b 160 Log::Log4perl->init('/etc/gruntmasterd/gruntmasterd-log.conf');
6101c7ad 161 get_logger->info("gruntmasterd $VERSION started ($daemon)");
01d59515
MG
162 my $dir = tempdir 'gruntmasterd.XXXX', CLEANUP => 1, TMPDIR => 1;
163 chmod 0711, $dir;
164 chdir $dir;
be3e5b32 165 while (1) {
6101c7ad 166 my $job = take_job($daemon);
be3e5b32
MG
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.039068 seconds and 4 git commands to generate.