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