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