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 ##################################################
39 my ($type, $sub, $name) = @_;
41 get_logger
->logdie("Invalid \l$type: '$name'") unless $name =~ /^\w+$/s;
42 my $pkg = __PACKAGE__
. "::${type}::${name}";
43 eval "require $pkg" or get_logger
->warn("Error while requiring $pkg: $@");
44 $pkg->can($sub) or get_logger
->logdie("No such \l$type: '$name'");
51 my @full_results = ();
57 my ($files, $generator, $runner, $judge, $testcnt) = map { $meta->{$_} or die "Required parameter missing: $_\n"} qw
/files generator runner judge testcnt/;
59 $generator = safe_can Generator
=> generate
=> $generator;
60 $runner = safe_can Runner
=> run
=> $runner;
61 $judge = safe_can Judge
=> judge
=> $judge;
63 for my $test (1 .. $testcnt) {
64 my $start_time = time;
67 $generator->($test, $meta);
68 $result = $runner->($test, $meta);
71 unless (ref $result) {
73 $result = [ERR
, $result];
78 get_logger
->trace("Test $test result is " . $result->[1]);
79 push @full_results, {id
=> $test, result
=> $result->[0], result_text
=> $result->[1], time => time - $start_time}
81 get_logger
->trace("Test $test result is $result");
82 push @full_results, {id
=> $test, result
=> 0, result_text
=> $result, time => time - $start_time}
84 push @results, $result;
85 last if $meta->{judge
} eq 'Absolute' && ref $result
88 my %results = $judge->(@results);
89 $meta->{$_} = $results{$_} for keys %results;
90 $meta->{results
} = \
@full_results
95 $meta->{result_text
} = $_;
98 $meta->{errors
} = $errors;
100 get_logger
->info('Job result: ' . $meta->{result_text
});
106 problem
=> $job->problem->id,
109 name
=> 'prog.' . $job->extension,
110 format
=> $job->format,
111 content
=> $job->source,
114 map { $_ => $job->problem->get_column($_) } qw
/generator runner judge testcnt timeout olimit/
116 $meta->{tests
} = decode_json
$job->problem->tests if $meta->{runner
} eq 'File';
118 $meta->{files
}{ver
} = {
119 name
=> 'ver.' . FORMAT_EXTENSION
->{$job->problem->verformat},
120 format
=> $job->problem->verformat,
121 content
=> $job->problem->versource,
122 } if $job->problem->verformat;
127 result
=> $meta->{result
},
128 result_text
=> $meta->{result_text
},
129 ($meta->{results
} ?
(results
=> encode_json
$meta->{results
}) : ()),
130 $meta->{errors
} ?
(errors
=> $meta->{errors
}) : ()
133 my $solved = $meta->{result
} ?
0 : 1;
134 my $status = $db->problem_statuses->find($job->owner, $job->problem);
135 $db->problem_statuses->update_or_create({
136 problem
=> $job->problem,
137 owner
=> $job->owner,
139 solved
=> $solved}) if !$job->problem->is_private;
145 get_logger
->debug("Taking job $id...");
146 my $daemon = hostname
. ":$$";
147 $job->update({daemon
=> $daemon});
148 #if (set_job_daemon $job, hostname . ":$$") {
150 get_logger
->debug("Succesfully taken job $id");
152 get_logger
->debug("Job $id done");
154 get_logger
->debug("Job $id already taken");
159 require Gruntmaster
::Data
;
160 $db = Gruntmaster
::Data
->connect($ENV{GRUNTMASTER_DSN
} // 'dbi:Pg:');
161 Log
::Log4perl
->init('/etc/gruntmasterd/gruntmasterd-log.conf');
162 get_logger
->info("gruntmasterd $VERSION started");
163 my $dir = tempdir
'gruntmasterd.XXXX', CLEANUP
=> 1, TMPDIR
=> 1;
167 my $job = $db->jobs->search({daemon
=> undef}, {rows
=> 1})->first;
168 got_job
$job if defined $job;
169 sleep 2 unless defined $job;
178 Gruntmaster::Daemon - Gruntmaster 6000 Online Judge -- daemon
182 use Gruntmaster::Daemon;
183 Gruntmaster::Daemon->run;
187 Gruntmaster::Daemon is the daemon component of the Gruntmaster 6000 online judge.
191 Marius Gavrilescu E<lt>marius@ieval.roE<gt>
193 =head1 COPYRIGHT AND LICENSE
195 Copyright (C) 2014 by Marius Gavrilescu
197 This library is free software: you can redistribute it and/or modify
198 it under the terms of the GNU Affero General Public License as published by
199 the Free Software Foundation, either version 3 of the License, or
200 (at your option) any later version.