]>
git.wh0rd.org - tt-rss.git/blob - update_daemon2.php
3 // This is an experimental multiprocess update daemon.
4 // Some configurable variable may be found below.
7 chdir(dirname(__FILE__
));
9 define('DISABLE_SESSIONS', true);
11 require_once "version.php";
13 if (strpos(VERSION
, ".99") !== false ||
getenv('DAEMON_XDEBUG')) {
14 define('DAEMON_EXTENDED_DEBUG', true);
17 define('PURGE_INTERVAL', 3600); // seconds
18 define('MAX_CHILD_RUNTIME', 600); // seconds
20 require_once "sanity_check.php";
21 require_once "config.php";
23 define('MAX_JOBS', 2);
24 define('SPAWN_INTERVAL', DAEMON_SLEEP_INTERVAL
);
26 if (!function_exists('pcntl_fork')) {
27 die("error: This script requires PHP compiled with PCNTL module.\n");
30 require_once "db.php";
31 require_once "db-prefs.php";
32 require_once "functions.php";
33 require_once "lib/magpierss/rss_fetch.inc";
38 $last_checkpoint = -1;
40 function reap_children() {
46 foreach ($children as $pid) {
47 if (pcntl_waitpid($pid, $status, WNOHANG
) != $pid) {
49 if (file_is_locked("update_daemon-$pid.lock")) {
50 array_push($tmp, $pid);
52 _debug("[reap_children] child $pid seems active but lockfile is unlocked.");
55 _debug("[reap_children] child $pid reaped.");
65 function check_ctimes() {
68 foreach (array_keys($ctimes) as $pid) {
69 $started = $ctimes[$pid];
71 if (time() - $started > MAX_CHILD_RUNTIME
) {
72 _debug("[MASTER] child process $pid seems to be stuck, aborting...");
73 posix_kill($pid, SIGKILL
);
78 function sigchld_handler($signal) {
79 $running_jobs = reap_children();
81 _debug("[SIGCHLD] jobs left: $running_jobs");
83 pcntl_waitpid(-1, $status, WNOHANG
);
87 if (file_exists(LOCK_DIRECTORY
. "/update_daemon.lock"))
88 unlink(LOCK_DIRECTORY
. "/update_daemon.lock");
91 function task_shutdown() {
92 $pid = posix_getpid();
94 if (file_exists(LOCK_DIRECTORY
. "/update_daemon-$pid.lock"))
95 unlink(LOCK_DIRECTORY
. "/update_daemon-$pid.lock");
98 function sigint_handler() {
100 die("[SIGINT] removing lockfile and exiting.\n");
103 function task_sigint_handler() {
105 die("[SIGINT] removing lockfile and exiting.\n");
108 pcntl_signal(SIGCHLD
, 'sigchld_handler');
110 if (file_is_locked("update_daemon.lock")) {
111 die("error: Can't create lockfile. ".
112 "Maybe another daemon is already running.\n");
116 pcntl_signal(SIGINT
, 'sigint_handler');
117 register_shutdown_function('shutdown');
119 // Try to lock a file in order to avoid concurrent update.
120 $lock_handle = make_lockfile("update_daemon.lock");
123 die("error: Can't create lockfile. ".
124 "Maybe another daemon is already running.\n");
127 while (true) { sleep(100); }
130 // Testing database connection.
131 // It is unnecessary to start the fork loop if database is not ok.
132 $link = db_connect(DB_HOST
, DB_USER
, DB_PASS
, DB_NAME
);
135 if (DB_TYPE
== "mysql") {
138 // PG seems to display its own errors just fine by default.
146 // Since sleep is interupted by SIGCHLD, we need another way to
147 // respect the SPAWN_INTERVAL
148 $next_spawn = $last_checkpoint + SPAWN_INTERVAL
- time();
150 if ($next_spawn %
10 == 0) {
151 $running_jobs = count($children);
152 _debug("[MASTER] active jobs: $running_jobs, next spawn at $next_spawn sec.");
155 if ($last_checkpoint + SPAWN_INTERVAL
< time()) {
160 for ($j = count($children); $j < MAX_JOBS
; $j++
) {
163 die("fork failed!\n");
165 _debug("[MASTER] spawned client $j [PID:$pid]...");
166 array_push($children, $pid);
167 $ctimes[$pid] = time();
169 pcntl_signal(SIGCHLD
, SIG_IGN
);
170 pcntl_signal(SIGINT
, 'task_sigint_handler');
172 register_shutdown_function('task_shutdown');
174 $my_pid = posix_getpid();
175 $lock_filename = "update_daemon-$my_pid.lock";
177 $lock_handle = make_lockfile($lock_filename);
180 die("error: Can't create lockfile ($lock_filename). ".
181 "Maybe another daemon is already running.\n");
184 // ****** Updating RSS code *******
185 // Only run in fork process.
187 $start_timestamp = time();
189 $link = db_connect(DB_HOST
, DB_USER
, DB_PASS
, DB_NAME
);
192 if (DB_TYPE
== "mysql") {
195 // PG seems to display its own errors just fine by default.
199 init_connection($link);
201 // We disable stamp file, since it is of no use in a multiprocess update.
202 // not really, tho for the time being -fox
203 if (!make_stampfile('update_daemon.stamp')) {
204 print "warning: unable to create stampfile";
207 // Call to the feed batch update function
208 // or regenerate feedbrowser cache
210 if (rand(0,100) > 30) {
211 update_daemon_common($link);
213 $count = update_feedbrowser_cache($link);
214 _debug("Feedbrowser updated, $count feeds processed.");
216 purge_orphans($link, true);
218 $rc = cleanup_tags($link, 14, 50000);
220 _debug("Cleaned $rc cached tags.");
222 _debug("Updating linked feeds...");
223 get_linked_feeds($link);
227 _debug("Elapsed time: " . (time() - $start_timestamp) . " second(s)");
232 // We wait a little before exiting to avoid to be faster than our parent process.
235 unlink(LOCK_DIRECTORY
. "/$lock_filename");
237 // We exit in order to avoid fork bombing.
241 // We wait a little time before the next fork, in order to let the first fork
242 // mark the feeds it update :
245 $last_checkpoint = time();