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('SIMPLEPIE_CACHE_DIR', '/var/tmp/simplepie-ttrss-cache-daemon');
13 define('DISABLE_SESSIONS', true);
15 require_once "version.php";
17 if (strpos(VERSION
, ".99") !== false) {
18 define('DAEMON_EXTENDED_DEBUG', true);
21 define('PURGE_INTERVAL', 3600); // seconds
23 require_once "sanity_check.php";
24 require_once "config.php";
26 if (!ENABLE_UPDATE_DAEMON
) {
27 die("Please enable option ENABLE_UPDATE_DAEMON in config.php\n");
30 require_once "db.php";
31 require_once "db-prefs.php";
32 require_once "functions.php";
33 require_once "magpierss/rss_fetch.inc";
35 error_reporting(DEFAULT_ERROR_LEVEL
);
37 function sigint_handler() {
38 unlink("update_daemon.lock");
39 die("Received SIGINT. Exiting.\n");
42 function sigalrm_handler() {
43 die("received SIGALRM, hang in feed update?\n");
46 pcntl_signal(SIGINT
, sigint_handler
);
47 pcntl_signal(SIGALRM
, sigalrm_handler
);
49 $lock_handle = make_lockfile("update_daemon.lock");
52 die("error: Can't create lockfile ($lock_filename). ".
53 "Maybe another daemon is already running.\n");
56 $link = db_connect(DB_HOST
, DB_USER
, DB_PASS
, DB_NAME
);
59 if (DB_TYPE
== "mysql") {
62 // PG seems to display its own errors just fine by default.
66 if (DB_TYPE
== "pgsql") {
67 pg_query("set client_encoding = 'utf-8'");
68 pg_set_client_encoding("UNICODE");
70 if (defined('MYSQL_CHARSET') && MYSQL_CHARSET
) {
71 db_query($link, "SET NAMES " . MYSQL_CHARSET
);
72 // db_query($link, "SET CHARACTER SET " . MYSQL_CHARSET);
80 if (!make_stampfile('update_daemon.stamp')) {
81 print "error: unable to create stampfile";
85 if (time() - $last_purge > PURGE_INTERVAL
) {
86 _debug("Purging old posts (random 30 feeds)...");
87 global_purge_old_posts($link, true, 30);
91 // FIXME: get all scheduled updates w/forced refetch
92 // Stub, until I figure out if it is really needed.
94 # $result = db_query($link, "SELECT * FROM ttrss_scheduled_updates ORDER BY id");
95 # while ($line = db_fetch_assoc($result)) {
96 # print "Scheduled feed update: " . $line["feed_id"] . ", UID: " .
97 # $line["owner_uid"] . "\n";
100 // Process all other feeds using last_updated and interval parameters
102 $random_qpart = sql_random_function();
105 ttrss_entries.date_entered < NOW() - INTERVAL '$purge_interval days'");
108 $rows = pg_affected_rows($result);
112 $result = db_query($link, "DELETE FROM ttrss_user_entries
113 USING ttrss_user_entries, ttrss_entries
114 WHERE ttrss_entries.id = ref_id AND
116 feed_id = '$feed_id' AND
117 ttrss_entries.date_entered < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)"); */
119 if (DAEMON_UPDATE_LOGIN_LIMIT
> 0) {
120 if (DB_TYPE
== "pgsql") {
121 $login_thresh_qpart = "AND ttrss_users.last_login >= NOW() - INTERVAL '".DAEMON_UPDATE_LOGIN_LIMIT
." days'";
123 $login_thresh_qpart = "AND ttrss_users.last_login >= DATE_SUB(NOW(), INTERVAL ".DAEMON_UPDATE_LOGIN_LIMIT
." DAY)";
126 $login_thresh_qpart = "";
129 if (DB_TYPE
== "pgsql") {
130 $update_limit_qpart = "AND ttrss_feeds.last_updated < NOW() - INTERVAL '".(DAEMON_SLEEP_INTERVAL
*2)." seconds'";
132 $update_limit_qpart = "AND ttrss_feeds.last_updated < DATE_SUB(NOW(), INTERVAL ".(DAEMON_SLEEP_INTERVAL
*2)." SECOND)";
135 $result = db_query($link, "SELECT feed_url,ttrss_feeds.id,owner_uid,
136 SUBSTRING(last_updated,1,19) AS last_updated,
139 ttrss_feeds,ttrss_users
141 ttrss_users.id = owner_uid $login_thresh_qpart $update_limit_qpart
142 ORDER BY $random_qpart DESC LIMIT " . DAEMON_FEED_LIMIT
);
144 $user_prefs_cache = array();
146 _debug(sprintf("Scheduled %d feeds to update...\n", db_num_rows($result)));
148 while ($line = db_fetch_assoc($result)) {
150 $upd_intl = $line["update_interval"];
151 $user_id = $line["owner_uid"];
153 if (!$upd_intl ||
$upd_intl == 0) {
154 if (!$user_prefs_cache[$user_id]['DEFAULT_UPDATE_INTERVAL']) {
155 $upd_intl = get_pref($link, 'DEFAULT_UPDATE_INTERVAL', $user_id);
156 $user_prefs_cache[$user_id]['DEFAULT_UPDATE_INTERVAL'] = $upd_intl;
158 $upd_intl = $user_prefs_cache[$user_id]['DEFAULT_UPDATE_INTERVAL'];
163 # print "Updates disabled.\n";
167 _debug("Feed: " . $line["feed_url"] . ", " . $line["last_updated"]);
169 // _debug(sprintf("\tLU: %d, INTL: %d, UID: %d) ",
170 // time() - strtotime($line["last_updated"]), $upd_intl*60, $user_id));
172 if (!$line["last_updated"] ||
173 time() - strtotime($line["last_updated"]) > ($upd_intl * 60)) {
175 _debug("Updating...");
179 update_rss_feed($link, $line["feed_url"], $line["id"], true);
183 sleep(1); // prevent flood (FIXME make this an option?)
185 _debug("Update not needed.");
189 if (DAEMON_SENDS_DIGESTS
) send_headlines_digests($link);
191 _debug("Sleeping for " . DAEMON_SLEEP_INTERVAL
. " seconds...");
193 sleep(DAEMON_SLEEP_INTERVAL
);