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 if (!ENABLE_UPDATE_DAEMON) {
30 die("error: Please enable option ENABLE_UPDATE_DAEMON in config.php\n");
33 require_once "db.php";
34 require_once "db-prefs.php";
35 require_once "functions.php";
36 require_once "lib/magpierss/rss_fetch.inc";
41 $last_checkpoint = -1;
43 function reap_children() {
49 foreach ($children as $pid) {
50 if (pcntl_waitpid($pid, $status, WNOHANG) != $pid) {
52 if (file_is_locked("update_daemon-$pid.lock")) {
53 array_push($tmp, $pid);
55 _debug("[reap_children] child $pid seems active but lockfile is unlocked.");
58 _debug("[reap_children] child $pid reaped.");
68 function check_ctimes() {
71 foreach (array_keys($ctimes) as $pid) {
72 $started = $ctimes[$pid];
74 if (time() - $started > MAX_CHILD_RUNTIME) {
75 _debug("[MASTER] child process $pid seems to be stuck, aborting...");
76 posix_kill($pid, SIGKILL);
81 function sigchld_handler($signal) {
82 $running_jobs = reap_children();
84 _debug("[SIGCHLD] jobs left: $running_jobs");
86 pcntl_waitpid(-1, $status, WNOHANG);
90 if (file_exists(LOCK_DIRECTORY . "/update_daemon.lock"))
91 unlink(LOCK_DIRECTORY . "/update_daemon.lock");
94 function task_shutdown() {
95 $pid = posix_getpid();
97 if (file_exists(LOCK_DIRECTORY . "/update_daemon-$pid.lock"))
98 unlink(LOCK_DIRECTORY . "/update_daemon-$pid.lock");
101 function sigint_handler() {
103 die("[SIGINT] removing lockfile and exiting.\n");
106 function task_sigint_handler() {
108 die("[SIGINT] removing lockfile and exiting.\n");
111 pcntl_signal(SIGCHLD, 'sigchld_handler');
113 if (file_is_locked("update_daemon.lock")) {
114 die("error: Can't create lockfile. ".
115 "Maybe another daemon is already running.\n");
119 pcntl_signal(SIGINT, 'sigint_handler');
120 register_shutdown_function('shutdown');
122 // Try to lock a file in order to avoid concurrent update.
123 $lock_handle = make_lockfile("update_daemon.lock");
126 die("error: Can't create lockfile. ".
127 "Maybe another daemon is already running.\n");
130 while (true) { sleep(100); }
133 // Testing database connection.
134 // It is unnecessary to start the fork loop if database is not ok.
135 $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
138 if (DB_TYPE == "mysql") {
141 // PG seems to display its own errors just fine by default.
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);
195 if (DB_TYPE == "mysql") {
198 // PG seems to display its own errors just fine by default.
202 init_connection($link);
204 // We disable stamp file, since it is of no use in a multiprocess update.
205 // not really, tho for the time being -fox
206 if (!make_stampfile('update_daemon.stamp')) {
207 print "warning: unable to create stampfile";
210 // Call to the feed batch update function
211 // or regenerate feedbrowser cache
213 if (rand(0,100) > 30) {
214 update_daemon_common($link);
216 $count = update_feedbrowser_cache($link);
217 _debug("Feedbrowser updated, $count feeds processed.");
219 purge_orphans($link, true);
221 $rc = cleanup_tags($link, 14, 50000);
223 _debug("Cleaned $rc cached tags.");
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();