1 package Gruntmaster
::Daemon
;
7 our $VERSION = '5999.000_005';
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/;
17 use Log
::Log4perl qw
/get_logger/;
19 use constant FORMAT_EXTENSION
=> {
42 ##################################################
45 my ($type, $sub, $name) = @_;
47 get_logger
->logdie("Invalid \l$type: '$name'") unless $name =~ /^\w+$/s;
48 my $pkg = __PACKAGE__
. "::${type}::${name}";
49 eval "require $pkg" or get_logger
->warn("Error while requiring $pkg: $@");
50 $pkg->can($sub) or get_logger
->logdie("No such \l$type: '$name'");
57 my @full_results = ();
63 my ($files, $generator, $runner, $judge, $testcnt) = map { $meta->{$_} or die "Required parameter missing: $_\n"} qw
/files generator runner judge testcnt/;
65 $generator = safe_can Generator
=> generate
=> $generator;
66 $runner = safe_can Runner
=> run
=> $runner;
67 $judge = safe_can Judge
=> judge
=> $judge;
69 for my $test (1 .. $testcnt) {
73 $generator->($test, $meta);
74 $result = $runner->($test, $meta);
77 unless (ref $result) {
79 $result = [ERR
, $result];
84 $time = (read_file
'time-stop-prog') - (read_file
'time-start-prog') if -f
'time-stop-prog' && -f
'time-start-prog';
85 unlink 'time-start-prog', 'time-stop-prog';
87 my $result_text = $result->[1];
88 $result_text .= ': ' . read_file
'result' if -s
'result';
89 get_logger
->trace("Test $test result is " . $result_text);
90 push @full_results, {id
=> $test, result
=> $result->[0], result_text
=> $result_text, time => $time}
92 get_logger
->trace("Test $test result is $result");
93 push @full_results, {id
=> $test, result
=> 0, result_text
=> $result, time => $time}
95 push @results, $result;
96 last if $meta->{judge
} eq 'Absolute' && ref $result
99 my %results = $judge->(@results);
100 $meta->{$_} = $results{$_} for keys %results;
101 $meta->{results
} = \
@full_results
105 $meta->{result
} = -1;
106 $meta->{result_text
} = $_;
109 $meta->{errors
} = $errors;
111 get_logger
->info('Job result: ' . $meta->{result_text
});
116 my $pb = db
()->select('problems', '*', {id
=> $job->{problem
}})->hash;
118 problem
=> $job->{problem
},
121 name
=> 'prog.' . $job->{extension
},
122 format
=> $job->{format
},
123 content
=> $job->{source
},
126 map { $_ => $pb->{$_} } qw
/generator runner judge precnt testcnt timeout olimit/
128 my $timeout_override = db
()->query('SELECT timeout FROM limits WHERE problem=? AND format=?', $job->{problem
}, $job->{format
})->list;
129 $meta->{timeout
} = $timeout_override if defined $timeout_override;
130 $meta->{tests
} = decode_json
$pb->{tests
} if $meta->{runner
} eq 'File';
131 $job->{contest
} &&= contest_entry
($job->{contest
});
132 delete $meta->{precnt
} unless $job->{contest
} && $job->{contest
}{started
} && !$job->{contest
}{finished
}; ## no critic (ProhibitNegativeExpressionsInUnlessAndUntilConditions)
133 $meta->{testcnt
} = $meta->{precnt
} if $meta->{precnt
};
135 $meta->{files
}{ver
} = {
136 name
=> 'ver.' . FORMAT_EXTENSION
->{$pb->{verformat
}},
137 format
=> $pb->{verformat
},
138 content
=> $pb->{versource
},
139 } if $pb->{verformat
};
143 $meta->{result_text
} .= ' (pretests)' if $meta->{precnt
};
144 finish_job
($job, $job->{private
} || $pb->{private
},
145 result
=> $meta->{result
},
146 result_text
=> $meta->{result_text
},
147 ($meta->{results
} ?
(results
=> encode_json
$meta->{results
}) : ()),
148 $meta->{errors
} ?
(errors
=> $meta->{errors
}) : ());
154 get_logger
->debug("Processing job $id...");
156 get_logger
->debug("Job $id done");
159 my $daemon = hostname
. ":$$";
162 require Gruntmaster
::Data
;
163 Gruntmaster
::Data
->import;
164 dbinit
($ENV{GRUNTMASTER_DSN
} // 'dbi:Pg:');
165 Log
::Log4perl
->init('/etc/gruntmasterd/gruntmasterd-log.conf');
166 get_logger
->info("gruntmasterd $VERSION started ($daemon)");
167 my $dir = tempdir
'gruntmasterd.XXXX', CLEANUP
=> 1, TMPDIR
=> 1;
171 my $job = take_job
($daemon);
172 got_job
$job if defined $job;
173 sleep 2 unless defined $job;
182 Gruntmaster::Daemon - Gruntmaster 6000 Online Judge -- daemon
186 use Gruntmaster::Daemon;
187 Gruntmaster::Daemon->run;
191 Gruntmaster::Daemon is the daemon component of the Gruntmaster 6000 online judge.
195 Marius Gavrilescu E<lt>marius@ieval.roE<gt>
197 =head1 COPYRIGHT AND LICENSE
199 Copyright (C) 2014 by Marius Gavrilescu
201 This library is free software: you can redistribute it and/or modify
202 it under the terms of the GNU Affero General Public License as published by
203 the Free Software Foundation, either version 3 of the License, or
204 (at your option) any later version.