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