Add SBCL
[gruntmaster-daemon.git] / lib / Gruntmaster / Daemon.pm
1 package Gruntmaster::Daemon;
2
3 use 5.014000;
4 use strict;
5 use warnings;
6
7 our $VERSION = '5999.000_004';
8
9 use Gruntmaster::Daemon::Constants qw/ERR/;
10 use Gruntmaster::Daemon::Format qw/prepare_files stopvms/;
11
12 use File::Temp qw/tempdir/;
13 use JSON qw/decode_json encode_json/;
14 use Sys::Hostname qw/hostname/;
15 use Time::HiRes qw/time/;
16 use Try::Tiny;
17 use Log::Log4perl qw/get_logger/;
18
19 use constant FORMAT_EXTENSION => {
20 C => 'c',
21 CPP => 'cpp',
22 GCCGO => 'go',
23 GOLANG => 'go',
24 HASKELL => 'hs',
25 MONO => 'cs',
26 JAVA => 'java',
27 PASCAL => 'pas',
28 PERL => 'pl',
29 PYTHON => 'py',
30 SBCL => 'l',
31 };
32
33 ##################################################
34
35 my $db;
36
37 sub safe_can {
38 my ($type, $sub, $name) = @_;
39
40 get_logger->logdie("Invalid \l$type: '$name'") unless $name =~ /^\w+$/s;
41 my $pkg = __PACKAGE__ . "::${type}::${name}";
42 eval "require $pkg" or get_logger->warn("Error while requiring $pkg: $@");
43 $pkg->can($sub) or get_logger->logdie("No such \l$type: '$name'");
44 }
45
46 sub process{
47 my ($meta) = @_;
48
49 my @results;
50 my @full_results = ();
51 our $errors = '';
52 try {
53 prepare_files $meta;
54 chomp $errors;
55
56 my ($files, $generator, $runner, $judge, $testcnt) = map { $meta->{$_} or die "Required parameter missing: $_\n"} qw/files generator runner judge testcnt/;
57
58 $generator = safe_can Generator => generate => $generator;
59 $runner = safe_can Runner => run => $runner;
60 $judge = safe_can Judge => judge => $judge;
61
62 for my $test (1 .. $testcnt) {
63 my $start_time = time;
64 my $result;
65 try {
66 $generator->($test, $meta);
67 $result = $runner->($test, $meta);
68 } catch {
69 $result = $_;
70 unless (ref $result) {
71 chomp $result;
72 $result = [ERR, $result];
73 }
74 };
75
76 if (ref $result) {
77 get_logger->trace("Test $test result is " . $result->[1]);
78 push @full_results, {id => $test, result => $result->[0], result_text => $result->[1], time => time - $start_time}
79 } else {
80 get_logger->trace("Test $test result is $result");
81 push @full_results, {id => $test, result => 0, result_text => $result, time => time - $start_time}
82 }
83 push @results, $result;
84 last if $meta->{judge} eq 'Absolute' && ref $result
85 }
86
87 my %results = $judge->(@results);
88 $meta->{$_} = $results{$_} for keys %results;
89 $meta->{results} = \@full_results
90 } catch {
91 s/(.*) at .*/$1/s;
92 chomp;
93 $meta->{result} = -1;
94 $meta->{result_text} = $_;
95 };
96 stopvms;
97 $meta->{errors} = $errors;
98
99 get_logger->info('Job result: ' . $meta->{result_text});
100 }
101
102 sub process_job {
103 my ($job) = @_;
104 my $meta = {
105 problem => $job->problem->id,
106 files => {
107 prog => {
108 name => 'prog.' . $job->extension,
109 format => $job->format,
110 content => $job->source,
111 },
112 },
113 map { $_ => $job->problem->get_column($_) } qw/generator runner judge precnt testcnt timeout olimit/
114 };
115 $meta->{tests} = decode_json $job->problem->tests if $meta->{runner} eq 'File';
116 delete $meta->{precnt} if $job->problem->is_in_archive;
117 $meta->{testcnt} = $meta->{precnt} if $meta->{precnt};
118
119 $meta->{files}{ver} = {
120 name => 'ver.' . FORMAT_EXTENSION->{$job->problem->verformat},
121 format => $job->problem->verformat,
122 content => $job->problem->versource,
123 } if $job->problem->verformat;
124
125 process $meta;
126
127 $meta->{result_text} .= ' (pretests)' if $meta->{precnt};
128 $job->update({
129 result => $meta->{result},
130 result_text => $meta->{result_text},
131 ($meta->{results} ? (results => encode_json $meta->{results}) : ()),
132 $meta->{errors} ? (errors => $meta->{errors}) : ()
133 });
134
135 my $solved = $meta->{result} ? 0 : 1;
136 my $status = $db->problem_statuses->find($job->owner, $job->problem);
137 $db->problem_statuses->update_or_create({
138 problem => $job->problem,
139 owner => $job->owner,
140 job => $job->id,
141 solved => $solved}) if !$job->problem->is_private;
142 }
143
144 sub got_job{
145 my $job = $_[0];
146 my $id = $job->id;
147 get_logger->debug("Taking job $id...");
148 my $daemon = hostname . ":$$";
149 $job->update({daemon => $daemon}, {where => {daemon => undef}})->discard_changes;
150 if ($job->daemon eq $daemon) {
151 get_logger->debug("Succesfully taken job $id");
152 process_job $job;
153 get_logger->debug("Job $id done");
154 } else {
155 get_logger->debug("Job $id already taken by " . $job->daemon);
156 }
157 }
158
159 sub run{
160 require Gruntmaster::Data;
161 $db = Gruntmaster::Data->connect($ENV{GRUNTMASTER_DSN} // 'dbi:Pg:');
162 Log::Log4perl->init('/etc/gruntmasterd/gruntmasterd-log.conf');
163 get_logger->info("gruntmasterd $VERSION started");
164 my $dir = tempdir 'gruntmasterd.XXXX', CLEANUP => 1, TMPDIR => 1;
165 chmod 0711, $dir;
166 chdir $dir;
167 while (1) {
168 my $job = $db->jobs->search({daemon => undef}, {rows => 1})->first;
169 got_job $job if defined $job;
170 sleep 2 unless defined $job;
171 }
172 }
173
174 1;
175 __END__
176
177 =head1 NAME
178
179 Gruntmaster::Daemon - Gruntmaster 6000 Online Judge -- daemon
180
181 =head1 SYNOPSIS
182
183 use Gruntmaster::Daemon;
184 Gruntmaster::Daemon->run;
185
186 =head1 DESCRIPTION
187
188 Gruntmaster::Daemon is the daemon component of the Gruntmaster 6000 online judge.
189
190 =head1 AUTHOR
191
192 Marius Gavrilescu E<lt>marius@ieval.roE<gt>
193
194 =head1 COPYRIGHT AND LICENSE
195
196 Copyright (C) 2014 by Marius Gavrilescu
197
198 This library is free software: you can redistribute it and/or modify
199 it under the terms of the GNU Affero General Public License as published by
200 the Free Software Foundation, either version 3 of the License, or
201 (at your option) any later version.
202
203
204 =cut
This page took 0.069472 seconds and 4 git commands to generate.