]>
Commit | Line | Data |
---|---|---|
02008cb1 AD |
1 | #!/usr/bin/php |
2 | <?php | |
3 | // This is an experimental multiprocess update daemon | |
4 | // It consists of the master server (this file) and | |
5 | // client batch script (update_daemon2_client.php) which | |
6 | // should only be run by the server process | |
7 | ||
8 | declare(ticks = 1); | |
9 | ||
10 | require "config.php"; | |
11 | ||
12 | define('MAX_JOBS', 2); | |
13 | define('CLIENT_PROCESS', './update_daemon2_client.php SRV_RUN_OK'); | |
14 | define('SPAWN_INTERVAL', DAEMON_SLEEP_INTERVAL); | |
15 | ||
16 | $running_jobs = 0; | |
17 | $last_checkpoint = -1; | |
18 | ||
19 | function sigchld_handler($signal) { | |
20 | global $running_jobs; | |
21 | if ($running_jobs > 0) $running_jobs--; | |
22 | print posix_getpid() . ": SIGCHLD received, jobs left: $running_jobs\n"; | |
23 | pcntl_waitpid(-1, $status, WNOHANG); | |
24 | } | |
25 | ||
26 | pcntl_signal(SIGCHLD, 'sigchld_handler'); | |
27 | ||
28 | while (true) { | |
29 | ||
30 | $next_spawn = $last_checkpoint + SPAWN_INTERVAL - time(); | |
31 | ||
32 | print "[MASTER] active jobs: $running_jobs, next spawn at $next_spawn sec\n"; | |
33 | ||
34 | if ($last_checkpoint + SPAWN_INTERVAL < time()) { | |
35 | ||
36 | for ($j = $running_jobs; $j < MAX_JOBS; $j++) { | |
37 | print "[MASTER] spawning client $j..."; | |
38 | $pid = pcntl_fork(); | |
39 | if ($pid == -1) { | |
40 | die("fork failed!\n"); | |
41 | } else if ($pid) { | |
42 | $running_jobs++; | |
43 | print "OK [$running_jobs]\n"; | |
44 | } else { | |
45 | pcntl_signal(SIGCHLD, SIG_IGN); | |
46 | passthru(CLIENT_PROCESS); | |
47 | exit(0); | |
48 | } | |
49 | } | |
50 | $last_checkpoint = time(); | |
51 | } | |
52 | sleep(1); | |
53 | } | |
54 | ||
55 | ?> |