]>
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.
8 define('DISABLE_SESSIONS', true);
10 require_once "version.php";
12 if (strpos(VERSION
, ".99") !== false ||
getenv('DAEMON_XDEBUG')) {
13 define('DAEMON_EXTENDED_DEBUG', true);
16 define('PURGE_INTERVAL', 3600); // seconds
17 define('MAX_CHILD_RUNTIME', 600); // seconds
19 require_once "sanity_check.php";
20 require_once "config.php";
22 define('MAX_JOBS', 2);
23 define('SPAWN_INTERVAL', DAEMON_SLEEP_INTERVAL
);
25 if (!function_exists('pcntl_fork')) {
26 die("error: This script requires PHP compiled with PCNTL module.\n");
29 require_once "db.php";
30 require_once "db-prefs.php";
31 require_once "functions.php";
32 require_once "lib/magpierss/rss_fetch.inc";
37 $last_checkpoint = -1;
39 function reap_children() {
45 foreach ($children as $pid) {
46 if (pcntl_waitpid($pid, $status, WNOHANG
) != $pid) {
48 if (file_is_locked("update_daemon-$pid.lock")) {
49 array_push($tmp, $pid);
51 _debug("[reap_children] child $pid seems active but lockfile is unlocked.");
54 _debug("[reap_children] child $pid reaped.");
64 function check_ctimes() {
67 foreach (array_keys($ctimes) as $pid) {
68 $started = $ctimes[$pid];
70 if (time() - $started > MAX_CHILD_RUNTIME
) {
71 _debug("[MASTER] child process $pid seems to be stuck, aborting...");
72 posix_kill($pid, SIGKILL
);
77 function sigchld_handler($signal) {
78 $running_jobs = reap_children();
80 _debug("[SIGCHLD] jobs left: $running_jobs");
82 pcntl_waitpid(-1, $status, WNOHANG
);
86 if (file_exists(LOCK_DIRECTORY
. "/update_daemon.lock"))
87 unlink(LOCK_DIRECTORY
. "/update_daemon.lock");
90 function task_shutdown() {
91 $pid = posix_getpid();
93 if (file_exists(LOCK_DIRECTORY
. "/update_daemon-$pid.lock"))
94 unlink(LOCK_DIRECTORY
. "/update_daemon-$pid.lock");
97 function sigint_handler() {
99 die("[SIGINT] removing lockfile and exiting.\n");
102 function task_sigint_handler() {
104 die("[SIGINT] removing lockfile and exiting.\n");
107 pcntl_signal(SIGCHLD
, 'sigchld_handler');
109 if (file_is_locked("update_daemon.lock")) {
110 die("error: Can't create lockfile. ".
111 "Maybe another daemon is already running.\n");
115 pcntl_signal(SIGINT
, 'sigint_handler');
116 register_shutdown_function('shutdown');
118 // Try to lock a file in order to avoid concurrent update.
119 $lock_handle = make_lockfile("update_daemon.lock");
122 die("error: Can't create lockfile. ".
123 "Maybe another daemon is already running.\n");
126 while (true) { sleep(100); }
129 // Testing database connection.
130 // It is unnecessary to start the fork loop if database is not ok.
131 $link = db_connect(DB_HOST
, DB_USER
, DB_PASS
, DB_NAME
);
134 if (DB_TYPE
== "mysql") {
137 // PG seems to display its own errors just fine by default.
145 // Since sleep is interupted by SIGCHLD, we need another way to
146 // respect the SPAWN_INTERVAL
147 $next_spawn = $last_checkpoint + SPAWN_INTERVAL
- time();
149 if ($next_spawn %
10 == 0) {
150 $running_jobs = count($children);
151 _debug("[MASTER] active jobs: $running_jobs, next spawn at $next_spawn sec.");
154 if ($last_checkpoint + SPAWN_INTERVAL
< time()) {
159 for ($j = count($children); $j < MAX_JOBS
; $j++
) {
162 die("fork failed!\n");
164 _debug("[MASTER] spawned client $j [PID:$pid]...");
165 array_push($children, $pid);
166 $ctimes[$pid] = time();
168 pcntl_signal(SIGCHLD
, SIG_IGN
);
169 pcntl_signal(SIGINT
, 'task_sigint_handler');
171 register_shutdown_function('task_shutdown');
173 $my_pid = posix_getpid();
174 $lock_filename = "update_daemon-$my_pid.lock";
176 $lock_handle = make_lockfile($lock_filename);
179 die("error: Can't create lockfile ($lock_filename). ".
180 "Maybe another daemon is already running.\n");
183 // ****** Updating RSS code *******
184 // Only run in fork process.
186 $start_timestamp = time();
188 $link = db_connect(DB_HOST
, DB_USER
, DB_PASS
, DB_NAME
);
191 if (DB_TYPE
== "mysql") {
194 // PG seems to display its own errors just fine by default.
198 init_connection($link);
200 // We disable stamp file, since it is of no use in a multiprocess update.
201 // not really, tho for the time being -fox
202 if (!make_stampfile('update_daemon.stamp')) {
203 print "warning: unable to create stampfile";
206 // Call to the feed batch update function
207 // or regenerate feedbrowser cache
209 if (rand(0,100) > 30) {
210 update_daemon_common($link);
212 $count = update_feedbrowser_cache($link);
213 _debug("Feedbrowser updated, $count feeds processed.");
215 purge_orphans($link, true);
217 $rc = cleanup_tags($link, 14, 50000);
219 _debug("Cleaned $rc cached tags.");
223 _debug("Elapsed time: " . (time() - $start_timestamp) . " second(s)");
228 // We wait a little before exiting to avoid to be faster than our parent process.
231 unlink(LOCK_DIRECTORY
. "/$lock_filename");
233 // We exit in order to avoid fork bombing.
237 // We wait a little time before the next fork, in order to let the first fork
238 // mark the feeds it update :
241 $last_checkpoint = time();