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