diff options
author | Andrew Dolgov <[email protected]> | 2008-01-23 10:19:36 +0100 |
---|---|---|
committer | Andrew Dolgov <[email protected]> | 2008-01-23 10:19:36 +0100 |
commit | 02008cb19ac99727889cca05c7eddfbeef30d684 (patch) | |
tree | b1ff053ad8f846ea4171460edf0ba5220c2cb2e2 /update_daemon2.php | |
parent | d4761137527576a249964eabaee66d36e62eb74f (diff) |
add multiprocess update daemon
Diffstat (limited to 'update_daemon2.php')
-rw-r--r-- | update_daemon2.php | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/update_daemon2.php b/update_daemon2.php new file mode 100644 index 000000000..3721a0979 --- /dev/null +++ b/update_daemon2.php @@ -0,0 +1,55 @@ +#!/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); + + require "config.php"; + + define('MAX_JOBS', 2); + define('CLIENT_PROCESS', './update_daemon2_client.php SRV_RUN_OK'); + define('SPAWN_INTERVAL', DAEMON_SLEEP_INTERVAL); + + $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); + } + + pcntl_signal(SIGCHLD, 'sigchld_handler'); + + while (true) { + + $next_spawn = $last_checkpoint + SPAWN_INTERVAL - time(); + + print "[MASTER] active jobs: $running_jobs, next spawn at $next_spawn sec\n"; + + 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); + passthru(CLIENT_PROCESS); + exit(0); + } + } + $last_checkpoint = time(); + } + sleep(1); + } + +?> |