]>
git.wh0rd.org - tt-rss.git/blob - update_daemon2.php
3 set_include_path(dirname(__FILE__
) ."/include" . PATH_SEPARATOR
.
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 "functions.php";
21 require_once "rssfuncs.php";
22 require_once "sanity_check.php";
23 require_once "config.php";
24 require_once "db.php";
25 require_once "db-prefs.php";
27 define('MAX_JOBS', 2);
28 define('SPAWN_INTERVAL', DAEMON_SLEEP_INTERVAL
);
30 if (!function_exists('pcntl_fork')) {
31 die("error: This script requires PHP compiled with PCNTL module.\n");
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.");
56 _debug("[reap_children] child $pid reaped.");
66 function check_ctimes() {
69 foreach (array_keys($ctimes) as $pid) {
70 $started = $ctimes[$pid];
72 if (time() - $started > MAX_CHILD_RUNTIME
) {
73 _debug("[MASTER] child process $pid seems to be stuck, aborting...");
74 posix_kill($pid, SIGKILL
);
79 function sigchld_handler($signal) {
80 $running_jobs = reap_children();
82 _debug("[SIGCHLD] jobs left: $running_jobs");
84 pcntl_waitpid(-1, $status, WNOHANG
);
88 if (file_exists(LOCK_DIRECTORY
. "/update_daemon.lock"))
89 unlink(LOCK_DIRECTORY
. "/update_daemon.lock");
92 function task_shutdown() {
93 $pid = posix_getpid();
95 if (file_exists(LOCK_DIRECTORY
. "/update_daemon-$pid.lock"))
96 unlink(LOCK_DIRECTORY
. "/update_daemon-$pid.lock");
99 function sigint_handler() {
101 die("[SIGINT] removing lockfile and exiting.\n");
104 function task_sigint_handler() {
106 die("[SIGINT] removing lockfile and exiting.\n");
109 pcntl_signal(SIGCHLD
, 'sigchld_handler');
111 if (file_is_locked("update_daemon.lock")) {
112 die("error: Can't create lockfile. ".
113 "Maybe another daemon is already running.\n");
116 // Try to lock a file in order to avoid concurrent update.
117 $lock_handle = make_lockfile("update_daemon.lock");
120 die("error: Can't create lockfile. ".
121 "Maybe another daemon is already running.\n");
124 /* if (!pcntl_fork()) {
125 pcntl_signal(SIGINT, 'sigint_handler');
126 register_shutdown_function('shutdown');
128 // Try to lock a file in order to avoid concurrent update.
129 $lock_handle = make_lockfile("update_daemon.lock");
132 die("error: Can't create lockfile. ".
133 "Maybe another daemon is already running.\n");
136 while (true) { sleep(100); }
139 // Testing database connection.
140 // It is unnecessary to start the fork loop if database is not ok.
141 $link = db_connect(DB_HOST
, DB_USER
, DB_PASS
, DB_NAME
);
143 if (!init_connection($link)) return;
149 // Since sleep is interupted by SIGCHLD, we need another way to
150 // respect the SPAWN_INTERVAL
151 $next_spawn = $last_checkpoint + SPAWN_INTERVAL
- time();
153 if ($next_spawn %
10 == 0) {
154 $running_jobs = count($children);
155 _debug("[MASTER] active jobs: $running_jobs, next spawn at $next_spawn sec.");
158 if ($last_checkpoint + SPAWN_INTERVAL
< time()) {
163 for ($j = count($children); $j < MAX_JOBS
; $j++
) {
166 die("fork failed!\n");
168 _debug("[MASTER] spawned client $j [PID:$pid]...");
169 array_push($children, $pid);
170 $ctimes[$pid] = time();
172 pcntl_signal(SIGCHLD
, SIG_IGN
);
173 pcntl_signal(SIGINT
, 'task_sigint_handler');
175 register_shutdown_function('task_shutdown');
177 $my_pid = posix_getpid();
178 $lock_filename = "update_daemon-$my_pid.lock";
180 $lock_handle = make_lockfile($lock_filename);
183 die("error: Can't create lockfile ($lock_filename). ".
184 "Maybe another daemon is already running.\n");
187 // ****** Updating RSS code *******
188 // Only run in fork process.
190 $start_timestamp = time();
192 $link = db_connect(DB_HOST
, DB_USER
, DB_PASS
, DB_NAME
);
194 if (!init_connection($link)) return;
196 // We disable stamp file, since it is of no use in a multiprocess update.
197 // not really, tho for the time being -fox
198 if (!make_stampfile('update_daemon.stamp')) {
199 print "warning: unable to create stampfile";
202 // Call to the feed batch update function
203 // or regenerate feedbrowser cache
205 if (rand(0,100) > 30) {
206 update_daemon_common($link);
208 $count = update_feedbrowser_cache($link);
209 _debug("Feedbrowser updated, $count feeds processed.");
211 purge_orphans($link, true);
213 $rc = cleanup_tags($link, 14, 50000);
215 _debug("Cleaned $rc cached tags.");
218 $pluginhost->run_hooks($pluginhost::HOOK_UPDATE_TASK
, "hook_update_task", $op);
221 _debug("Elapsed time: " . (time() - $start_timestamp) . " second(s)");
226 // We wait a little before exiting to avoid to be faster than our parent process.
229 unlink(LOCK_DIRECTORY
. "/$lock_filename");
231 // We exit in order to avoid fork bombing.
235 // We wait a little time before the next fork, in order to let the first fork
236 // mark the feeds it update :
239 $last_checkpoint = time();