2008-01-23 10:19:36 +01:00
|
|
|
#!/usr/bin/php
|
|
|
|
<?php
|
|
|
|
// This is an experimental multiprocess update daemon
|
|
|
|
// It consists of the master server (this file) and
|
|
|
|
// client batch script (update_daemon2_client.php) which
|
|
|
|
// should only be run by the server process
|
|
|
|
|
|
|
|
declare(ticks = 1);
|
|
|
|
|
2008-01-23 10:30:55 +01:00
|
|
|
require_once "config.php";
|
|
|
|
require_once "functions.php";
|
2008-01-23 10:19:36 +01:00
|
|
|
|
|
|
|
define('MAX_JOBS', 2);
|
|
|
|
define('CLIENT_PROCESS', './update_daemon2_client.php SRV_RUN_OK');
|
|
|
|
define('SPAWN_INTERVAL', DAEMON_SLEEP_INTERVAL);
|
2008-01-23 15:29:24 +01:00
|
|
|
define('PHP_EXECUTABLE', '/usr/bin/php');
|
2008-01-23 10:19:36 +01:00
|
|
|
|
|
|
|
$running_jobs = 0;
|
|
|
|
$last_checkpoint = -1;
|
|
|
|
|
|
|
|
function sigchld_handler($signal) {
|
|
|
|
global $running_jobs;
|
|
|
|
if ($running_jobs > 0) $running_jobs--;
|
|
|
|
print posix_getpid() . ": SIGCHLD received, jobs left: $running_jobs\n";
|
|
|
|
pcntl_waitpid(-1, $status, WNOHANG);
|
|
|
|
}
|
|
|
|
|
2008-01-23 10:30:55 +01:00
|
|
|
function sigint_handler() {
|
|
|
|
unlink(LOCK_DIRECTORY . "/update_daemon.lock");
|
|
|
|
die("Received SIGINT. Exiting.\n");
|
|
|
|
}
|
|
|
|
|
2008-01-23 10:19:36 +01:00
|
|
|
pcntl_signal(SIGCHLD, 'sigchld_handler');
|
2008-01-23 10:30:55 +01:00
|
|
|
pcntl_signal(SIGINT, 'sigint_handler');
|
|
|
|
|
2008-01-23 12:43:11 +01:00
|
|
|
if (file_is_locked("update_daemon.lock")) {
|
|
|
|
die("error: Can't create lockfile. ".
|
2008-01-23 10:30:55 +01:00
|
|
|
"Maybe another daemon is already running.\n");
|
|
|
|
}
|
2008-01-23 10:19:36 +01:00
|
|
|
|
2008-01-23 12:43:11 +01:00
|
|
|
if (!pcntl_fork()) {
|
|
|
|
$lock_handle = make_lockfile("update_daemon.lock");
|
|
|
|
|
|
|
|
if (!$lock_handle) {
|
|
|
|
die("error: Can't create lockfile. ".
|
|
|
|
"Maybe another daemon is already running.\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
while (true) { sleep(100); }
|
|
|
|
}
|
|
|
|
|
2008-01-23 10:19:36 +01:00
|
|
|
while (true) {
|
|
|
|
|
|
|
|
$next_spawn = $last_checkpoint + SPAWN_INTERVAL - time();
|
|
|
|
|
2008-01-23 10:33:41 +01:00
|
|
|
if ($next_spawn % 10 == 0) {
|
|
|
|
print "[MASTER] active jobs: $running_jobs, next spawn at $next_spawn sec\n";
|
|
|
|
}
|
2008-01-23 10:19:36 +01:00
|
|
|
|
|
|
|
if ($last_checkpoint + SPAWN_INTERVAL < time()) {
|
|
|
|
|
|
|
|
for ($j = $running_jobs; $j < MAX_JOBS; $j++) {
|
|
|
|
print "[MASTER] spawning client $j...";
|
|
|
|
$pid = pcntl_fork();
|
|
|
|
if ($pid == -1) {
|
|
|
|
die("fork failed!\n");
|
|
|
|
} else if ($pid) {
|
|
|
|
$running_jobs++;
|
|
|
|
print "OK [$running_jobs]\n";
|
|
|
|
} else {
|
|
|
|
pcntl_signal(SIGCHLD, SIG_IGN);
|
2008-01-23 10:30:55 +01:00
|
|
|
pcntl_signal(SIGINT, SIG_DFL);
|
2008-01-23 15:29:24 +01:00
|
|
|
passthru(PHP_EXECUTABLE . ' ' . CLIENT_PROCESS);
|
2008-01-23 10:19:36 +01:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$last_checkpoint = time();
|
|
|
|
}
|
|
|
|
sleep(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
?>
|