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