1 package Gruntmaster
::Daemon
;
7 our $VERSION = '5999.000_004';
9 use Gruntmaster
::Daemon
::Constants qw
/ERR/;
10 use Gruntmaster
::Daemon
::Format qw
/prepare_files stopvms/;
12 use File
::Slurp qw
/read_file/;
13 use File
::Temp qw
/tempdir/;
14 use JSON qw
/decode_json encode_json/;
15 use Sys
::Hostname qw
/hostname/;
16 use Time
::HiRes qw
/time/;
18 use Log
::Log4perl qw
/get_logger/;
20 use constant FORMAT_EXTENSION
=> {
35 ##################################################
40 my ($type, $sub, $name) = @_;
42 get_logger
->logdie("Invalid \l$type: '$name'") unless $name =~ /^\w+$/s;
43 my $pkg = __PACKAGE__
. "::${type}::${name}";
44 eval "require $pkg" or get_logger
->warn("Error while requiring $pkg: $@");
45 $pkg->can($sub) or get_logger
->logdie("No such \l$type: '$name'");
52 my @full_results = ();
58 my ($files, $generator, $runner, $judge, $testcnt) = map { $meta->{$_} or die "Required parameter missing: $_\n"} qw
/files generator runner judge testcnt/;
60 $generator = safe_can Generator
=> generate
=> $generator;
61 $runner = safe_can Runner
=> run
=> $runner;
62 $judge = safe_can Judge
=> judge
=> $judge;
64 for my $test (1 .. $testcnt) {
65 my $start_time = time;
69 $generator->($test, $meta);
70 $result = $runner->($test, $meta);
73 unless (ref $result) {
75 $result = [ERR
, $result];
80 my $result_text = $result->[1];
81 $result_text .= ': ' . read_file
'result' if -s
'result';
82 get_logger
->trace("Test $test result is " . $result_text);
83 push @full_results, {id
=> $test, result
=> $result->[0], result_text
=> $result_text, time => time - $start_time}
85 get_logger
->trace("Test $test result is $result");
86 push @full_results, {id
=> $test, result
=> 0, result_text
=> $result, time => time - $start_time}
88 push @results, $result;
89 last if $meta->{judge
} eq 'Absolute' && ref $result
92 my %results = $judge->(@results);
93 $meta->{$_} = $results{$_} for keys %results;
94 $meta->{results
} = \
@full_results
99 $meta->{result_text
} = $_;
102 $meta->{errors
} = $errors;
104 get_logger
->info('Job result: ' . $meta->{result_text
});
110 problem
=> $job->problem->id,
113 name
=> 'prog.' . $job->extension,
114 format
=> $job->format,
115 content
=> $job->source,
118 map { $_ => $job->problem->get_column($_) } qw
/generator runner judge precnt testcnt timeout olimit/
120 $meta->{tests
} = decode_json
$job->problem->tests if $meta->{runner
} eq 'File';
121 delete $meta->{precnt
} unless $job->contest;
122 $meta->{testcnt
} = $meta->{precnt
} if $meta->{precnt
};
124 $meta->{files
}{ver
} = {
125 name
=> 'ver.' . FORMAT_EXTENSION
->{$job->problem->verformat},
126 format
=> $job->problem->verformat,
127 content
=> $job->problem->versource,
128 } if $job->problem->verformat;
132 $meta->{result_text
} .= ' (pretests)' if $meta->{precnt
};
134 result
=> $meta->{result
},
135 result_text
=> $meta->{result_text
},
136 ($meta->{results
} ?
(results
=> encode_json
$meta->{results
}) : ()),
137 $meta->{errors
} ?
(errors
=> $meta->{errors
}) : ()
140 my $solved = $meta->{result
} ?
0 : 1;
141 my $status = $db->problem_statuses->find($job->owner, $job->problem);
142 $db->problem_statuses->update_or_create({
143 problem
=> $job->problem,
144 owner
=> $job->owner,
146 solved
=> $solved}) if !$job->problem->private;
152 get_logger
->debug("Taking job $id...");
153 my $daemon = hostname
. ":$$";
154 $job->update({daemon
=> $daemon}, {where
=> {daemon
=> undef}})->discard_changes;
155 if ($job->daemon eq $daemon) {
156 get_logger
->debug("Succesfully taken job $id");
158 get_logger
->debug("Job $id done");
160 get_logger
->debug("Job $id already taken by " . $job->daemon);
165 require Gruntmaster
::Data
;
166 $db = Gruntmaster
::Data
->connect($ENV{GRUNTMASTER_DSN
} // 'dbi:Pg:');
167 Log
::Log4perl
->init('/etc/gruntmasterd/gruntmasterd-log.conf');
168 get_logger
->info("gruntmasterd $VERSION started");
169 my $dir = tempdir
'gruntmasterd.XXXX', CLEANUP
=> 1, TMPDIR
=> 1;
173 my $job = $db->jobs->search({daemon
=> undef}, {rows
=> 1})->first;
174 got_job
$job if defined $job;
175 sleep 2 unless defined $job;
184 Gruntmaster::Daemon - Gruntmaster 6000 Online Judge -- daemon
188 use Gruntmaster::Daemon;
189 Gruntmaster::Daemon->run;
193 Gruntmaster::Daemon is the daemon component of the Gruntmaster 6000 online judge.
197 Marius Gavrilescu E<lt>marius@ieval.roE<gt>
199 =head1 COPYRIGHT AND LICENSE
201 Copyright (C) 2014 by Marius Gavrilescu
203 This library is free software: you can redistribute it and/or modify
204 it under the terms of the GNU Affero General Public License as published by
205 the Free Software Foundation, either version 3 of the License, or
206 (at your option) any later version.