]> git.wh0rd.org - tt-rss.git/blob - update_daemon_loop.php
new option: ALLOW_SELECT_UPDATE_METHOD (bump config ver)
[tt-rss.git] / update_daemon_loop.php
1 #!/usr/bin/php
2 <?php
3 // this daemon runs in the background and updates all feeds
4 // continuously
5
6 if ($argv[1] != "SRV_RUN_OK") {
7 die("This script should be run by update_daemon.php\n");
8 }
9
10 // define('DEFAULT_ERROR_LEVEL', E_ALL);
11 define('DEFAULT_ERROR_LEVEL', E_ERROR | E_WARNING | E_PARSE);
12
13 declare(ticks = 1);
14
15 define('MAGPIE_CACHE_DIR', '/var/tmp/magpie-ttrss-cache-daemon');
16 define('SIMPLEPIE_CACHE_DIR', '/var/tmp/simplepie-ttrss-cache-daemon');
17 define('DISABLE_SESSIONS', true);
18
19 require_once "version.php";
20
21 if (strpos(VERSION, ".99") !== false) {
22 define('DAEMON_EXTENDED_DEBUG', true);
23 }
24
25 define('PURGE_INTERVAL', 3600); // seconds
26
27 require_once "sanity_check.php";
28 require_once "config.php";
29
30 if (!ENABLE_UPDATE_DAEMON) {
31 die("Please enable option ENABLE_UPDATE_DAEMON in config.php\n");
32 }
33
34 require_once "db.php";
35 require_once "db-prefs.php";
36 require_once "functions.php";
37 require_once "magpierss/rss_fetch.inc";
38
39 error_reporting(DEFAULT_ERROR_LEVEL);
40
41 function sigalrm_handler() {
42 die("received SIGALRM, hang in feed update?\n");
43 }
44
45 pcntl_signal(SIGALRM, sigalrm_handler);
46
47 $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
48
49 if (!$link) {
50 if (DB_TYPE == "mysql") {
51 print mysql_error();
52 }
53 // PG seems to display its own errors just fine by default.
54 return;
55 }
56
57 if (DB_TYPE == "pgsql") {
58 pg_query("set client_encoding = 'utf-8'");
59 pg_set_client_encoding("UNICODE");
60 } else {
61 if (defined('MYSQL_CHARSET') && MYSQL_CHARSET) {
62 db_query($link, "SET NAMES " . MYSQL_CHARSET);
63 // db_query($link, "SET CHARACTER SET " . MYSQL_CHARSET);
64 }
65 }
66
67 $last_purge = 0;
68
69 if (!make_stampfile('update_daemon.stamp')) {
70 print "error: unable to create stampfile";
71 die;
72 }
73
74 if (time() - $last_purge > PURGE_INTERVAL) {
75 _debug("Purging old posts (random 30 feeds)...");
76 global_purge_old_posts($link, true, 30);
77 $last_purge = time();
78 }
79
80 // FIXME: get all scheduled updates w/forced refetch
81 // Stub, until I figure out if it is really needed.
82
83 # $result = db_query($link, "SELECT * FROM ttrss_scheduled_updates ORDER BY id");
84 # while ($line = db_fetch_assoc($result)) {
85 # print "Scheduled feed update: " . $line["feed_id"] . ", UID: " .
86 # $line["owner_uid"] . "\n";
87 # }
88
89 // Process all other feeds using last_updated and interval parameters
90
91 $random_qpart = sql_random_function();
92
93 /*
94 ttrss_entries.date_entered < NOW() - INTERVAL '$purge_interval days'");
95 }
96
97 $rows = pg_affected_rows($result);
98
99 } else {
100
101 $result = db_query($link, "DELETE FROM ttrss_user_entries
102 USING ttrss_user_entries, ttrss_entries
103 WHERE ttrss_entries.id = ref_id AND
104 marked = false AND
105 feed_id = '$feed_id' AND
106 ttrss_entries.date_entered < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)"); */
107
108 if (DAEMON_UPDATE_LOGIN_LIMIT > 0) {
109 if (DB_TYPE == "pgsql") {
110 $login_thresh_qpart = "AND ttrss_users.last_login >= NOW() - INTERVAL '".DAEMON_UPDATE_LOGIN_LIMIT." days'";
111 } else {
112 $login_thresh_qpart = "AND ttrss_users.last_login >= DATE_SUB(NOW(), INTERVAL ".DAEMON_UPDATE_LOGIN_LIMIT." DAY)";
113 }
114 } else {
115 $login_thresh_qpart = "";
116 }
117
118 if (DB_TYPE == "pgsql") {
119 $update_limit_qpart = "AND ((
120 ttrss_feeds.update_interval = 0
121 AND ttrss_feeds.last_updated < NOW() - CAST((ttrss_user_prefs.value || ' minutes') AS INTERVAL)
122 ) OR (
123 ttrss_feeds.update_interval > 0
124 AND ttrss_feeds.last_updated < NOW() - CAST((ttrss_feeds.update_interval || ' minutes') AS INTERVAL)
125 ))";
126 } else {
127 $update_limit_qpart = "AND ((
128 ttrss_feeds.update_interval = 0
129 AND ttrss_feeds.last_updated < DATE_SUB(NOW(), INTERVAL CONVERT(ttrss_user_prefs.value, SIGNED INTEGER) MINUTE)
130 ) OR (
131 ttrss_feeds.update_interval > 0
132 AND ttrss_feeds.last_updated < DATE_SUB(NOW(), INTERVAL ttrss_feeds.update_interval MINUTE)
133 ))";
134 }
135
136 if (DB_TYPE == "pgsql") {
137 $updstart_thresh_qpart = "AND (ttrss_feeds.last_update_started IS NULL OR ttrss_feeds.last_update_started < NOW() - INTERVAL '120 seconds')";
138 } else {
139 $updstart_thresh_qpart = "AND (ttrss_feeds.last_update_started IS NULL OR ttrss_feeds.last_update_started < DATE_SUB(NOW(), INTERVAL 120 SECOND))";
140 }
141
142 $result = db_query($link, "SELECT ttrss_feeds.feed_url,ttrss_feeds.id, ttrss_feeds.owner_uid,
143 SUBSTRING(ttrss_feeds.last_updated,1,19) AS last_updated,
144 ttrss_feeds.update_interval
145 FROM
146 ttrss_feeds, ttrss_users, ttrss_user_prefs
147 WHERE
148 ttrss_feeds.owner_uid = ttrss_users.id
149 AND ttrss_users.id = ttrss_user_prefs.owner_uid
150 AND ttrss_user_prefs.pref_name='DEFAULT_UPDATE_INTERVAL'
151 $login_thresh_qpart $update_limit_qpart
152 $updstart_thresh_qpart
153 ORDER BY $random_qpart DESC LIMIT " . DAEMON_FEED_LIMIT);
154
155 $user_prefs_cache = array();
156
157 _debug(sprintf("Scheduled %d feeds to update...\n", db_num_rows($result)));
158
159 // Here is a little cache magic in order to minimize risk of double feed updates.
160 $feeds_to_update = array();
161 while ($line = db_fetch_assoc($result)) {
162 $feeds_to_update[$line['id']] = $line;
163 }
164
165 // We update the feed last update started date before anything else.
166 // There is no lag due to feed contents downloads
167 // It prevent an other process to update the same feed (for exemple, forced update by user).
168 $feed_ids = array_keys($feeds_to_update);
169 if($feed_ids) {
170 db_query($link, sprintf("UPDATE ttrss_feeds SET last_update_started = NOW()
171 WHERE id IN (%s)", implode(',', $feed_ids)));
172 }
173
174 while ($line = array_pop($feeds_to_update)) {
175
176 _debug("Feed: " . $line["feed_url"] . ", " . $line["last_updated"]);
177
178 pcntl_alarm(300);
179 update_rss_feed($link, $line["feed_url"], $line["id"], true);
180 pcntl_alarm(0);
181
182 sleep(1); // prevent flood (FIXME make this an option?)
183 }
184
185 if (DAEMON_SENDS_DIGESTS) send_headlines_digests($link);
186
187 db_close($link);
188
189 ?>