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
=> {
36 ##################################################
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;
68 $generator->($test, $meta);
69 $result = $runner->($test, $meta);
72 unless (ref $result) {
74 $result = [ERR
, $result];
79 my $result_text = $result->[1];
80 $result_text .= ': ' . read_file
'result' if -s
'result';
81 get_logger
->trace("Test $test result is " . $result_text);
82 push @full_results, {id
=> $test, result
=> $result->[0], result_text
=> $result_text, time => time - $start_time}
84 get_logger
->trace("Test $test result is $result");
85 push @full_results, {id
=> $test, result
=> 0, result_text
=> $result, time => time - $start_time}
87 push @results, $result;
88 last if $meta->{judge
} eq 'Absolute' && ref $result
91 my %results = $judge->(@results);
92 $meta->{$_} = $results{$_} for keys %results;
93 $meta->{results
} = \
@full_results
98 $meta->{result_text
} = $_;
101 $meta->{errors
} = $errors;
103 get_logger
->info('Job result: ' . $meta->{result_text
});
108 my $pb = db
()->select('problems', '*', {id
=> $job->{problem
}})->hash;
110 problem
=> $job->{problem
},
113 name
=> 'prog.' . $job->{extension
},
114 format
=> $job->{format
},
115 content
=> $job->{source
},
118 map { $_ => $pb->{$_} } qw
/generator runner judge precnt testcnt timeout olimit/
120 my $timeout_override = db
()->query('SELECT timeout FROM limits WHERE problem=? AND format=?', $job->{problem
}, $job->{format
})->list;
121 $meta->{timeout
} = $timeout_override if defined $timeout_override;
122 $meta->{tests
} = decode_json
$pb->{tests
} if $meta->{runner
} eq 'File';
123 $job->{contest
} &&= contest_entry
($job->{contest
});
124 delete $meta->{precnt
} unless $job->{contest
} && $job->{contest
}{started
} && !$job->{contest
}{finished
}; ## no critic (ProhibitNegativeExpressionsInUnlessAndUntilConditions)
125 $meta->{testcnt
} = $meta->{precnt
} if $meta->{precnt
};
127 $meta->{files
}{ver
} = {
128 name
=> 'ver.' . FORMAT_EXTENSION
->{$pb->{verformat
}},
129 format
=> $pb->{verformat
},
130 content
=> $pb->{versource
},
131 } if $pb->{verformat
};
135 $meta->{result_text
} .= ' (pretests)' if $meta->{precnt
};
136 finish_job
($job, $job->{private
} || $pb->{private
},
137 result
=> $meta->{result
},
138 result_text
=> $meta->{result_text
},
139 ($meta->{results
} ?
(results
=> encode_json
$meta->{results
}) : ()),
140 $meta->{errors
} ?
(errors
=> $meta->{errors
}) : ());
146 get_logger
->debug("Processing job $id...");
148 get_logger
->debug("Job $id done");
151 my $daemon = hostname
. ":$$";
154 require Gruntmaster
::Data
;
155 Gruntmaster
::Data
->import;
156 dbinit
($ENV{GRUNTMASTER_DSN
} // 'dbi:Pg:');
157 Log
::Log4perl
->init('/etc/gruntmasterd/gruntmasterd-log.conf');
158 get_logger
->info("gruntmasterd $VERSION started ($daemon)");
159 my $dir = tempdir
'gruntmasterd.XXXX', CLEANUP
=> 1, TMPDIR
=> 1;
163 my $job = take_job
($daemon);
164 got_job
$job if defined $job;
165 sleep 2 unless defined $job;
174 Gruntmaster::Daemon - Gruntmaster 6000 Online Judge -- daemon
178 use Gruntmaster::Daemon;
179 Gruntmaster::Daemon->run;
183 Gruntmaster::Daemon is the daemon component of the Gruntmaster 6000 online judge.
187 Marius Gavrilescu E<lt>marius@ieval.roE<gt>
189 =head1 COPYRIGHT AND LICENSE
191 Copyright (C) 2014 by Marius Gavrilescu
193 This library is free software: you can redistribute it and/or modify
194 it under the terms of the GNU Affero General Public License as published by
195 the Free Software Foundation, either version 3 of the License, or
196 (at your option) any later version.