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