]> git.wh0rd.org - tt-rss.git/blame - update_daemon2.php
Merge branch 'master' of github.com:gothfox/Tiny-Tiny-RSS
[tt-rss.git] / update_daemon2.php
CommitLineData
bf0bedcb 1#!/usr/bin/env php
02008cb1 2<?php
88e8fb3a
AD
3 set_include_path(dirname(__FILE__) ."/include" . PATH_SEPARATOR .
4 get_include_path());
ffa7cbae 5
02008cb1 6 declare(ticks = 1);
9b27cec8 7 chdir(dirname(__FILE__));
02008cb1 8
ffa7cbae 9 define('DISABLE_SESSIONS', true);
02008cb1 10
ffa7cbae 11 require_once "version.php";
0e6bdaef 12 require_once "config.php";
404e2e36 13 require_once "autoload.php";
9765e8b9 14 require_once "functions.php";
8cabc200 15 require_once "rssfuncs.php";
0e6bdaef
AD
16
17 // defaults
18 define_default('PURGE_INTERVAL', 3600); // seconds
f90728cd 19 define_default('MAX_CHILD_RUNTIME', 1800); // seconds
0e6bdaef
AD
20 define_default('MAX_JOBS', 2);
21 define_default('SPAWN_INTERVAL', DAEMON_SLEEP_INTERVAL); // seconds
0e6bdaef 22
ffa7cbae 23 require_once "sanity_check.php";
5893edd5
AD
24 require_once "db.php";
25 require_once "db-prefs.php";
ffa7cbae 26
ffa7cbae 27
16956646
AD
28 if (!function_exists('pcntl_fork')) {
29 die("error: This script requires PHP compiled with PCNTL module.\n");
30 }
31
e9338405 32 $children = array();
c90a028c 33 $ctimes = array();
e9338405 34
02008cb1
AD
35 $last_checkpoint = -1;
36
5a613536 37 function reap_children() {
e9338405 38 global $children;
51ddf0f8 39 global $ctimes;
e9338405
AD
40
41 $tmp = array();
42
43 foreach ($children as $pid) {
44 if (pcntl_waitpid($pid, $status, WNOHANG) != $pid) {
7aabaa09
AD
45
46 if (file_is_locked("update_daemon-$pid.lock")) {
8ccaff02
AD
47 array_push($tmp, $pid);
48 } else {
49 _debug("[reap_children] child $pid seems active but lockfile is unlocked.");
125ab8db
AD
50 unset($ctimes[$pid]);
51
8ccaff02 52 }
e9338405 53 } else {
8ccaff02 54 _debug("[reap_children] child $pid reaped.");
c90a028c 55 unset($ctimes[$pid]);
e9338405
AD
56 }
57 }
58
59 $children = $tmp;
60
5a613536
AD
61 return count($tmp);
62 }
63
c90a028c
AD
64 function check_ctimes() {
65 global $ctimes;
dbaa4e4a 66
c90a028c
AD
67 foreach (array_keys($ctimes) as $pid) {
68 $started = $ctimes[$pid];
69
70 if (time() - $started > MAX_CHILD_RUNTIME) {
71 _debug("[MASTER] child process $pid seems to be stuck, aborting...");
0618b81c 72 posix_kill($pid, SIGKILL);
c90a028c
AD
73 }
74 }
5a613536
AD
75 }
76
77 function sigchld_handler($signal) {
78 $running_jobs = reap_children();
e9338405
AD
79
80 _debug("[SIGCHLD] jobs left: $running_jobs");
5a613536 81
02008cb1
AD
82 pcntl_waitpid(-1, $status, WNOHANG);
83 }
84
85 pcntl_signal(SIGCHLD, 'sigchld_handler');
6a69e61f 86
dc24b520
AD
87 $longopts = array("log:",
88 "tasks:",
ec25336d 89 "interval:",
db985423 90 "quiet",
dc24b520
AD
91 "help");
92
93 $options = getopt("", $longopts);
94
95 if (isset($options["help"]) ) {
96 print "Tiny Tiny RSS update daemon.\n\n";
97 print "Options:\n";
98 print " --log FILE - log messages to FILE\n";
99 print " --tasks N - amount of update tasks to spawn\n";
100 print " default: " . MAX_JOBS . "\n";
101 print " --interval N - task spawn interval\n";
102 print " default: " . SPAWN_INTERVAL . " seconds.\n";
103 print " --quiet - don't output messages to stdout\n";
104 return;
105 }
106
107 define('QUIET', isset($options['quiet']));
108
109 if (isset($options["tasks"])) {
110 _debug("Set to spawn " . $options["tasks"] . " children.");
ec25336d 111 $max_jobs = $options["tasks"];
dc24b520
AD
112 } else {
113 $max_jobs = MAX_JOBS;
114 }
115
116 if (isset($options["interval"])) {
117 _debug("Spawn interval: " . $options["interval"] . " seconds.");
ec25336d 118 $spawn_interval = $options["interval"];
dc24b520
AD
119 } else {
120 $spawn_interval = SPAWN_INTERVAL;
121 }
122
123 if (isset($options["log"])) {
124 _debug("Logging to " . $options["log"]);
125 define('LOGFILE', $options["log"]);
126 }
127
884c0a36
AD
128 if (file_is_locked("update_daemon.lock")) {
129 die("error: Can't create lockfile. ".
6a69e61f
AD
130 "Maybe another daemon is already running.\n");
131 }
02008cb1 132
1a43a68c
AD
133 // Try to lock a file in order to avoid concurrent update.
134 $lock_handle = make_lockfile("update_daemon.lock");
135
136 if (!$lock_handle) {
137 die("error: Can't create lockfile. ".
138 "Maybe another daemon is already running.\n");
139 }
140
6322ac79 141 $schema_version = get_schema_version();
4c2da349 142
857efe49
AD
143 if ($schema_version != SCHEMA_VERSION) {
144 die("Schema version is wrong, please upgrade the database.\n");
145 }
146
e2cf81e2
AD
147 // Protip: children close shared database handle when terminating, it's a bad idea to
148 // do database stuff on main process from now on.
149
02008cb1
AD
150 while (true) {
151
45004d43 152 // Since sleep is interupted by SIGCHLD, we need another way to
dc24b520
AD
153 // respect the spawn interval
154 $next_spawn = $last_checkpoint + $spawn_interval - time();
02008cb1 155
61aa7499 156 if ($next_spawn % 60 == 0) {
e9338405
AD
157 $running_jobs = count($children);
158 _debug("[MASTER] active jobs: $running_jobs, next spawn at $next_spawn sec.");
ce1aa9b7 159 }
02008cb1 160
dc24b520 161 if ($last_checkpoint + $spawn_interval < time()) {
c90a028c 162 check_ctimes();
5a613536
AD
163 reap_children();
164
dc24b520 165 for ($j = count($children); $j < $max_jobs; $j++) {
02008cb1
AD
166 $pid = pcntl_fork();
167 if ($pid == -1) {
168 die("fork failed!\n");
169 } else if ($pid) {
e9338405
AD
170 _debug("[MASTER] spawned client $j [PID:$pid]...");
171 array_push($children, $pid);
c90a028c 172 $ctimes[$pid] = time();
02008cb1
AD
173 } else {
174 pcntl_signal(SIGCHLD, SIG_IGN);
8ccaff02 175
7440a7fe 176 $quiet = (isset($options["quiet"])) ? "--quiet" : "";
8292d05b 177
7440a7fe 178 $my_pid = posix_getpid();
dbaa4e4a 179
7440a7fe 180 passthru(PHP_EXECUTABLE . " update.php --daemon-loop $quiet --task $j --pidlock $my_pid");
a26f0c17 181
ffa7cbae 182 sleep(1);
8ccaff02 183
ffa7cbae 184 // We exit in order to avoid fork bombing.
02008cb1
AD
185 exit(0);
186 }
187 }
188 $last_checkpoint = time();
189 }
190 sleep(1);
191 }
192
193?>