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
::Basename qw
/fileparse/;
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 PAGE_SIZE
=> 10;
21 use constant FORMAT_EXTENSION
=> {
34 ##################################################
41 my ($type, $sub, $name) = @_;
43 return unless $name =~ /^\w+$/s;
44 my $pkg = __PACKAGE__
. "::${type}::${name}";
45 eval "require $pkg" or get_logger
->warn("Error while requiring $pkg: $@");
50 my ($type, $sub, $name) = @_;
52 safe_can_nodie
@_ or get_logger
->logdie("No such \l$type: '$name'");
56 my ($job, $jobr) = @_;
59 my @full_results = ();
67 problem
=> $jobr->problem->id,
70 name
=> 'prog.' . $jobr->extension,
71 format
=> $jobr->format,
72 content
=> $jobr->source,
75 map { $_ => $jobr->problem->get_column($_) } qw
/generator runner judge testcnt timeout olimit/
77 $meta->{tests
} = decode_json
$jobr->problem->tests if $meta->{runner
} eq 'File';
79 $meta->{files
}{ver
} = {
80 name
=> 'ver.' . FORMAT_EXTENSION
->{$jobr->problem->verformat},
81 format
=> $jobr->problem->verformat,
82 content
=> $jobr->problem->versource,
83 } if $jobr->problem->verformat;
89 my ($files, $generator, $runner, $judge, $testcnt) = map { $meta->{$_} or die "Required parameter missing: $_\n"} qw
/files generator runner judge testcnt/;
91 $generator = safe_can Generator
=> generate
=> $generator;
92 $runner = safe_can Runner
=> run
=> $runner;
93 $judge = safe_can Judge
=> judge
=> $judge;
95 for my $test (1 .. $testcnt) {
96 my $start_time = time;
99 $generator->($test, $meta);
100 $result = $runner->($test, $meta);
103 unless (ref $result) {
105 $result = [ERR
, $result];
110 get_logger
->trace("Test $test result is " . $result->[1]);
111 push @full_results, {id
=> $test, result
=> $result->[0], result_text
=> $result->[1], time => time - $start_time}
113 get_logger
->trace("Test $test result is $result");
114 push @full_results, {id
=> $test, result
=> 0, result_text
=> $result, time => time - $start_time}
116 push @results, $result;
117 last if $meta->{judge
} eq 'Absolute' && ref $result
120 my %results = $judge->(@results);
121 $meta->{$_} = $results{$_} for keys %results;
122 $meta->{results
} = \
@full_results
126 $meta->{result
} = -1;
127 $meta->{result_text
} = $_;
131 get_logger
->info('Job result: ' . $meta->{result_text
});
134 result
=> $meta->{result
},
135 result_text
=> $meta->{result_text
},
136 ($meta->{results
} ?
(results
=> encode_json
$meta->{results
}) : ()),
137 $errors ?
(errors
=> $errors) : ()
140 my $solved = $meta->{result
} ?
0 : 1;
141 my $status = $db->problem_statuses->find($jobr->owner, $jobr->problem);
142 $db->problem_statuses->update_or_create({
143 problem
=> $jobr->problem,
144 owner
=> $jobr->owner,
150 purge
'/st/' . $jobr->contest if $jobr->contest;
152 purge
'/us/' . $jobr->owner;
158 get_logger
->debug("Taking job $id...");
159 my $daemon = hostname
. ":$$";
160 $job->update({daemon
=> $daemon});
161 #if (set_job_daemon $job, hostname . ":$$") {
163 get_logger
->debug("Succesfully taken job $id");
165 get_logger
->debug("Job $id done");
167 get_logger
->debug("Job $id already taken");
172 warn "No GRUNTMASTER_VM environment variable. Running without a VM is a security risk.\n" unless $ENV{GRUNTMASTER_VM
};
173 require Gruntmaster
::Data
;
174 $db = Gruntmaster
::Data
->connect('dbi:Pg:');
175 Log
::Log4perl
->init('/etc/gruntmasterd/gruntmasterd-log.conf');
176 get_logger
->info("gruntmasterd $VERSION started");
177 chdir tempdir
'gruntmasterd.XXXX', CLEANUP
=> 1, TMPDIR
=> 1;
179 my $job = $db->jobs->search({daemon
=> undef}, {rows
=> 1})->first;
180 got_job
$job if defined $job;
181 sleep 2 unless defined $job;
190 Gruntmaster::Daemon - Gruntmaster 6000 Online Judge -- daemon
194 use Gruntmaster::Daemon;
195 Gruntmaster::Daemon->run;
199 Gruntmaster::Daemon is the daemon component of the Gruntmaster 6000 online judge.
203 Marius Gavrilescu E<lt>marius@ieval.roE<gt>
205 =head1 COPYRIGHT AND LICENSE
207 Copyright (C) 2014 by Marius Gavrilescu
209 This library is free software: you can redistribute it and/or modify
210 it under the terms of the GNU Affero General Public License as published by
211 the Free Software Foundation, either version 3 of the License, or
212 (at your option) any later version.