]>
Commit | Line | Data |
---|---|---|
02008cb1 AD |
1 | #!/usr/bin/php |
2 | <?php | |
ffa7cbae AD |
3 | // This is an experimental multiprocess update daemon. |
4 | // Some configurable variable may be found below. | |
5 | ||
6 | // define('DEFAULT_ERROR_LEVEL', E_ALL); | |
7 | define('DEFAULT_ERROR_LEVEL', E_ERROR | E_WARNING | E_PARSE); | |
02008cb1 AD |
8 | |
9 | declare(ticks = 1); | |
10 | ||
ffa7cbae AD |
11 | define('MAGPIE_CACHE_DIR', '/var/tmp/magpie-ttrss-cache-daemon'); |
12 | define('SIMPLEPIE_CACHE_DIR', '/var/tmp/simplepie-ttrss-cache-daemon'); | |
13 | define('DISABLE_SESSIONS', true); | |
02008cb1 AD |
14 | |
15 | define('MAX_JOBS', 2); | |
ffa7cbae AD |
16 | |
17 | require_once "version.php"; | |
18 | ||
010c16f1 | 19 | if (strpos(VERSION, ".99") !== false || getenv('DAEMON_XDEBUG')) { |
ffa7cbae AD |
20 | define('DAEMON_EXTENDED_DEBUG', true); |
21 | } | |
22 | ||
23 | define('PURGE_INTERVAL', 3600); // seconds | |
24 | ||
25 | require_once "sanity_check.php"; | |
26 | require_once "config.php"; | |
27 | ||
02008cb1 | 28 | define('SPAWN_INTERVAL', DAEMON_SLEEP_INTERVAL); |
ffa7cbae AD |
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); | |
02008cb1 | 40 | |
e9338405 AD |
41 | $children = array(); |
42 | ||
02008cb1 AD |
43 | $last_checkpoint = -1; |
44 | ||
5a613536 | 45 | function reap_children() { |
e9338405 AD |
46 | global $children; |
47 | ||
48 | $tmp = array(); | |
49 | ||
50 | foreach ($children as $pid) { | |
51 | if (pcntl_waitpid($pid, $status, WNOHANG) != $pid) { | |
52 | array_push($tmp, $pid); | |
53 | } else { | |
54 | _debug("[SIGCHLD] child $pid reaped."); | |
55 | } | |
56 | } | |
57 | ||
58 | $children = $tmp; | |
59 | ||
5a613536 AD |
60 | return count($tmp); |
61 | } | |
62 | ||
63 | function sigalrm_handler() { | |
64 | die("received SIGALRM, hang in feed update?\n"); | |
65 | } | |
66 | ||
67 | function sigchld_handler($signal) { | |
68 | $running_jobs = reap_children(); | |
e9338405 AD |
69 | |
70 | _debug("[SIGCHLD] jobs left: $running_jobs"); | |
5a613536 | 71 | |
02008cb1 AD |
72 | pcntl_waitpid(-1, $status, WNOHANG); |
73 | } | |
74 | ||
6a69e61f AD |
75 | function sigint_handler() { |
76 | unlink(LOCK_DIRECTORY . "/update_daemon.lock"); | |
77 | die("Received SIGINT. Exiting.\n"); | |
78 | } | |
79 | ||
ffa7cbae | 80 | pcntl_signal(SIGALRM, 'sigalrm_handler'); |
02008cb1 | 81 | pcntl_signal(SIGCHLD, 'sigchld_handler'); |
6a69e61f AD |
82 | pcntl_signal(SIGINT, 'sigint_handler'); |
83 | ||
884c0a36 AD |
84 | if (file_is_locked("update_daemon.lock")) { |
85 | die("error: Can't create lockfile. ". | |
6a69e61f AD |
86 | "Maybe another daemon is already running.\n"); |
87 | } | |
02008cb1 | 88 | |
ffa7cbae AD |
89 | if (file_is_locked("update_daemon.lock")) { |
90 | die("error: Can't create lockfile. ". | |
91 | "Maybe another daemon is already running.\n"); | |
92 | } | |
93 | ||
884c0a36 AD |
94 | if (!pcntl_fork()) { |
95 | $lock_handle = make_lockfile("update_daemon.lock"); | |
96 | ||
97 | if (!$lock_handle) { | |
98 | die("error: Can't create lockfile. ". | |
99 | "Maybe another daemon is already running.\n"); | |
100 | } | |
101 | ||
102 | while (true) { sleep(100); } | |
103 | } | |
104 | ||
ffa7cbae AD |
105 | // Testing database connection. |
106 | // It is unnecessary to start the fork loop if database is not ok. | |
107 | $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME); | |
108 | ||
109 | if (!$link) { | |
110 | if (DB_TYPE == "mysql") { | |
111 | print mysql_error(); | |
112 | } | |
113 | // PG seems to display its own errors just fine by default. | |
114 | return; | |
115 | } | |
116 | ||
117 | db_close($link); | |
118 | ||
119 | ||
02008cb1 AD |
120 | while (true) { |
121 | ||
122 | $next_spawn = $last_checkpoint + SPAWN_INTERVAL - time(); | |
123 | ||
ce1aa9b7 | 124 | if ($next_spawn % 10 == 0) { |
e9338405 AD |
125 | $running_jobs = count($children); |
126 | _debug("[MASTER] active jobs: $running_jobs, next spawn at $next_spawn sec."); | |
ce1aa9b7 | 127 | } |
02008cb1 AD |
128 | |
129 | if ($last_checkpoint + SPAWN_INTERVAL < time()) { | |
130 | ||
5a613536 AD |
131 | reap_children(); |
132 | ||
e9338405 | 133 | for ($j = count($children); $j < MAX_JOBS; $j++) { |
02008cb1 AD |
134 | $pid = pcntl_fork(); |
135 | if ($pid == -1) { | |
136 | die("fork failed!\n"); | |
137 | } else if ($pid) { | |
e9338405 AD |
138 | _debug("[MASTER] spawned client $j [PID:$pid]..."); |
139 | array_push($children, $pid); | |
02008cb1 AD |
140 | } else { |
141 | pcntl_signal(SIGCHLD, SIG_IGN); | |
6a69e61f | 142 | pcntl_signal(SIGINT, SIG_DFL); |
ffa7cbae AD |
143 | |
144 | // ****** Updating RSS code ******* | |
145 | // Only run in fork process. | |
146 | ||
147 | $start_timestamp = time(); | |
148 | ||
149 | $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME); | |
150 | ||
151 | if (!$link) { | |
152 | if (DB_TYPE == "mysql") { | |
153 | print mysql_error(); | |
154 | } | |
155 | // PG seems to display its own errors just fine by default. | |
156 | return; | |
157 | } | |
158 | ||
159 | if (DB_TYPE == "pgsql") { | |
160 | pg_query("set client_encoding = 'utf-8'"); | |
161 | pg_set_client_encoding("UNICODE"); | |
162 | } else { | |
163 | if (defined('MYSQL_CHARSET') && MYSQL_CHARSET) { | |
164 | db_query($link, "SET NAMES " . MYSQL_CHARSET); | |
165 | // db_query($link, "SET CHARACTER SET " . MYSQL_CHARSET); | |
166 | } | |
167 | } | |
168 | ||
169 | // We disable stamp file, since it is of no use in a multiprocess update. | |
170 | // not really, tho for the time being -fox | |
171 | if (!make_stampfile('update_daemon.stamp')) { | |
172 | print "warning: unable to create stampfile"; | |
173 | } | |
174 | ||
175 | // $last_purge = 0; | |
176 | ||
177 | // if (time() - $last_purge > PURGE_INTERVAL) { | |
178 | ||
179 | // FIXME : $last_purge is of no use in a multiprocess update. | |
180 | // FIXME : We ALWAYS purge old posts. | |
181 | _debug("Purging old posts (random 30 feeds)..."); | |
182 | global_purge_old_posts($link, true, 30); | |
183 | ||
184 | // $last_purge = time(); | |
185 | // } | |
186 | ||
187 | // Process all other feeds using last_updated and interval parameters | |
188 | ||
189 | $random_qpart = sql_random_function(); | |
190 | ||
191 | if (DAEMON_UPDATE_LOGIN_LIMIT > 0) { | |
192 | if (DB_TYPE == "pgsql") { | |
193 | $login_thresh_qpart = "AND ttrss_users.last_login >= NOW() - INTERVAL '".DAEMON_UPDATE_LOGIN_LIMIT." days'"; | |
194 | } else { | |
195 | $login_thresh_qpart = "AND ttrss_users.last_login >= DATE_SUB(NOW(), INTERVAL ".DAEMON_UPDATE_LOGIN_LIMIT." DAY)"; | |
196 | } | |
197 | } else { | |
198 | $login_thresh_qpart = ""; | |
199 | } | |
200 | ||
201 | if (DB_TYPE == "pgsql") { | |
202 | $update_limit_qpart = "AND ttrss_feeds.last_updated < NOW() - INTERVAL '".(DAEMON_SLEEP_INTERVAL*2)." seconds'"; | |
203 | } else { | |
204 | $update_limit_qpart = "AND ttrss_feeds.last_updated < DATE_SUB(NOW(), INTERVAL ".(DAEMON_SLEEP_INTERVAL*2)." SECOND)"; | |
205 | } | |
206 | ||
207 | if (DB_TYPE == "pgsql") { | |
208 | $updstart_thresh_qpart = "AND (ttrss_feeds.last_update_started IS NULL OR ttrss_feeds.last_update_started < NOW() - INTERVAL '120 seconds')"; | |
209 | } else { | |
210 | $updstart_thresh_qpart = "AND (ttrss_feeds.last_update_started IS NULL OR ttrss_feeds.last_update_started < DATE_SUB(NOW(), INTERVAL 120 SECOND))"; | |
211 | } | |
212 | ||
213 | $result = db_query($link, "SELECT feed_url,ttrss_feeds.id,owner_uid, | |
214 | SUBSTRING(last_updated,1,19) AS last_updated, | |
215 | update_interval | |
216 | FROM | |
217 | ttrss_feeds,ttrss_users | |
218 | WHERE | |
219 | ttrss_users.id = owner_uid $login_thresh_qpart $update_limit_qpart | |
220 | $updstart_thresh_qpart | |
221 | ORDER BY $random_qpart DESC LIMIT " . DAEMON_FEED_LIMIT); | |
222 | ||
223 | $user_prefs_cache = array(); | |
224 | ||
225 | _debug(sprintf("Scheduled %d feeds to update...\n", db_num_rows($result))); | |
226 | ||
227 | // Here is a little cache magic in order to minimize risk of double feed updates. | |
228 | $feeds_to_update = array(); | |
229 | while ($line = db_fetch_assoc($result)) { | |
230 | $feeds_to_update[$line['id']] = $line; | |
231 | } | |
232 | ||
233 | // We update the feed last update started date before anything else. | |
234 | // There is no lag due to feed contents downloads | |
235 | // It prevent an other process to update the same feed. | |
236 | $feed_ids = array_keys($feeds_to_update); | |
237 | if($feed_ids) { | |
238 | db_query($link, sprintf("UPDATE ttrss_feeds SET last_update_started = NOW() | |
239 | WHERE id IN (%s)", implode(',', $feed_ids))); | |
240 | } | |
241 | ||
242 | while ($line = array_pop($feeds_to_update)) { | |
243 | ||
244 | $upd_intl = $line["update_interval"]; | |
245 | $user_id = $line["owner_uid"]; | |
246 | ||
247 | if (!$upd_intl || $upd_intl == 0) { | |
248 | if (!$user_prefs_cache[$user_id]['DEFAULT_UPDATE_INTERVAL']) { | |
249 | $upd_intl = get_pref($link, 'DEFAULT_UPDATE_INTERVAL', $user_id); | |
250 | $user_prefs_cache[$user_id]['DEFAULT_UPDATE_INTERVAL'] = $upd_intl; | |
251 | } else { | |
252 | $upd_intl = $user_prefs_cache[$user_id]['DEFAULT_UPDATE_INTERVAL']; | |
253 | } | |
254 | } | |
255 | ||
256 | if ($upd_intl < 0) { | |
257 | # print "Updates disabled.\n"; | |
258 | continue; | |
259 | } | |
260 | ||
261 | _debug("Feed: " . $line["feed_url"] . ", " . $line["last_updated"]); | |
262 | ||
263 | // _debug(sprintf("\tLU: %d, INTL: %d, UID: %d) ", | |
264 | // time() - strtotime($line["last_updated"]), $upd_intl*60, $user_id)); | |
265 | ||
266 | if (!$line["last_updated"] || | |
267 | time() - strtotime($line["last_updated"]) > ($upd_intl * 60)) { | |
268 | ||
269 | _debug("Updating..."); | |
270 | ||
271 | pcntl_alarm(300); | |
272 | ||
273 | update_rss_feed($link, $line["feed_url"], $line["id"], true); | |
274 | ||
275 | pcntl_alarm(0); | |
276 | ||
277 | sleep(1); // prevent flood (FIXME make this an option?) | |
278 | } else { | |
279 | _debug("Update not needed."); | |
280 | } | |
281 | } | |
282 | ||
283 | if (DAEMON_SENDS_DIGESTS) send_headlines_digests($link); | |
284 | ||
285 | print "Elapsed time: " . (time() - $start_timestamp) . " second(s)\n"; | |
286 | ||
287 | db_close($link); | |
288 | ||
289 | // We are in a fork. | |
290 | // We wait a little before exiting to avoid to be faster than our parent process. | |
291 | sleep(1); | |
292 | // We exit in order to avoid fork bombing. | |
02008cb1 AD |
293 | exit(0); |
294 | } | |
295 | } | |
296 | $last_checkpoint = time(); | |
297 | } | |
298 | sleep(1); | |
299 | } | |
300 | ||
301 | ?> |