-
-
Notifications
You must be signed in to change notification settings - Fork 59
/
judged.php
216 lines (184 loc) · 6.58 KB
/
judged.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
<?php
// This file is part of Moodle - https://moodle.org
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* NOTICE OF COPYRIGHT
*
* Online Judge for Moodle
* https://github.com/hit-moodle/moodle-local_onlinejudge
*
* Copyright (C) 2009 onwards
* Sun Zhigang http://sunner.cn
* Andrew Naguib <andrew at fci helwan edu eg>
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details:
*
* http://www.gnu.org/copyleft/gpl.html
*/
/**
* Judges all unjudged tasks
*
* In Linux, it will create a daemon and exit
* In Windows, it will never exit except killed by users
*
* @package local_onlinejudge
* @copyright 2011 Sun Zhigang (http://sunner.cn)
* @author Sun Zhigang
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
define('CLI_SCRIPT', true);
define('LOCK_FILE', '/temp/onlinejudge/lock');
require_once(dirname(dirname(dirname(dirname(__FILE__)))) . '/config.php');
require_once($CFG->libdir . '/adminlib.php');
require_once($CFG->libdir . '/clilib.php'); // cli only functions
require_once($CFG->dirroot . '/local/onlinejudge/judgelib.php');
// Ensure errors are well explained
if ($CFG->debug < DEBUG_NORMAL) {
$CFG->debug = DEBUG_NORMAL;
}
// now get cli options
$longoptions = array('help' => false, 'nodaemon' => false, 'once' => false, 'verbose' => false);
$shortoptions = array('h' => 'help', 'n' => 'nodaemon', 'o' => 'once', 'v' => 'verbose');
list($options, $unrecognized) = cli_get_params($longoptions, $shortoptions);
if ($unrecognized) {
$unrecognized = implode("\n ", $unrecognized);
cli_error(get_string('cliunknowoption', 'admin', $unrecognized));
}
if ($options['help']) {
$help = "Judge all unjudged tasks.
Options:
-h, --help Print out this help
-n, --nodaemon Do not run as daemon (Linux only)
-o, --once Exit while no more to judge
-v, --verbose Verbose output
Example:
\$sudo -u www-data /usr/bin/php local/onlinejudge/cli/judged.php
";
echo $help;
die;
}
if ($CFG->ostype != 'WINDOWS' and !$options['nodaemon']) {
// create daemon
verbose(cli_heading('Creating daemon', true));
if (!extension_loaded('pcntl') || !extension_loaded('posix')) {
cli_error('PHP pcntl and posix extension must be installed!');
}
$pid = pcntl_fork();
if ($pid == -1) {
cli_error('Could not fork');
} else if ($pid > 0) { // parent process
mtrace('Judge daemon successfully created. PID = ' . $pid);
die;
} else { // child process
// make the current process a session leader.
$sid = posix_setsid();
if ($sid < 0) {
cli_error('Can not setsid()');
}
// reconnect DB
unset($DB);
setup_DB();
}
}
verbose(cli_separator(true));
verbose('Judge daemon is running now.');
if ($CFG->ostype != 'WINDOWS' and function_exists('pcntl_signal')) {
// Handle SIGTERM and SIGINT so that can be killed without pain
declare(ticks=1); // tick use required as of PHP 4.3.0
pcntl_signal(SIGTERM, 'sigterm_handler');
pcntl_signal(SIGINT, 'sigterm_handler');
}
// Run forever until being killed or the plugin was upgraded
$lockfile = $CFG->dataroot . LOCK_FILE;
if (!check_dir_exists(dirname($lockfile))) {
throw new moodle_exception('errorcreatingdirectory', '', '', $lockfile);
}
$lock = fopen($lockfile, 'w');
if (!$lock) {
mtrace('Can not create' . $CFG->dataroot . LOCK_FILE);
die;
}
$forcestop = false;
$pluginversion = get_config('local_onlinejudge', 'version');
while (!$forcestop) {
judge_all_unjudged();
if ($options['once']) {
break;
}
// Check interval is definable by admin (5 seconds default)
sleep(get_config('local_onlinejudge', 'judgecheckinterval'));
if ($pluginversion < get_config('local_onlinejudge', 'version')) {
verbose('Plugin was upgraded.');
break;
}
}
verbose('Clean temp files.');
onlinejudge_clean_temp_dir(false); // Clean full tree of temp dir
verbose('Judge daemon exits.');
fclose($lock);
/**
* Return one unjudged task and set it status as JUDGING
*
* @return an unjudged task or null;
*/
function get_one_unjudged_task() {
global $CFG, $DB, $lock;
$task = null;
flock($lock, LOCK_EX); // try locking, but ignore if not available (eg. on NFS and FAT)
try {
if ($task = $DB->get_record('onlinejudge_tasks', array('status' => ONLINEJUDGE_STATUS_PENDING), '*', IGNORE_MULTIPLE)) {
$DB->set_field('onlinejudge_tasks', 'status', ONLINEJUDGE_STATUS_JUDGING, array('id' => $task->id));
}
} catch (Exception $e) {
flock($lock, LOCK_UN);
throw $e;
}
flock($lock, LOCK_UN);
return $task;
}
// Judge all unjudged tasks
function judge_all_unjudged() {
while ($task = get_one_unjudged_task()) {
verbose(cli_heading('TASK: ' . $task->id, true));
verbose('Judging...');
try {
$task = onlinejudge_judge($task);
verbose("Successfully judged: $task->status");
} catch (Exception $e) {
$info = get_exception_info($e);
$errmsg = "Judged inner level exception handler: " . $info->message . ' Debug: ' . $info->debuginfo . "\n" . format_backtrace($info->backtrace, true);
cli_problem($errmsg);
// Continue to get next unjudged task
}
}
}
function sigterm_handler($signo) {
global $forcestop;
$forcestop = true;
verbose("Signal $signo catched");
}
function verbose($msg) {
global $options;
if ($options['verbose']) {
mtrace(rtrim($msg));
}
}