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