summaryrefslogtreecommitdiff
path: root/update_daemon2.php
blob: d422c536876d3ff11ed1dd289334d0a015272a43 (plain)
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
#!/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_once "config.php";
	require_once "functions.php";

	define('MAX_JOBS', 2);
	define('CLIENT_PROCESS', './update_daemon2_client.php SRV_RUN_OK');
	define('SPAWN_INTERVAL', DAEMON_SLEEP_INTERVAL);
	define('PHP_EXECUTABLE', '/usr/bin/php');

	$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);
	}

	function sigint_handler() {
		unlink(LOCK_DIRECTORY . "/update_daemon.lock");
		die("Received SIGINT. Exiting.\n");
	}

	pcntl_signal(SIGCHLD, 'sigchld_handler');
	pcntl_signal(SIGINT, 'sigint_handler');

	if (file_is_locked("update_daemon.lock")) {
		die("error: Can't create lockfile. ".
			"Maybe another daemon is already running.\n");
	}

	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); }
	}

	while (true) {

		$next_spawn = $last_checkpoint + SPAWN_INTERVAL - time();

		if ($next_spawn % 10 == 0) {
			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);
					pcntl_signal(SIGINT, SIG_DFL);
					passthru(PHP_EXECUTABLE . ' ' . CLIENT_PROCESS);
					exit(0);
				}
			}
			$last_checkpoint = time();
		}
		sleep(1);
	}

?>