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('MAGPIE_CACHE_DIR', '/var/tmp/magpie-ttrss-cache-daemon');
12 define('SIMPLEPIE_CACHE_DIR', '/var/tmp/simplepie-ttrss-cache-daemon');
13 define('DISABLE_SESSIONS', true);
15 define('MAX_JOBS', 2);
17 require_once "version.php";
19 if (strpos(VERSION
, ".99") !== false ||
getenv('DAEMON_XDEBUG')) {
20 define('DAEMON_EXTENDED_DEBUG', true);
23 define('PURGE_INTERVAL', 3600); // seconds
25 require_once "sanity_check.php";
26 require_once "config.php";
28 define('SPAWN_INTERVAL', DAEMON_SLEEP_INTERVAL
);
30 if (!ENABLE_UPDATE_DAEMON
) {
31 die("Please enable option ENABLE_UPDATE_DAEMON in config.php\n");
34 require_once "db.php";
35 require_once "db-prefs.php";
36 require_once "functions.php";
37 require_once "magpierss/rss_fetch.inc";
39 error_reporting(DEFAULT_ERROR_LEVEL
);
43 $last_checkpoint = -1;
45 function sigalrm_handler() {
46 die("received SIGALRM, hang in feed update?\n");
49 function sigchld_handler($signal) {
54 foreach ($children as $pid) {
55 if (pcntl_waitpid($pid, $status, WNOHANG
) != $pid) {
56 array_push($tmp, $pid);
58 _debug("[SIGCHLD] child $pid reaped.");
64 $running_jobs = count($children);
66 _debug("[SIGCHLD] jobs left: $running_jobs");
67 pcntl_waitpid(-1, $status, WNOHANG
);
70 function sigint_handler() {
71 unlink(LOCK_DIRECTORY
. "/update_daemon.lock");
72 die("Received SIGINT. Exiting.\n");
75 pcntl_signal(SIGALRM
, 'sigalrm_handler');
76 pcntl_signal(SIGCHLD
, 'sigchld_handler');
77 pcntl_signal(SIGINT
, 'sigint_handler');
79 if (file_is_locked("update_daemon.lock")) {
80 die("error: Can't create lockfile. ".
81 "Maybe another daemon is already running.\n");
84 if (file_is_locked("update_daemon.lock")) {
85 die("error: Can't create lockfile. ".
86 "Maybe another daemon is already running.\n");
90 $lock_handle = make_lockfile("update_daemon.lock");
93 die("error: Can't create lockfile. ".
94 "Maybe another daemon is already running.\n");
97 while (true) { sleep(100); }
100 // Testing database connection.
101 // It is unnecessary to start the fork loop if database is not ok.
102 $link = db_connect(DB_HOST
, DB_USER
, DB_PASS
, DB_NAME
);
105 if (DB_TYPE
== "mysql") {
108 // PG seems to display its own errors just fine by default.
117 $next_spawn = $last_checkpoint + SPAWN_INTERVAL
- time();
119 if ($next_spawn %
10 == 0) {
120 $running_jobs = count($children);
121 _debug("[MASTER] active jobs: $running_jobs, next spawn at $next_spawn sec.");
124 if ($last_checkpoint + SPAWN_INTERVAL
< time()) {
126 for ($j = count($children); $j < MAX_JOBS
; $j++
) {
129 die("fork failed!\n");
131 _debug("[MASTER] spawned client $j [PID:$pid]...");
132 array_push($children, $pid);
134 pcntl_signal(SIGCHLD
, SIG_IGN
);
135 pcntl_signal(SIGINT
, SIG_DFL
);
137 // ****** Updating RSS code *******
138 // Only run in fork process.
140 $start_timestamp = time();
142 $link = db_connect(DB_HOST
, DB_USER
, DB_PASS
, DB_NAME
);
145 if (DB_TYPE
== "mysql") {
148 // PG seems to display its own errors just fine by default.
152 if (DB_TYPE
== "pgsql") {
153 pg_query("set client_encoding = 'utf-8'");
154 pg_set_client_encoding("UNICODE");
156 if (defined('MYSQL_CHARSET') && MYSQL_CHARSET
) {
157 db_query($link, "SET NAMES " . MYSQL_CHARSET
);
158 // db_query($link, "SET CHARACTER SET " . MYSQL_CHARSET);
162 // We disable stamp file, since it is of no use in a multiprocess update.
163 // not really, tho for the time being -fox
164 if (!make_stampfile('update_daemon.stamp')) {
165 print "warning: unable to create stampfile";
170 // if (time() - $last_purge > PURGE_INTERVAL) {
172 // FIXME : $last_purge is of no use in a multiprocess update.
173 // FIXME : We ALWAYS purge old posts.
174 _debug("Purging old posts (random 30 feeds)...");
175 global_purge_old_posts($link, true, 30);
177 // $last_purge = time();
180 // Process all other feeds using last_updated and interval parameters
182 $random_qpart = sql_random_function();
184 if (DAEMON_UPDATE_LOGIN_LIMIT
> 0) {
185 if (DB_TYPE
== "pgsql") {
186 $login_thresh_qpart = "AND ttrss_users.last_login >= NOW() - INTERVAL '".DAEMON_UPDATE_LOGIN_LIMIT
." days'";
188 $login_thresh_qpart = "AND ttrss_users.last_login >= DATE_SUB(NOW(), INTERVAL ".DAEMON_UPDATE_LOGIN_LIMIT
." DAY)";
191 $login_thresh_qpart = "";
194 if (DB_TYPE
== "pgsql") {
195 $update_limit_qpart = "AND ttrss_feeds.last_updated < NOW() - INTERVAL '".(DAEMON_SLEEP_INTERVAL
*2)." seconds'";
197 $update_limit_qpart = "AND ttrss_feeds.last_updated < DATE_SUB(NOW(), INTERVAL ".(DAEMON_SLEEP_INTERVAL
*2)." SECOND)";
200 if (DB_TYPE
== "pgsql") {
201 $updstart_thresh_qpart = "AND (ttrss_feeds.last_update_started IS NULL OR ttrss_feeds.last_update_started < NOW() - INTERVAL '120 seconds')";
203 $updstart_thresh_qpart = "AND (ttrss_feeds.last_update_started IS NULL OR ttrss_feeds.last_update_started < DATE_SUB(NOW(), INTERVAL 120 SECOND))";
206 $result = db_query($link, "SELECT feed_url,ttrss_feeds.id,owner_uid,
207 SUBSTRING(last_updated,1,19) AS last_updated,
210 ttrss_feeds,ttrss_users
212 ttrss_users.id = owner_uid $login_thresh_qpart $update_limit_qpart
213 $updstart_thresh_qpart
214 ORDER BY $random_qpart DESC LIMIT " . DAEMON_FEED_LIMIT
);
216 $user_prefs_cache = array();
218 _debug(sprintf("Scheduled %d feeds to update...\n", db_num_rows($result)));
220 // Here is a little cache magic in order to minimize risk of double feed updates.
221 $feeds_to_update = array();
222 while ($line = db_fetch_assoc($result)) {
223 $feeds_to_update[$line['id']] = $line;
226 // We update the feed last update started date before anything else.
227 // There is no lag due to feed contents downloads
228 // It prevent an other process to update the same feed.
229 $feed_ids = array_keys($feeds_to_update);
231 db_query($link, sprintf("UPDATE ttrss_feeds SET last_update_started = NOW()
232 WHERE id IN (%s)", implode(',', $feed_ids)));
235 while ($line = array_pop($feeds_to_update)) {
237 $upd_intl = $line["update_interval"];
238 $user_id = $line["owner_uid"];
240 if (!$upd_intl ||
$upd_intl == 0) {
241 if (!$user_prefs_cache[$user_id]['DEFAULT_UPDATE_INTERVAL']) {
242 $upd_intl = get_pref($link, 'DEFAULT_UPDATE_INTERVAL', $user_id);
243 $user_prefs_cache[$user_id]['DEFAULT_UPDATE_INTERVAL'] = $upd_intl;
245 $upd_intl = $user_prefs_cache[$user_id]['DEFAULT_UPDATE_INTERVAL'];
250 # print "Updates disabled.\n";
254 _debug("Feed: " . $line["feed_url"] . ", " . $line["last_updated"]);
256 // _debug(sprintf("\tLU: %d, INTL: %d, UID: %d) ",
257 // time() - strtotime($line["last_updated"]), $upd_intl*60, $user_id));
259 if (!$line["last_updated"] ||
260 time() - strtotime($line["last_updated"]) > ($upd_intl * 60)) {
262 _debug("Updating...");
266 update_rss_feed($link, $line["feed_url"], $line["id"], true);
270 sleep(1); // prevent flood (FIXME make this an option?)
272 _debug("Update not needed.");
276 if (DAEMON_SENDS_DIGESTS
) send_headlines_digests($link);
278 print "Elapsed time: " . (time() - $start_timestamp) . " second(s)\n";
283 // We wait a little before exiting to avoid to be faster than our parent process.
285 // We exit in order to avoid fork bombing.
289 $last_checkpoint = time();