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 copy_from_vm 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
=> {
43 ##################################################
46 my ($type, $sub, $name) = @_;
48 get_logger
->logdie("Invalid \l$type: '$name'") unless $name =~ /^\w+$/s;
49 my $pkg = __PACKAGE__
. "::${type}::${name}";
50 eval "require $pkg" or get_logger
->warn("Error while requiring $pkg: $@");
51 $pkg->can($sub) or get_logger
->logdie("No such \l$type: '$name'");
58 my @full_results = ();
64 my ($files, $generator, $runner, $judge, $testcnt) = map { $meta->{$_} or die "Required parameter missing: $_\n"} qw
/files generator runner judge testcnt/;
66 $generator = safe_can Generator
=> generate
=> $generator;
67 $runner = safe_can Runner
=> run
=> $runner;
68 $judge = safe_can Judge
=> judge
=> $judge;
70 for my $test (1 .. $testcnt) {
74 $generator->($test, $meta);
75 $result = $runner->($test, $meta);
78 unless (ref $result) {
80 $result = [ERR
, $result];
85 $time = (read_file
'time-stop-prog') - (read_file
'time-start-prog') if -f
'time-stop-prog' && -f
'time-start-prog';
86 unlink 'time-start-prog', 'time-stop-prog';
88 my $result_text = $result->[1];
89 copy_from_vm
'result';
90 $result_text .= ': ' . read_file
'result' if -s
'result';
91 get_logger
->trace("Test $test result is " . $result_text);
92 push @full_results, {id
=> $test, result
=> $result->[0], result_text
=> $result_text, time => $time}
94 get_logger
->trace("Test $test result is $result");
95 push @full_results, {id
=> $test, result
=> 0, result_text
=> $result, time => $time}
97 push @results, $result;
98 last if $meta->{judge
} eq 'Absolute' && ref $result
101 my %results = $judge->(@results);
102 $meta->{$_} = $results{$_} for keys %results;
103 $meta->{results
} = \
@full_results
107 $meta->{result
} = -1;
108 $meta->{result_text
} = $_;
111 $meta->{errors
} = $errors;
113 get_logger
->info('Job result: ' . $meta->{result_text
});
118 my $pb = db
()->select('problems', '*', {id
=> $job->{problem
}})->hash;
120 problem
=> $job->{problem
},
123 name
=> 'prog.' . $job->{extension
},
124 format
=> $job->{format
},
125 content
=> $job->{source
},
128 map { $_ => $pb->{$_} } qw
/generator runner judge precnt testcnt timeout olimit/
130 my $timeout_override = db
()->query('SELECT timeout FROM limits WHERE problem=? AND format=?', $job->{problem
}, $job->{format
})->list;
131 $meta->{timeout
} = $timeout_override if defined $timeout_override;
132 $meta->{tests
} = decode_json
$pb->{tests
} if $meta->{runner
} eq 'File';
133 $job->{contest
} &&= contest_entry
($job->{contest
});
134 delete $meta->{precnt
} unless $job->{contest
} && $job->{contest
}{started
} && !$job->{contest
}{finished
}; ## no critic (ProhibitNegativeExpressionsInUnlessAndUntilConditions)
135 $meta->{testcnt
} = $meta->{precnt
} if $meta->{precnt
};
137 $meta->{files
}{ver
} = {
138 name
=> 'ver.' . FORMAT_EXTENSION
->{$pb->{verformat
}},
139 format
=> $pb->{verformat
},
140 content
=> $pb->{versource
},
141 } if $pb->{verformat
};
145 $meta->{result_text
} .= ' (pretests)' if $meta->{precnt
};
146 finish_job
($job, $job->{private
} || $pb->{private
},
147 result
=> $meta->{result
},
148 result_text
=> $meta->{result_text
},
149 ($meta->{results
} ?
(results
=> encode_json
$meta->{results
}) : ()),
150 $meta->{errors
} ?
(errors
=> $meta->{errors
}) : ());
156 get_logger
->debug("Processing job $id...");
158 get_logger
->debug("Job $id done");
161 my $daemon = hostname
. ":$$";
164 require Gruntmaster
::Data
;
165 Gruntmaster
::Data
->import;
166 dbinit
($ENV{GRUNTMASTER_DSN
} // 'dbi:Pg:');
167 Log
::Log4perl
->init('/etc/gruntmasterd/gruntmasterd-log.conf');
168 get_logger
->info("gruntmasterd $VERSION started ($daemon)");
169 my $dir = tempdir
'gruntmasterd.XXXX', CLEANUP
=> 1, TMPDIR
=> 1;
173 my $job = take_job
($daemon);
174 got_job
$job if defined $job;
175 sleep 2 unless defined $job;
184 Gruntmaster::Daemon - Gruntmaster 6000 Online Judge -- daemon
188 use Gruntmaster::Daemon;
189 Gruntmaster::Daemon->run;
193 Gruntmaster::Daemon is the daemon component of the Gruntmaster 6000 online judge.
197 Marius Gavrilescu E<lt>marius@ieval.roE<gt>
199 =head1 COPYRIGHT AND LICENSE
201 Copyright (C) 2014 by Marius Gavrilescu
203 This library is free software: you can redistribute it and/or modify
204 it under the terms of the GNU Affero General Public License as published by
205 the Free Software Foundation, either version 3 of the License, or
206 (at your option) any later version.