3 // this daemon runs in the background and updates all feeds
6 define('DEFAULT_ERROR_LEVEL', E_ALL
);
10 define('MAGPIE_CACHE_DIR', '/var/tmp/magpie-ttrss-cache-daemon');
11 define('DISABLE_SESSIONS', true);
12 define('DAEMON_EXTENDED_DEBUG', true);
14 define('PURGE_INTERVAL', 3600); // seconds
16 require_once "sanity_check.php";
17 require_once "config.php";
19 if (!ENABLE_UPDATE_DAEMON
) {
20 die("Please enable option ENABLE_UPDATE_DAEMON in config.php\n");
23 require_once "db.php";
24 require_once "db-prefs.php";
25 require_once "functions.php";
26 require_once "magpierss/rss_fetch.inc";
28 error_reporting(E_ALL
);
30 function sigint_handler() {
31 unlink("update_daemon.lock");
32 die("Received SIGINT. Exiting.\n");
35 function sigalrm_handler() {
36 die("received SIGALRM, hang in feed update?\n");
39 pcntl_signal(SIGINT
, sigint_handler
);
40 pcntl_signal(SIGALRM
, sigalrm_handler
);
42 $lock_handle = make_lockfile("update_daemon.lock");
45 die("error: Can't create lockfile ($lock_filename). ".
46 "Maybe another daemon is already running.\n");
49 $link = db_connect(DB_HOST
, DB_USER
, DB_PASS
, DB_NAME
);
52 if (DB_TYPE
== "mysql") {
55 // PG seems to display its own errors just fine by default.
59 if (DB_TYPE
== "pgsql") {
60 pg_query("set client_encoding = 'utf-8'");
61 pg_set_client_encoding("UNICODE");
68 if (time() - $last_purge > PURGE_INTERVAL
) {
69 _debug("Purging old posts (random 30 feeds)...");
70 global_purge_old_posts($link, true, 30);
74 // FIXME: get all scheduled updates w/forced refetch
75 // Stub, until I figure out if it is really needed.
77 # $result = db_query($link, "SELECT * FROM ttrss_scheduled_updates ORDER BY id");
78 # while ($line = db_fetch_assoc($result)) {
79 # print "Scheduled feed update: " . $line["feed_id"] . ", UID: " .
80 # $line["owner_uid"] . "\n";
83 // Process all other feeds using last_updated and interval parameters
85 $random_qpart = sql_random_function();
88 ttrss_entries.date_entered < NOW() - INTERVAL '$purge_interval days'");
91 $rows = pg_affected_rows($result);
95 $result = db_query($link, "DELETE FROM ttrss_user_entries
96 USING ttrss_user_entries, ttrss_entries
97 WHERE ttrss_entries.id = ref_id AND
99 feed_id = '$feed_id' AND
100 ttrss_entries.date_entered < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)"); */
102 if (DAEMON_UPDATE_LOGIN_LIMIT
> 0) {
103 if (DB_TYPE
== "pgsql") {
104 $login_thresh_qpart = "AND ttrss_users.last_login >= NOW() - INTERVAL '".DAEMON_UPDATE_LOGIN_LIMIT
." days'";
106 $login_thresh_qpart = "AND ttrss_users.last_login >= DATE_SUB(NOW(), INTERVAL ".DAEMON_UPDATE_LOGIN_LIMIT
." DAY)";
109 $login_thresh_qpart = "";
112 $result = db_query($link, "SELECT feed_url,ttrss_feeds.id,owner_uid,
113 SUBSTRING(last_updated,1,19) AS last_updated,
116 ttrss_feeds,ttrss_users
118 ttrss_users.id = owner_uid $login_thresh_qpart
119 ORDER BY $random_qpart DESC");
121 $user_prefs_cache = array();
123 _debug(sprintf("Scheduled %d feeds to update...\n", db_num_rows($result)));
125 while ($line = db_fetch_assoc($result)) {
127 $upd_intl = $line["update_interval"];
128 $user_id = $line["owner_uid"];
130 if (!$upd_intl ||
$upd_intl == 0) {
131 if (!$user_prefs_cache[$user_id]['DEFAULT_UPDATE_INTERVAL']) {
132 $upd_intl = get_pref($link, 'DEFAULT_UPDATE_INTERVAL', $user_id);
133 $user_prefs_cache[$user_id]['DEFAULT_UPDATE_INTERVAL'] = $upd_intl;
135 $upd_intl = $user_prefs_cache[$user_id]['DEFAULT_UPDATE_INTERVAL'];
140 # print "Updates disabled.\n";
144 _debug("Feed: " . $line["feed_url"]);
146 // _debug(sprintf("\tLU: %d, INTL: %d, UID: %d) ",
147 // time() - strtotime($line["last_updated"]), $upd_intl*60, $user_id));
149 if (!$line["last_updated"] ||
150 time() - strtotime($line["last_updated"]) > ($upd_intl * 60)) {
152 _debug("Updating...");
154 if (defined('MAGPIE_FETCH_TIME_OUT')) {
155 pcntl_alarm(MAGPIE_FETCH_TIME_OUT
* 2);
160 update_rss_feed($link, $line["feed_url"], $line["id"], true);
164 sleep(1); // prevent flood (FIXME make this an option?)
166 _debug("Update not needed.");
170 if (DAEMON_SENDS_DIGESTS
) send_headlines_digests($link);
172 _debug("Sleeping for " . DAEMON_SLEEP_INTERVAL
. " seconds...");
174 sleep(DAEMON_SLEEP_INTERVAL
);