]> git.wh0rd.org - tt-rss.git/blame - update_daemon2.php
update_daemon2: make proper lockfile
[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
46 print "[MASTER] active jobs: $running_jobs, next spawn at $next_spawn sec\n";
47
48 if ($last_checkpoint + SPAWN_INTERVAL < time()) {
49
50 for ($j = $running_jobs; $j < MAX_JOBS; $j++) {
51 print "[MASTER] spawning client $j...";
52 $pid = pcntl_fork();
53 if ($pid == -1) {
54 die("fork failed!\n");
55 } else if ($pid) {
56 $running_jobs++;
57 print "OK [$running_jobs]\n";
58 } else {
59 pcntl_signal(SIGCHLD, SIG_IGN);
6a69e61f 60 pcntl_signal(SIGINT, SIG_DFL);
02008cb1
AD
61 passthru(CLIENT_PROCESS);
62 exit(0);
63 }
64 }
65 $last_checkpoint = time();
66 }
67 sleep(1);
68 }
69
70?>