3 // This is an experimental multiprocess update daemon.
4 // Some configurable variable may be found below.
6 // define('DEFAULT_ERROR_LEVEL', E_ALL);
7 define('DEFAULT_ERROR_LEVEL', E_ERROR | E_WARNING | E_PARSE
);
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
21 require_once "sanity_check.php";
22 require_once "config.php";
24 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 if (!ENABLE_UPDATE_DAEMON
) {
33 die("error: Please enable option ENABLE_UPDATE_DAEMON in config.php\n");
36 require_once "db.php";
37 require_once "db-prefs.php";
38 require_once "functions.php";
39 require_once "lib/magpierss/rss_fetch.inc";
41 error_reporting(DEFAULT_ERROR_LEVEL
);
45 $last_checkpoint = -1;
47 function reap_children() {
52 foreach ($children as $pid) {
53 if (pcntl_waitpid($pid, $status, WNOHANG
) != $pid) {
54 array_push($tmp, $pid);
56 _debug("[SIGCHLD] child $pid reaped.");
65 function sigalrm_handler() {
66 die("[SIGALRM] hang in feed update?\n");
69 function sigchld_handler($signal) {
70 $running_jobs = reap_children();
72 _debug("[SIGCHLD] jobs left: $running_jobs");
74 pcntl_waitpid(-1, $status, WNOHANG
);
77 function sigint_handler() {
78 unlink(LOCK_DIRECTORY
. "/update_daemon.lock");
79 die("[SIGINT] removing lockfile and exiting.\n");
82 pcntl_signal(SIGALRM
, 'sigalrm_handler');
83 pcntl_signal(SIGCHLD
, 'sigchld_handler');
85 if (file_is_locked("update_daemon.lock")) {
86 die("error: Can't create lockfile. ".
87 "Maybe another daemon is already running.\n");
91 pcntl_signal(SIGINT
, 'sigint_handler');
93 // Try to lock a file in order to avoid concurrent update.
94 $lock_handle = make_lockfile("update_daemon.lock");
97 die("error: Can't create lockfile. ".
98 "Maybe another daemon is already running.\n");
101 while (true) { sleep(100); }
104 // Testing database connection.
105 // It is unnecessary to start the fork loop if database is not ok.
106 $link = db_connect(DB_HOST
, DB_USER
, DB_PASS
, DB_NAME
);
109 if (DB_TYPE
== "mysql") {
112 // PG seems to display its own errors just fine by default.
120 // Since sleep is interupted by SIGCHLD, we need another way to
121 // respect the SPAWN_INTERVAL
122 $next_spawn = $last_checkpoint + SPAWN_INTERVAL
- time();
124 if ($next_spawn %
10 == 0) {
125 $running_jobs = count($children);
126 _debug("[MASTER] active jobs: $running_jobs, next spawn at $next_spawn sec.");
129 if ($last_checkpoint + SPAWN_INTERVAL
< time()) {
133 for ($j = count($children); $j < MAX_JOBS
; $j++
) {
136 die("fork failed!\n");
138 _debug("[MASTER] spawned client $j [PID:$pid]...");
139 array_push($children, $pid);
141 pcntl_signal(SIGCHLD
, SIG_IGN
);
142 pcntl_signal(SIGINT
, SIG_DFL
);
144 // ****** Updating RSS code *******
145 // Only run in fork process.
147 $start_timestamp = time();
149 $link = db_connect(DB_HOST
, DB_USER
, DB_PASS
, DB_NAME
);
152 if (DB_TYPE
== "mysql") {
155 // PG seems to display its own errors just fine by default.
159 init_connection($link);
161 // We disable stamp file, since it is of no use in a multiprocess update.
162 // not really, tho for the time being -fox
163 if (!make_stampfile('update_daemon.stamp')) {
164 print "warning: unable to create stampfile";
167 // FIXME : $last_purge is of no use in a multiprocess update.
168 // FIXME : We ALWAYS purge old posts.
169 //_debug("Purging old posts (random 30 feeds)...");
170 //global_purge_old_posts($link, true, 30);
172 // Call to the feed batch update function
173 // or regenerate feedbrowser cache
175 if (rand(0,100) > 50) {
176 update_daemon_common($link);
178 $count = update_feedbrowser_cache($link);
179 _debug("Finished, $count feeds processed.");
182 _debug("Elapsed time: " . (time() - $start_timestamp) . " second(s)");
187 // We wait a little before exiting to avoid to be faster than our parent process.
189 // We exit in order to avoid fork bombing.
193 // We wait a little time before the next fork, in order to let the first fork
194 // mark the feeds it update :
197 $last_checkpoint = time();