]> git.wh0rd.org - tt-rss.git/blame - update_daemon2.php
reinstate error handlers; better DB error reporting on failed queries
[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
AD
11 require_once "version.php";
12
010c16f1 13 if (strpos(VERSION, ".99") !== false || getenv('DAEMON_XDEBUG')) {
ffa7cbae
AD
14 define('DAEMON_EXTENDED_DEBUG', true);
15 }
16
404e2e36 17 require_once "autoload.php";
9765e8b9
AD
18 require_once "functions.php";
19 require_once "rssfuncs.php";
ffa7cbae
AD
20 require_once "sanity_check.php";
21 require_once "config.php";
5893edd5
AD
22 require_once "db.php";
23 require_once "db-prefs.php";
2a649c44 24 require_once "errorhandler.php";
ffa7cbae 25
dc24b520
AD
26 // defaults
27 define('PURGE_INTERVAL', 3600); // seconds
28 define('MAX_CHILD_RUNTIME', 600); // seconds
98f70418 29 define('MAX_JOBS', 2);
dc24b520 30 define('SPAWN_INTERVAL', DAEMON_SLEEP_INTERVAL); // seconds
ffa7cbae 31
16956646
AD
32 if (!function_exists('pcntl_fork')) {
33 die("error: This script requires PHP compiled with PCNTL module.\n");
34 }
35
724b7942
AD
36 $master_handlers_installed = false;
37
e9338405 38 $children = array();
c90a028c 39 $ctimes = array();
e9338405 40
02008cb1
AD
41 $last_checkpoint = -1;
42
5a613536 43 function reap_children() {
e9338405 44 global $children;
51ddf0f8 45 global $ctimes;
e9338405
AD
46
47 $tmp = array();
48
49 foreach ($children as $pid) {
50 if (pcntl_waitpid($pid, $status, WNOHANG) != $pid) {
7aabaa09
AD
51
52 if (file_is_locked("update_daemon-$pid.lock")) {
8ccaff02
AD
53 array_push($tmp, $pid);
54 } else {
55 _debug("[reap_children] child $pid seems active but lockfile is unlocked.");
125ab8db
AD
56 unset($ctimes[$pid]);
57
8ccaff02 58 }
e9338405 59 } else {
8ccaff02 60 _debug("[reap_children] child $pid reaped.");
c90a028c 61 unset($ctimes[$pid]);
e9338405
AD
62 }
63 }
64
65 $children = $tmp;
66
5a613536
AD
67 return count($tmp);
68 }
69
c90a028c
AD
70 function check_ctimes() {
71 global $ctimes;
dbaa4e4a 72
c90a028c
AD
73 foreach (array_keys($ctimes) as $pid) {
74 $started = $ctimes[$pid];
75
76 if (time() - $started > MAX_CHILD_RUNTIME) {
77 _debug("[MASTER] child process $pid seems to be stuck, aborting...");
0618b81c 78 posix_kill($pid, SIGKILL);
c90a028c
AD
79 }
80 }
5a613536
AD
81 }
82
83 function sigchld_handler($signal) {
84 $running_jobs = reap_children();
e9338405
AD
85
86 _debug("[SIGCHLD] jobs left: $running_jobs");
5a613536 87
02008cb1
AD
88 pcntl_waitpid(-1, $status, WNOHANG);
89 }
90
cfe6d444
AD
91 function shutdown($caller_pid) {
92 if ($caller_pid == posix_getpid()) {
93 if (file_exists(LOCK_DIRECTORY . "/update_daemon.lock")) {
94 _debug("removing lockfile (master)...");
95 unlink(LOCK_DIRECTORY . "/update_daemon.lock");
96 }
2cd099f0 97 }
8ccaff02
AD
98 }
99
100 function task_shutdown() {
101 $pid = posix_getpid();
102
2cd099f0
AD
103 if (file_exists(LOCK_DIRECTORY . "/update_daemon-$pid.lock")) {
104 _debug("removing lockfile ($pid)...");
8ccaff02 105 unlink(LOCK_DIRECTORY . "/update_daemon-$pid.lock");
2cd099f0 106 }
8ccaff02
AD
107 }
108
6a69e61f 109 function sigint_handler() {
2cd099f0 110 _debug("[MASTER] SIG_INT received.\n");
cfe6d444 111 shutdown(posix_getpid());
2cd099f0 112 die;
dbaa4e4a 113 }
8ccaff02
AD
114
115 function task_sigint_handler() {
2cd099f0 116 _debug("[TASK] SIG_INT received.\n");
8ccaff02 117 task_shutdown();
2cd099f0 118 die;
dbaa4e4a 119 }
6a69e61f 120
02008cb1 121 pcntl_signal(SIGCHLD, 'sigchld_handler');
6a69e61f 122
dc24b520
AD
123 $longopts = array("log:",
124 "tasks:",
ec25336d 125 "interval:",
db985423 126 "quiet",
dc24b520
AD
127 "help");
128
129 $options = getopt("", $longopts);
130
131 if (isset($options["help"]) ) {
132 print "Tiny Tiny RSS update daemon.\n\n";
133 print "Options:\n";
134 print " --log FILE - log messages to FILE\n";
135 print " --tasks N - amount of update tasks to spawn\n";
136 print " default: " . MAX_JOBS . "\n";
137 print " --interval N - task spawn interval\n";
138 print " default: " . SPAWN_INTERVAL . " seconds.\n";
139 print " --quiet - don't output messages to stdout\n";
140 return;
141 }
142
143 define('QUIET', isset($options['quiet']));
144
145 if (isset($options["tasks"])) {
146 _debug("Set to spawn " . $options["tasks"] . " children.");
ec25336d 147 $max_jobs = $options["tasks"];
dc24b520
AD
148 } else {
149 $max_jobs = MAX_JOBS;
150 }
151
152 if (isset($options["interval"])) {
153 _debug("Spawn interval: " . $options["interval"] . " seconds.");
ec25336d 154 $spawn_interval = $options["interval"];
dc24b520
AD
155 } else {
156 $spawn_interval = SPAWN_INTERVAL;
157 }
158
159 if (isset($options["log"])) {
160 _debug("Logging to " . $options["log"]);
161 define('LOGFILE', $options["log"]);
162 }
163
884c0a36
AD
164 if (file_is_locked("update_daemon.lock")) {
165 die("error: Can't create lockfile. ".
6a69e61f
AD
166 "Maybe another daemon is already running.\n");
167 }
02008cb1 168
1a43a68c
AD
169 // Try to lock a file in order to avoid concurrent update.
170 $lock_handle = make_lockfile("update_daemon.lock");
171
172 if (!$lock_handle) {
173 die("error: Can't create lockfile. ".
174 "Maybe another daemon is already running.\n");
175 }
176
ffa7cbae
AD
177 // Testing database connection.
178 // It is unnecessary to start the fork loop if database is not ok.
dbaa4e4a 179 $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
ffa7cbae 180
ba68b681 181 if (!init_plugins($link)) die("Can't initialize db connection.\n");
4c2da349
AD
182
183 $schema_version = get_schema_version($link);
ffa7cbae
AD
184
185 db_close($link);
186
857efe49
AD
187 if ($schema_version != SCHEMA_VERSION) {
188 die("Schema version is wrong, please upgrade the database.\n");
189 }
190
02008cb1
AD
191 while (true) {
192
45004d43 193 // Since sleep is interupted by SIGCHLD, we need another way to
dc24b520
AD
194 // respect the spawn interval
195 $next_spawn = $last_checkpoint + $spawn_interval - time();
02008cb1 196
61aa7499 197 if ($next_spawn % 60 == 0) {
e9338405
AD
198 $running_jobs = count($children);
199 _debug("[MASTER] active jobs: $running_jobs, next spawn at $next_spawn sec.");
ce1aa9b7 200 }
02008cb1 201
dc24b520 202 if ($last_checkpoint + $spawn_interval < time()) {
02008cb1 203
4c2da349
AD
204 /* Check if schema version changed */
205
206 $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
ba68b681 207 if (!init_plugins($link)) die("Can't initialize db connection.\n");
4c2da349
AD
208 $test_schema_version = get_schema_version($link);
209 db_close($link);
210
211 if ($test_schema_version != $schema_version) {
f0e015c4
AD
212 echo "Expected schema version: $schema_version, got: $test_schema_version\n";
213 echo "Schema version changed while we were running, bailing out\n";
4c2da349
AD
214 exit(100);
215 }
216
c90a028c 217 check_ctimes();
5a613536
AD
218 reap_children();
219
dc24b520 220 for ($j = count($children); $j < $max_jobs; $j++) {
02008cb1
AD
221 $pid = pcntl_fork();
222 if ($pid == -1) {
223 die("fork failed!\n");
224 } else if ($pid) {
724b7942
AD
225
226 if (!$master_handlers_installed) {
227 _debug("[MASTER] installing shutdown handlers");
228 pcntl_signal(SIGINT, 'sigint_handler');
cfe6d444 229 register_shutdown_function('shutdown', posix_getpid());
724b7942
AD
230 $master_handlers_installed = true;
231 }
232
e9338405
AD
233 _debug("[MASTER] spawned client $j [PID:$pid]...");
234 array_push($children, $pid);
c90a028c 235 $ctimes[$pid] = time();
02008cb1
AD
236 } else {
237 pcntl_signal(SIGCHLD, SIG_IGN);
8ccaff02
AD
238 pcntl_signal(SIGINT, 'task_sigint_handler');
239
240 register_shutdown_function('task_shutdown');
241
242 $my_pid = posix_getpid();
243 $lock_filename = "update_daemon-$my_pid.lock";
244
245 $lock_handle = make_lockfile($lock_filename);
246
247 if (!$lock_handle) {
248 die("error: Can't create lockfile ($lock_filename). ".
249 "Maybe another daemon is already running.\n");
250 }
ffa7cbae
AD
251
252 // ****** Updating RSS code *******
253 // Only run in fork process.
254
255 $start_timestamp = time();
256
dbaa4e4a 257 $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
ffa7cbae 258
ba68b681 259 if (!init_plugins($link)) return;
ffa7cbae
AD
260
261 // We disable stamp file, since it is of no use in a multiprocess update.
262 // not really, tho for the time being -fox
263 if (!make_stampfile('update_daemon.stamp')) {
e81610d9 264 _debug("warning: unable to create stampfile\n");
dbaa4e4a 265 }
ffa7cbae 266
dbaa4e4a 267 // Call to the feed batch update function
842c2ab4 268 // and maybe regenerate feedbrowser cache
e3b54693 269
8292d05b
AD
270 $nf = 0;
271
32f3c02b
AD
272 _debug("Waiting before update [$j]..");
273 sleep($j*5);
842c2ab4
AD
274 $nf = update_daemon_common($link);
275
276 if (rand(0,100) > 50) {
e3b54693 277 $count = update_feedbrowser_cache($link);
e3b42c5a
AD
278 _debug("Feedbrowser updated, $count feeds processed.");
279
280 purge_orphans($link, true);
281
282 $rc = cleanup_tags($link, 14, 50000);
283
284 _debug("Cleaned $rc cached tags.");
285
41b82aa4
AD
286 global $pluginhost;
287 $pluginhost->run_hooks($pluginhost::HOOK_UPDATE_TASK, "hook_update_task", $op);
e3b54693 288 }
ffa7cbae 289
0d6a7147 290 _debug("Elapsed time: " . (time() - $start_timestamp) . " second(s)");
dbaa4e4a 291
8292d05b 292 if ($nf > 0) {
a26f0c17
AD
293 _debug("Feeds processed: $nf");
294
295 if (time() - $start_timestamp > 0) {
296 _debug("Feeds/minute: " . sprintf("%.2d", $nf/((time()-$start_timestamp)/60)));
297 }
8292d05b
AD
298 }
299
ffa7cbae
AD
300 db_close($link);
301
302 // We are in a fork.
303 // We wait a little before exiting to avoid to be faster than our parent process.
304 sleep(1);
8ccaff02
AD
305
306 unlink(LOCK_DIRECTORY . "/$lock_filename");
307
ffa7cbae 308 // We exit in order to avoid fork bombing.
02008cb1
AD
309 exit(0);
310 }
311 }
312 $last_checkpoint = time();
313 }
314 sleep(1);
315 }
316
317?>