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 return 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'");
48 my ($job, $jobr) = @_;
51 my @full_results = ();
59 problem
=> $jobr->problem->id,
62 name
=> 'prog.' . $jobr->extension,
63 format
=> $jobr->format,
64 content
=> $jobr->source,
67 map { $_ => $jobr->problem->get_column($_) } qw
/generator runner judge testcnt timeout olimit/
69 $meta->{tests
} = decode_json
$jobr->problem->tests if $meta->{runner
} eq 'File';
71 $meta->{files
}{ver
} = {
72 name
=> 'ver.' . FORMAT_EXTENSION
->{$jobr->problem->verformat},
73 format
=> $jobr->problem->verformat,
74 content
=> $jobr->problem->versource,
75 } if $jobr->problem->verformat;
81 my ($files, $generator, $runner, $judge, $testcnt) = map { $meta->{$_} or die "Required parameter missing: $_\n"} qw
/files generator runner judge testcnt/;
83 $generator = safe_can Generator
=> generate
=> $generator;
84 $runner = safe_can Runner
=> run
=> $runner;
85 $judge = safe_can Judge
=> judge
=> $judge;
87 for my $test (1 .. $testcnt) {
88 my $start_time = time;
91 $generator->($test, $meta);
92 $result = $runner->($test, $meta);
95 unless (ref $result) {
97 $result = [ERR
, $result];
102 get_logger
->trace("Test $test result is " . $result->[1]);
103 push @full_results, {id
=> $test, result
=> $result->[0], result_text
=> $result->[1], time => time - $start_time}
105 get_logger
->trace("Test $test result is $result");
106 push @full_results, {id
=> $test, result
=> 0, result_text
=> $result, time => time - $start_time}
108 push @results, $result;
109 last if $meta->{judge
} eq 'Absolute' && ref $result
112 my %results = $judge->(@results);
113 $meta->{$_} = $results{$_} for keys %results;
114 $meta->{results
} = \
@full_results
118 $meta->{result
} = -1;
119 $meta->{result_text
} = $_;
123 get_logger
->info('Job result: ' . $meta->{result_text
});
126 result
=> $meta->{result
},
127 result_text
=> $meta->{result_text
},
128 ($meta->{results
} ?
(results
=> encode_json
$meta->{results
}) : ()),
129 $errors ?
(errors
=> $errors) : ()
132 my $solved = $meta->{result
} ?
0 : 1;
133 my $status = $db->problem_statuses->find($jobr->owner, $jobr->problem);
134 $db->problem_statuses->update_or_create({
135 problem
=> $jobr->problem,
136 owner
=> $jobr->owner,
138 solved
=> $solved}) if !$jobr->problem->is_private;
144 get_logger
->debug("Taking job $id...");
145 my $daemon = hostname
. ":$$";
146 $job->update({daemon
=> $daemon});
147 #if (set_job_daemon $job, hostname . ":$$") {
149 get_logger
->debug("Succesfully taken job $id");
151 get_logger
->debug("Job $id done");
153 get_logger
->debug("Job $id already taken");
158 require Gruntmaster
::Data
;
159 $db = Gruntmaster
::Data
->connect($ENV{GRUNTMASTER_DSN
} // 'dbi:Pg:');
160 Log
::Log4perl
->init('/etc/gruntmasterd/gruntmasterd-log.conf');
161 get_logger
->info("gruntmasterd $VERSION started");
162 my $dir = tempdir
'gruntmasterd.XXXX', CLEANUP
=> 1, TMPDIR
=> 1;
166 my $job = $db->jobs->search({daemon
=> undef}, {rows
=> 1})->first;
167 got_job
$job if defined $job;
168 sleep 2 unless defined $job;
177 Gruntmaster::Daemon - Gruntmaster 6000 Online Judge -- daemon
181 use Gruntmaster::Daemon;
182 Gruntmaster::Daemon->run;
186 Gruntmaster::Daemon is the daemon component of the Gruntmaster 6000 online judge.
190 Marius Gavrilescu E<lt>marius@ieval.roE<gt>
192 =head1 COPYRIGHT AND LICENSE
194 Copyright (C) 2014 by Marius Gavrilescu
196 This library is free software: you can redistribute it and/or modify
197 it under the terms of the GNU Affero General Public License as published by
198 the Free Software Foundation, either version 3 of the License, or
199 (at your option) any later version.