]>
git.wh0rd.org - tt-rss.git/blob - update_daemon2.php
3 set_include_path(get_include_path() . PATH_SEPARATOR
. "include");
5 // This is an experimental multiprocess update daemon.
6 // Some configurable variable may be found below.
9 chdir(dirname(__FILE__
));
11 define('DISABLE_SESSIONS', true);
13 require_once "version.php";
15 if (strpos(VERSION
, ".99") !== false ||
getenv('DAEMON_XDEBUG')) {
16 define('DAEMON_EXTENDED_DEBUG', true);
19 define('PURGE_INTERVAL', 3600); // seconds
20 define('MAX_CHILD_RUNTIME', 600); // seconds
22 require_once "sanity_check.php";
23 require_once "config.php";
25 define('MAX_JOBS', 2);
26 define('SPAWN_INTERVAL', DAEMON_SLEEP_INTERVAL
);
28 if (!function_exists('pcntl_fork')) {
29 die("error: This script requires PHP compiled with PCNTL module.\n");
32 require_once "db.php";
33 require_once "db-prefs.php";
34 require_once "functions.php";
35 require_once "lib/magpierss/rss_fetch.inc";
40 $last_checkpoint = -1;
42 function reap_children() {
48 foreach ($children as $pid) {
49 if (pcntl_waitpid($pid, $status, WNOHANG
) != $pid) {
51 if (file_is_locked("update_daemon-$pid.lock")) {
52 array_push($tmp, $pid);
54 _debug("[reap_children] child $pid seems active but lockfile is unlocked.");
57 _debug("[reap_children] child $pid reaped.");
67 function check_ctimes() {
70 foreach (array_keys($ctimes) as $pid) {
71 $started = $ctimes[$pid];
73 if (time() - $started > MAX_CHILD_RUNTIME
) {
74 _debug("[MASTER] child process $pid seems to be stuck, aborting...");
75 posix_kill($pid, SIGKILL
);
80 function sigchld_handler($signal) {
81 $running_jobs = reap_children();
83 _debug("[SIGCHLD] jobs left: $running_jobs");
85 pcntl_waitpid(-1, $status, WNOHANG
);
89 if (file_exists(LOCK_DIRECTORY
. "/update_daemon.lock"))
90 unlink(LOCK_DIRECTORY
. "/update_daemon.lock");
93 function task_shutdown() {
94 $pid = posix_getpid();
96 if (file_exists(LOCK_DIRECTORY
. "/update_daemon-$pid.lock"))
97 unlink(LOCK_DIRECTORY
. "/update_daemon-$pid.lock");
100 function sigint_handler() {
102 die("[SIGINT] removing lockfile and exiting.\n");
105 function task_sigint_handler() {
107 die("[SIGINT] removing lockfile and exiting.\n");
110 pcntl_signal(SIGCHLD
, 'sigchld_handler');
112 if (file_is_locked("update_daemon.lock")) {
113 die("error: Can't create lockfile. ".
114 "Maybe another daemon is already running.\n");
118 pcntl_signal(SIGINT
, 'sigint_handler');
119 register_shutdown_function('shutdown');
121 // Try to lock a file in order to avoid concurrent update.
122 $lock_handle = make_lockfile("update_daemon.lock");
125 die("error: Can't create lockfile. ".
126 "Maybe another daemon is already running.\n");
129 while (true) { sleep(100); }
132 // Testing database connection.
133 // It is unnecessary to start the fork loop if database is not ok.
134 $link = db_connect(DB_HOST
, DB_USER
, DB_PASS
, DB_NAME
);
137 if (DB_TYPE
== "mysql") {
140 // PG seems to display its own errors just fine by default.
148 // Since sleep is interupted by SIGCHLD, we need another way to
149 // respect the SPAWN_INTERVAL
150 $next_spawn = $last_checkpoint + SPAWN_INTERVAL
- time();
152 if ($next_spawn %
10 == 0) {
153 $running_jobs = count($children);
154 _debug("[MASTER] active jobs: $running_jobs, next spawn at $next_spawn sec.");
157 if ($last_checkpoint + SPAWN_INTERVAL
< time()) {
162 for ($j = count($children); $j < MAX_JOBS
; $j++
) {
165 die("fork failed!\n");
167 _debug("[MASTER] spawned client $j [PID:$pid]...");
168 array_push($children, $pid);
169 $ctimes[$pid] = time();
171 pcntl_signal(SIGCHLD
, SIG_IGN
);
172 pcntl_signal(SIGINT
, 'task_sigint_handler');
174 register_shutdown_function('task_shutdown');
176 $my_pid = posix_getpid();
177 $lock_filename = "update_daemon-$my_pid.lock";
179 $lock_handle = make_lockfile($lock_filename);
182 die("error: Can't create lockfile ($lock_filename). ".
183 "Maybe another daemon is already running.\n");
186 // ****** Updating RSS code *******
187 // Only run in fork process.
189 $start_timestamp = time();
191 $link = db_connect(DB_HOST
, DB_USER
, DB_PASS
, DB_NAME
);
194 if (DB_TYPE
== "mysql") {
197 // PG seems to display its own errors just fine by default.
201 init_connection($link);
203 // We disable stamp file, since it is of no use in a multiprocess update.
204 // not really, tho for the time being -fox
205 if (!make_stampfile('update_daemon.stamp')) {
206 print "warning: unable to create stampfile";
209 // Call to the feed batch update function
210 // or regenerate feedbrowser cache
212 if (rand(0,100) > 30) {
213 update_daemon_common($link);
215 $count = update_feedbrowser_cache($link);
216 _debug("Feedbrowser updated, $count feeds processed.");
218 purge_orphans($link, true);
220 $rc = cleanup_tags($link, 14, 50000);
222 _debug("Cleaned $rc cached tags.");
224 _debug("Updating linked feeds...");
225 get_linked_feeds($link);
229 _debug("Elapsed time: " . (time() - $start_timestamp) . " second(s)");
234 // We wait a little before exiting to avoid to be faster than our parent process.
237 unlink(LOCK_DIRECTORY
. "/$lock_filename");
239 // We exit in order to avoid fork bombing.
243 // We wait a little time before the next fork, in order to let the first fork
244 // mark the feeds it update :
247 $last_checkpoint = time();