]> git.wh0rd.org - tt-rss.git/blob - update_daemon.php
add rudimentary hang-check based on pcntl_alarm
[tt-rss.git] / update_daemon.php
1 #!/usr/bin/php
2 <?php
3 // this daemon runs in the background and updates all feeds
4 // continuously
5
6 define('DEFAULT_ERROR_LEVEL', E_ALL);
7
8 declare(ticks = 1);
9
10 define('MAGPIE_CACHE_DIR', '/var/tmp/magpie-ttrss-cache-daemon');
11 define('DISABLE_SESSIONS', true);
12 define('DAEMON_EXTENDED_DEBUG', true);
13
14 define('PURGE_INTERVAL', 3600); // seconds
15
16 require_once "sanity_check.php";
17 require_once "config.php";
18
19 if (!ENABLE_UPDATE_DAEMON) {
20 die("Please enable option ENABLE_UPDATE_DAEMON in config.php\n");
21 }
22
23 require_once "db.php";
24 require_once "db-prefs.php";
25 require_once "functions.php";
26 require_once "magpierss/rss_fetch.inc";
27
28 error_reporting(E_ALL);
29
30 function sigint_handler() {
31 unlink("update_daemon.lock");
32 die("Received SIGINT. Exiting.\n");
33 }
34
35 function sigalrm_handler() {
36 die("received SIGALRM, hang in feed update?\n");
37 }
38
39 pcntl_signal(SIGINT, sigint_handler);
40 pcntl_signal(SIGALRM, sigalrm_handler);
41
42 $lock_handle = make_lockfile("update_daemon.lock");
43
44 if (!$lock_handle) {
45 die("error: Can't create lockfile ($lock_filename). ".
46 "Maybe another daemon is already running.\n");
47 }
48
49 $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
50
51 if (!$link) {
52 if (DB_TYPE == "mysql") {
53 print mysql_error();
54 }
55 // PG seems to display its own errors just fine by default.
56 return;
57 }
58
59 if (DB_TYPE == "pgsql") {
60 pg_query("set client_encoding = 'utf-8'");
61 pg_set_client_encoding("UNICODE");
62 }
63
64 $last_purge = 0;
65
66 while (true) {
67
68 if (time() - $last_purge > PURGE_INTERVAL) {
69 _debug("Purging old posts (random 30 feeds)...");
70 global_purge_old_posts($link, true, 30);
71 $last_purge = time();
72 }
73
74 // FIXME: get all scheduled updates w/forced refetch
75 // Stub, until I figure out if it is really needed.
76
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";
81 # }
82
83 // Process all other feeds using last_updated and interval parameters
84
85 $random_qpart = sql_random_function();
86
87 /*
88 ttrss_entries.date_entered < NOW() - INTERVAL '$purge_interval days'");
89 }
90
91 $rows = pg_affected_rows($result);
92
93 } else {
94
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
98 marked = false AND
99 feed_id = '$feed_id' AND
100 ttrss_entries.date_entered < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)"); */
101
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'";
105 } else {
106 $login_thresh_qpart = "AND ttrss_users.last_login >= DATE_SUB(NOW(), INTERVAL ".DAEMON_UPDATE_LOGIN_LIMIT." DAY)";
107 }
108 } else {
109 $login_thresh_qpart = "";
110 }
111
112 $result = db_query($link, "SELECT feed_url,ttrss_feeds.id,owner_uid,
113 SUBSTRING(last_updated,1,19) AS last_updated,
114 update_interval
115 FROM
116 ttrss_feeds,ttrss_users
117 WHERE
118 ttrss_users.id = owner_uid $login_thresh_qpart
119 ORDER BY $random_qpart DESC");
120
121 $user_prefs_cache = array();
122
123 _debug(sprintf("Scheduled %d feeds to update...\n", db_num_rows($result)));
124
125 while ($line = db_fetch_assoc($result)) {
126
127 $upd_intl = $line["update_interval"];
128 $user_id = $line["owner_uid"];
129
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;
134 } else {
135 $upd_intl = $user_prefs_cache[$user_id]['DEFAULT_UPDATE_INTERVAL'];
136 }
137 }
138
139 if ($upd_intl < 0) {
140 # print "Updates disabled.\n";
141 continue;
142 }
143
144 _debug("Feed: " . $line["feed_url"]);
145
146 // _debug(sprintf("\tLU: %d, INTL: %d, UID: %d) ",
147 // time() - strtotime($line["last_updated"]), $upd_intl*60, $user_id));
148
149 if (!$line["last_updated"] ||
150 time() - strtotime($line["last_updated"]) > ($upd_intl * 60)) {
151
152 _debug("Updating...");
153
154 if (defined('MAGPIE_FETCH_TIME_OUT')) {
155 pcntl_alarm(MAGPIE_FETCH_TIME_OUT * 2);
156 } else {
157 pcntl_alarm(300);
158 }
159
160 update_rss_feed($link, $line["feed_url"], $line["id"], true);
161
162 pcntl_alarm(0);
163
164 sleep(1); // prevent flood (FIXME make this an option?)
165 } else {
166 _debug("Update not needed.");
167 }
168 }
169
170 if (DAEMON_SENDS_DIGESTS) send_headlines_digests($link);
171
172 _debug("Sleeping for " . DAEMON_SLEEP_INTERVAL . " seconds...");
173
174 sleep(DAEMON_SLEEP_INTERVAL);
175 }
176
177 db_close($link);
178
179 ?>