]> git.wh0rd.org - tt-rss.git/blame - update_daemon2.php
reduce daemon chattiness a bit
[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
724b7942
AD
32 $master_handlers_installed = false;
33
e9338405 34 $children = array();
c90a028c 35 $ctimes = array();
e9338405 36
02008cb1
AD
37 $last_checkpoint = -1;
38
5a613536 39 function reap_children() {
e9338405 40 global $children;
51ddf0f8 41 global $ctimes;
e9338405
AD
42
43 $tmp = array();
44
45 foreach ($children as $pid) {
46 if (pcntl_waitpid($pid, $status, WNOHANG) != $pid) {
7aabaa09
AD
47
48 if (file_is_locked("update_daemon-$pid.lock")) {
8ccaff02
AD
49 array_push($tmp, $pid);
50 } else {
51 _debug("[reap_children] child $pid seems active but lockfile is unlocked.");
125ab8db
AD
52 unset($ctimes[$pid]);
53
8ccaff02 54 }
e9338405 55 } else {
8ccaff02 56 _debug("[reap_children] child $pid reaped.");
c90a028c 57 unset($ctimes[$pid]);
e9338405
AD
58 }
59 }
60
61 $children = $tmp;
62
5a613536
AD
63 return count($tmp);
64 }
65
c90a028c
AD
66 function check_ctimes() {
67 global $ctimes;
dbaa4e4a 68
c90a028c
AD
69 foreach (array_keys($ctimes) as $pid) {
70 $started = $ctimes[$pid];
71
72 if (time() - $started > MAX_CHILD_RUNTIME) {
73 _debug("[MASTER] child process $pid seems to be stuck, aborting...");
0618b81c 74 posix_kill($pid, SIGKILL);
c90a028c
AD
75 }
76 }
5a613536
AD
77 }
78
79 function sigchld_handler($signal) {
80 $running_jobs = reap_children();
e9338405
AD
81
82 _debug("[SIGCHLD] jobs left: $running_jobs");
5a613536 83
02008cb1
AD
84 pcntl_waitpid(-1, $status, WNOHANG);
85 }
86
cfe6d444
AD
87 function shutdown($caller_pid) {
88 if ($caller_pid == posix_getpid()) {
89 if (file_exists(LOCK_DIRECTORY . "/update_daemon.lock")) {
90 _debug("removing lockfile (master)...");
91 unlink(LOCK_DIRECTORY . "/update_daemon.lock");
92 }
2cd099f0 93 }
8ccaff02
AD
94 }
95
96 function task_shutdown() {
97 $pid = posix_getpid();
98
2cd099f0
AD
99 if (file_exists(LOCK_DIRECTORY . "/update_daemon-$pid.lock")) {
100 _debug("removing lockfile ($pid)...");
8ccaff02 101 unlink(LOCK_DIRECTORY . "/update_daemon-$pid.lock");
2cd099f0 102 }
8ccaff02
AD
103 }
104
6a69e61f 105 function sigint_handler() {
2cd099f0 106 _debug("[MASTER] SIG_INT received.\n");
cfe6d444 107 shutdown(posix_getpid());
2cd099f0 108 die;
dbaa4e4a 109 }
8ccaff02
AD
110
111 function task_sigint_handler() {
2cd099f0 112 _debug("[TASK] SIG_INT received.\n");
8ccaff02 113 task_shutdown();
2cd099f0 114 die;
dbaa4e4a 115 }
6a69e61f 116
02008cb1 117 pcntl_signal(SIGCHLD, 'sigchld_handler');
6a69e61f 118
dc24b520
AD
119 $longopts = array("log:",
120 "tasks:",
ec25336d 121 "interval:",
db985423 122 "quiet",
dc24b520
AD
123 "help");
124
125 $options = getopt("", $longopts);
126
127 if (isset($options["help"]) ) {
128 print "Tiny Tiny RSS update daemon.\n\n";
129 print "Options:\n";
130 print " --log FILE - log messages to FILE\n";
131 print " --tasks N - amount of update tasks to spawn\n";
132 print " default: " . MAX_JOBS . "\n";
133 print " --interval N - task spawn interval\n";
134 print " default: " . SPAWN_INTERVAL . " seconds.\n";
135 print " --quiet - don't output messages to stdout\n";
136 return;
137 }
138
139 define('QUIET', isset($options['quiet']));
140
141 if (isset($options["tasks"])) {
142 _debug("Set to spawn " . $options["tasks"] . " children.");
ec25336d 143 $max_jobs = $options["tasks"];
dc24b520
AD
144 } else {
145 $max_jobs = MAX_JOBS;
146 }
147
148 if (isset($options["interval"])) {
149 _debug("Spawn interval: " . $options["interval"] . " seconds.");
ec25336d 150 $spawn_interval = $options["interval"];
dc24b520
AD
151 } else {
152 $spawn_interval = SPAWN_INTERVAL;
153 }
154
155 if (isset($options["log"])) {
156 _debug("Logging to " . $options["log"]);
157 define('LOGFILE', $options["log"]);
158 }
159
884c0a36
AD
160 if (file_is_locked("update_daemon.lock")) {
161 die("error: Can't create lockfile. ".
6a69e61f
AD
162 "Maybe another daemon is already running.\n");
163 }
02008cb1 164
1a43a68c
AD
165 // Try to lock a file in order to avoid concurrent update.
166 $lock_handle = make_lockfile("update_daemon.lock");
167
168 if (!$lock_handle) {
169 die("error: Can't create lockfile. ".
170 "Maybe another daemon is already running.\n");
171 }
172
6322ac79 173 init_plugins();
ffa7cbae 174
6322ac79 175 $schema_version = get_schema_version();
4c2da349 176
857efe49
AD
177 if ($schema_version != SCHEMA_VERSION) {
178 die("Schema version is wrong, please upgrade the database.\n");
179 }
180
02008cb1
AD
181 while (true) {
182
45004d43 183 // Since sleep is interupted by SIGCHLD, we need another way to
dc24b520
AD
184 // respect the spawn interval
185 $next_spawn = $last_checkpoint + $spawn_interval - time();
02008cb1 186
61aa7499 187 if ($next_spawn % 60 == 0) {
e9338405
AD
188 $running_jobs = count($children);
189 _debug("[MASTER] active jobs: $running_jobs, next spawn at $next_spawn sec.");
ce1aa9b7 190 }
02008cb1 191
dc24b520 192 if ($last_checkpoint + $spawn_interval < time()) {
02008cb1 193
4c2da349
AD
194 /* Check if schema version changed */
195
6322ac79 196 $test_schema_version = get_schema_version();
4c2da349
AD
197
198 if ($test_schema_version != $schema_version) {
f0e015c4
AD
199 echo "Expected schema version: $schema_version, got: $test_schema_version\n";
200 echo "Schema version changed while we were running, bailing out\n";
4c2da349
AD
201 exit(100);
202 }
203
c90a028c 204 check_ctimes();
5a613536
AD
205 reap_children();
206
dc24b520 207 for ($j = count($children); $j < $max_jobs; $j++) {
02008cb1
AD
208 $pid = pcntl_fork();
209 if ($pid == -1) {
210 die("fork failed!\n");
211 } else if ($pid) {
724b7942
AD
212
213 if (!$master_handlers_installed) {
214 _debug("[MASTER] installing shutdown handlers");
215 pcntl_signal(SIGINT, 'sigint_handler');
cfe6d444 216 register_shutdown_function('shutdown', posix_getpid());
724b7942
AD
217 $master_handlers_installed = true;
218 }
219
e9338405
AD
220 _debug("[MASTER] spawned client $j [PID:$pid]...");
221 array_push($children, $pid);
c90a028c 222 $ctimes[$pid] = time();
02008cb1
AD
223 } else {
224 pcntl_signal(SIGCHLD, SIG_IGN);
8ccaff02
AD
225 pcntl_signal(SIGINT, 'task_sigint_handler');
226
227 register_shutdown_function('task_shutdown');
228
7440a7fe 229 $quiet = (isset($options["quiet"])) ? "--quiet" : "";
8292d05b 230
7440a7fe 231 $my_pid = posix_getpid();
dbaa4e4a 232
7440a7fe 233 passthru(PHP_EXECUTABLE . " update.php --daemon-loop $quiet --task $j --pidlock $my_pid");
a26f0c17 234
ffa7cbae 235 sleep(1);
8ccaff02 236
ffa7cbae 237 // We exit in order to avoid fork bombing.
02008cb1
AD
238 exit(0);
239 }
240 }
241 $last_checkpoint = time();
242 }
243 sleep(1);
244 }
245
246?>