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+$/;
45 my $pkg = __PACKAGE__
. "::${type}::${name}";
46 eval "require $pkg" or get_logger
->warn("Error while requiring $pkg: $@");
51 my ($type, $sub, $name) = @_;
53 safe_can_nodie
@_ or get_logger
->logdie("No such \l$type: '$name'");
57 my ($job, $jobr) = @_;
60 my @full_results = ();
68 problem
=> $jobr->problem->id,
71 name
=> 'prog.' . $jobr->extension,
72 format
=> $jobr->format,
73 content
=> $jobr->source,
76 map { $_ => $jobr->problem->get_column($_) } qw
/generator runner judge testcnt timeout olimit/
78 $meta->{tests
} = decode_json
$jobr->problem->tests if $meta->{runner
} eq 'File';
80 $meta->{files
}{ver
} = {
81 name
=> 'ver.' . FORMAT_EXTENSION
->{$jobr->problem->verformat},
82 format
=> $jobr->problem->verformat,
83 content
=> $jobr->problem->versource,
84 } if $jobr->problem->verformat;
90 my ($files, $generator, $runner, $judge, $testcnt) = map { $meta->{$_} or die "Required parameter missing: $_"} qw
/files generator runner judge testcnt/;
92 $generator = safe_can Generator
=> generate
=> $generator;
93 $runner = safe_can Runner
=> run
=> $runner;
94 $judge = safe_can Judge
=> judge
=> $judge;
96 for my $test (1 .. $testcnt) {
97 my $start_time = time;
100 $generator->($test, $meta);
101 $result = $runner->($test, $meta);
104 unless (ref $result) {
106 $result = [ERR
, $result];
111 get_logger
->trace("Test $test result is " . $result->[1]);
112 push @full_results, {id
=> $test, result
=> $result->[0], result_text
=> $result->[1], time => time - $start_time}
114 get_logger
->trace("Test $test result is $result");
115 push @full_results, {id
=> $test, result
=> 0, result_text
=> $result, time => time - $start_time}
117 push @results, $result;
118 last if $meta->{judge
} eq 'Absolute' && ref $result
121 my %results = $judge->(@results);
122 $meta->{$_} = $results{$_} for keys %results;
123 $meta->{results
} = \
@full_results
127 $meta->{result
} = -1;
128 $meta->{result_text
} = $_;
132 get_logger
->info("Job result: " . $meta->{result_text
});
135 result
=> $meta->{result
},
136 result_text
=> $meta->{result_text
},
137 ($meta->{results
} ?
(results
=> encode_json
$meta->{results
}) : ()),
138 $errors ?
(errors
=> $errors) : ()
141 my $solved = $meta->{result
} ?
0 : 1;
142 my $status = $db->problem_statuses->find($jobr->owner, $jobr->problem);
143 $db->problem_statuses->update_or_create({
144 problem
=> $jobr->problem,
145 owner
=> $jobr->owner,
151 purge
'/st/' . $jobr->contest if $jobr->contest;
153 purge
'/us/' . $jobr->owner;
159 get_logger
->debug("Taking job $id...");
160 my $daemon = hostname
. ":$$";
161 $job->update({daemon
=> $daemon});
162 #if (set_job_daemon $job, hostname . ":$$") {
164 get_logger
->debug("Succesfully taken job $id");
166 get_logger
->debug("Job $id done");
168 get_logger
->debug("Job $id already taken");
173 warn "No GRUNTMASTER_VM environment variable. Running without a VM is a security risk.\n" unless $ENV{GRUNTMASTER_VM
};
174 require Gruntmaster
::Data
;
175 $db = Gruntmaster
::Data
->connect('dbi:Pg:');
176 Log
::Log4perl
->init('/etc/gruntmasterd/gruntmasterd-log.conf');
177 get_logger
->info("gruntmasterd $VERSION started");
178 chdir tempdir
'gruntmasterd.XXXX', CLEANUP
=> 1, TMPDIR
=> 1;
180 my $job = $db->jobs->search({daemon
=> undef}, {rows
=> 1})->first;
181 got_job
$job if defined $job;
182 sleep 2 unless defined $job;
191 Gruntmaster::Daemon - Gruntmaster 6000 Online Judge -- daemon
195 use Gruntmaster::Daemon;
196 Gruntmaster::Daemon->run;
200 Gruntmaster::Daemon is the daemon component of the Gruntmaster 6000 online judge.
204 Marius Gavrilescu E<lt>marius@ieval.roE<gt>
206 =head1 COPYRIGHT AND LICENSE
208 Copyright (C) 2014 by Marius Gavrilescu
210 This library is free software: you can redistribute it and/or modify
211 it under the terms of the GNU Affero General Public License as published by
212 the Free Software Foundation, either version 3 of the License, or
213 (at your option) any later version.