]> git.wh0rd.org - tt-rss.git/blame - update_daemon2.php
update_daemon2: don't expect client part to be executable
[tt-rss.git] / update_daemon2.php
CommitLineData
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
6a69e61f
AD
10 require_once "config.php";
11 require_once "functions.php";
02008cb1
AD
12
13 define('MAX_JOBS', 2);
14 define('CLIENT_PROCESS', './update_daemon2_client.php SRV_RUN_OK');
15 define('SPAWN_INTERVAL', DAEMON_SLEEP_INTERVAL);
73d3b9b1 16 define('PHP_EXECUTABLE', '/usr/bin/php');
02008cb1
AD
17
18 $running_jobs = 0;
19 $last_checkpoint = -1;
20
21 function sigchld_handler($signal) {
22 global $running_jobs;
23 if ($running_jobs > 0) $running_jobs--;
24 print posix_getpid() . ": SIGCHLD received, jobs left: $running_jobs\n";
25 pcntl_waitpid(-1, $status, WNOHANG);
26 }
27
6a69e61f
AD
28 function sigint_handler() {
29 unlink(LOCK_DIRECTORY . "/update_daemon.lock");
30 die("Received SIGINT. Exiting.\n");
31 }
32
02008cb1 33 pcntl_signal(SIGCHLD, 'sigchld_handler');
6a69e61f
AD
34 pcntl_signal(SIGINT, 'sigint_handler');
35
884c0a36
AD
36 if (file_is_locked("update_daemon.lock")) {
37 die("error: Can't create lockfile. ".
6a69e61f
AD
38 "Maybe another daemon is already running.\n");
39 }
02008cb1 40
884c0a36
AD
41 if (!pcntl_fork()) {
42 $lock_handle = make_lockfile("update_daemon.lock");
43
44 if (!$lock_handle) {
45 die("error: Can't create lockfile. ".
46 "Maybe another daemon is already running.\n");
47 }
48
49 while (true) { sleep(100); }
50 }
51
02008cb1
AD
52 while (true) {
53
54 $next_spawn = $last_checkpoint + SPAWN_INTERVAL - time();
55
ce1aa9b7
AD
56 if ($next_spawn % 10 == 0) {
57 print "[MASTER] active jobs: $running_jobs, next spawn at $next_spawn sec\n";
58 }
02008cb1
AD
59
60 if ($last_checkpoint + SPAWN_INTERVAL < time()) {
61
62 for ($j = $running_jobs; $j < MAX_JOBS; $j++) {
63 print "[MASTER] spawning client $j...";
64 $pid = pcntl_fork();
65 if ($pid == -1) {
66 die("fork failed!\n");
67 } else if ($pid) {
68 $running_jobs++;
69 print "OK [$running_jobs]\n";
70 } else {
71 pcntl_signal(SIGCHLD, SIG_IGN);
6a69e61f 72 pcntl_signal(SIGINT, SIG_DFL);
73d3b9b1 73 passthru(PHP_EXECUTABLE . ' ' . CLIENT_PROCESS);
02008cb1
AD
74 exit(0);
75 }
76 }
77 $last_checkpoint = time();
78 }
79 sleep(1);
80 }
81
82?>