3 // this daemon runs in the background and updates all feeds
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('DISABLE_SESSIONS', true);
14 require_once "version.php";
16 if (strpos(VERSION
, ".99") !== false) {
17 define('DAEMON_EXTENDED_DEBUG', true);
20 define('PURGE_INTERVAL', 3600); // seconds
22 require_once "sanity_check.php";
23 require_once "config.php";
25 if (!ENABLE_UPDATE_DAEMON
) {
26 die("Please enable option ENABLE_UPDATE_DAEMON in config.php\n");
29 require_once "db.php";
30 require_once "db-prefs.php";
31 require_once "functions.php";
32 require_once "magpierss/rss_fetch.inc";
34 error_reporting(DEFAULT_ERROR_LEVEL
);
36 function sigint_handler() {
37 unlink("update_daemon.lock");
38 die("Received SIGINT. Exiting.\n");
41 function sigalrm_handler() {
42 die("received SIGALRM, hang in feed update?\n");
45 pcntl_signal(SIGINT
, sigint_handler
);
46 pcntl_signal(SIGALRM
, sigalrm_handler
);
48 $lock_handle = make_lockfile("update_daemon.lock");
51 die("error: Can't create lockfile ($lock_filename). ".
52 "Maybe another daemon is already running.\n");
55 $link = db_connect(DB_HOST
, DB_USER
, DB_PASS
, DB_NAME
);
58 if (DB_TYPE
== "mysql") {
61 // PG seems to display its own errors just fine by default.
65 if (DB_TYPE
== "pgsql") {
66 pg_query("set client_encoding = 'utf-8'");
67 pg_set_client_encoding("UNICODE");
69 if (defined('MYSQL_CHARSET') && MYSQL_CHARSET
) {
70 db_query($link, "SET NAMES " . MYSQL_CHARSET
);
71 // db_query($link, "SET CHARACTER SET " . MYSQL_CHARSET);
79 if (!make_stampfile('update_daemon.stamp')) {
80 print "error: unable to create stampfile";
84 if (time() - $last_purge > PURGE_INTERVAL
) {
85 _debug("Purging old posts (random 30 feeds)...");
86 global_purge_old_posts($link, true, 30);
90 // FIXME: get all scheduled updates w/forced refetch
91 // Stub, until I figure out if it is really needed.
93 # $result = db_query($link, "SELECT * FROM ttrss_scheduled_updates ORDER BY id");
94 # while ($line = db_fetch_assoc($result)) {
95 # print "Scheduled feed update: " . $line["feed_id"] . ", UID: " .
96 # $line["owner_uid"] . "\n";
99 // Process all other feeds using last_updated and interval parameters
101 $random_qpart = sql_random_function();
104 ttrss_entries.date_entered < NOW() - INTERVAL '$purge_interval days'");
107 $rows = pg_affected_rows($result);
111 $result = db_query($link, "DELETE FROM ttrss_user_entries
112 USING ttrss_user_entries, ttrss_entries
113 WHERE ttrss_entries.id = ref_id AND
115 feed_id = '$feed_id' AND
116 ttrss_entries.date_entered < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)"); */
118 if (DAEMON_UPDATE_LOGIN_LIMIT
> 0) {
119 if (DB_TYPE
== "pgsql") {
120 $login_thresh_qpart = "AND ttrss_users.last_login >= NOW() - INTERVAL '".DAEMON_UPDATE_LOGIN_LIMIT
." days'";
122 $login_thresh_qpart = "AND ttrss_users.last_login >= DATE_SUB(NOW(), INTERVAL ".DAEMON_UPDATE_LOGIN_LIMIT
." DAY)";
125 $login_thresh_qpart = "";
128 $result = db_query($link, "SELECT feed_url,ttrss_feeds.id,owner_uid,
129 SUBSTRING(last_updated,1,19) AS last_updated,
132 ttrss_feeds,ttrss_users
134 ttrss_users.id = owner_uid $login_thresh_qpart
135 ORDER BY $random_qpart DESC");
137 $user_prefs_cache = array();
139 _debug(sprintf("Scheduled %d feeds to update...\n", db_num_rows($result)));
141 while ($line = db_fetch_assoc($result)) {
143 $upd_intl = $line["update_interval"];
144 $user_id = $line["owner_uid"];
146 if (!$upd_intl ||
$upd_intl == 0) {
147 if (!$user_prefs_cache[$user_id]['DEFAULT_UPDATE_INTERVAL']) {
148 $upd_intl = get_pref($link, 'DEFAULT_UPDATE_INTERVAL', $user_id);
149 $user_prefs_cache[$user_id]['DEFAULT_UPDATE_INTERVAL'] = $upd_intl;
151 $upd_intl = $user_prefs_cache[$user_id]['DEFAULT_UPDATE_INTERVAL'];
156 # print "Updates disabled.\n";
160 _debug("Feed: " . $line["feed_url"]);
162 // _debug(sprintf("\tLU: %d, INTL: %d, UID: %d) ",
163 // time() - strtotime($line["last_updated"]), $upd_intl*60, $user_id));
165 if (!$line["last_updated"] ||
166 time() - strtotime($line["last_updated"]) > ($upd_intl * 60)) {
168 _debug("Updating...");
172 update_rss_feed($link, $line["feed_url"], $line["id"], true);
176 sleep(1); // prevent flood (FIXME make this an option?)
178 _debug("Update not needed.");
182 if (DAEMON_SENDS_DIGESTS
) send_headlines_digests($link);
184 _debug("Sleeping for " . DAEMON_SLEEP_INTERVAL
. " seconds...");
186 sleep(DAEMON_SLEEP_INTERVAL
);