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);
13 define('PURGE_INTERVAL', 3600); // seconds
15 require_once "sanity_check.php";
16 require_once "config.php";
18 if (!ENABLE_UPDATE_DAEMON
) {
19 die("Please enable option ENABLE_UPDATE_DAEMON in config.php\n");
22 require_once "db.php";
23 require_once "db-prefs.php";
24 require_once "functions.php";
25 require_once "magpierss/rss_fetch.inc";
27 function sigint_handler() {
28 unlink("update_daemon.lock");
29 die("Received SIGINT. Exiting.\n");
32 pcntl_signal(SIGINT
, sigint_handler
);
34 $lock_handle = make_lockfile("update_daemon.lock");
37 die("error: Can't create lockfile ($lock_filename). ".
38 "Maybe another daemon is already running.\n");
41 $link = db_connect(DB_HOST
, DB_USER
, DB_PASS
, DB_NAME
);
44 if (DB_TYPE
== "mysql") {
47 // PG seems to display its own errors just fine by default.
51 if (DB_TYPE
== "pgsql") {
52 pg_query("set client_encoding = 'utf-8'");
53 pg_set_client_encoding("UNICODE");
60 if (time() - $last_purge > PURGE_INTERVAL
) {
61 print "Purging old posts (random 30 feeds)...\n";
62 global_purge_old_posts($link, true, 30);
66 // FIXME: get all scheduled updates w/forced refetch
67 // Stub, until I figure out if it is really needed.
69 # $result = db_query($link, "SELECT * FROM ttrss_scheduled_updates ORDER BY id");
70 # while ($line = db_fetch_assoc($result)) {
71 # print "Scheduled feed update: " . $line["feed_id"] . ", UID: " .
72 # $line["owner_uid"] . "\n";
75 // Process all other feeds using last_updated and interval parameters
77 $random_qpart = sql_random_function();
80 ttrss_entries.date_entered < NOW() - INTERVAL '$purge_interval days'");
83 $rows = pg_affected_rows($result);
87 $result = db_query($link, "DELETE FROM ttrss_user_entries
88 USING ttrss_user_entries, ttrss_entries
89 WHERE ttrss_entries.id = ref_id AND
91 feed_id = '$feed_id' AND
92 ttrss_entries.date_entered < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)"); */
94 if (DAEMON_UPDATE_LOGIN_LIMIT
> 0) {
95 if (DB_TYPE
== "pgsql") {
96 $login_thresh_qpart = "AND ttrss_users.last_login >= NOW() - INTERVAL '".DAEMON_UPDATE_LOGIN_LIMIT
." days'";
98 $login_thresh_qpart = "AND ttrss_users.last_login >= DATE_SUB(NOW(), INTERVAL ".DAEMON_UPDATE_LOGIN_LIMIT
." DAY)";
101 $login_thresh_qpart = "";
104 $result = db_query($link, "SELECT feed_url,ttrss_feeds.id,owner_uid,
105 SUBSTRING(last_updated,1,19) AS last_updated,
108 ttrss_feeds,ttrss_users
110 ttrss_users.id = owner_uid $login_thresh_qpart
111 ORDER BY $random_qpart DESC");
113 $user_prefs_cache = array();
115 printf("Scheduled %d feeds to update...\n", db_num_rows($result));
117 while ($line = db_fetch_assoc($result)) {
119 $upd_intl = $line["update_interval"];
120 $user_id = $line["owner_uid"];
122 if (!$upd_intl ||
$upd_intl == 0) {
123 if (!$user_prefs_cache[$user_id]['DEFAULT_UPDATE_INTERVAL']) {
124 $upd_intl = get_pref($link, 'DEFAULT_UPDATE_INTERVAL', $user_id);
125 $user_prefs_cache[$user_id]['DEFAULT_UPDATE_INTERVAL'] = $upd_intl;
127 $upd_intl = $user_prefs_cache[$user_id]['DEFAULT_UPDATE_INTERVAL'];
132 # print "Updates disabled.\n";
136 print "Feed: " . $line["feed_url"] . ": ";
138 printf("(%d/%d, %d) ", time() - strtotime($line["last_updated"]),
139 $upd_intl*60, $user_id);
141 if (!$line["last_updated"] ||
142 time() - strtotime($line["last_updated"]) > ($upd_intl * 60)) {
144 print "Updating...\n";
145 update_rss_feed($link, $line["feed_url"], $line["id"], true);
146 sleep(1); // prevent flood (FIXME make this an option?)
148 print "Update not needed.\n";
152 if (DAEMON_SENDS_DIGESTS
) send_headlines_digests($link);
154 print "Sleeping for " . DAEMON_SLEEP_INTERVAL
. " seconds...\n";
156 sleep(DAEMON_SLEEP_INTERVAL
);