]> git.wh0rd.org - tt-rss.git/blame - update_daemon.php
add some more daemon debugging
[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');
de696427 12 define('DISABLE_SESSIONS', true);
34e420fb
AD
13
14 require_once "version.php";
15
16 if (strpos(VERSION, ".99") !== false) {
17 define('DAEMON_EXTENDED_DEBUG', true);
18 }
de696427 19
3b3d116e
AD
20 define('PURGE_INTERVAL', 3600); // seconds
21
de696427
AD
22 require_once "sanity_check.php";
23 require_once "config.php";
c6784aea
AD
24
25 if (!ENABLE_UPDATE_DAEMON) {
69ff793a 26 die("Please enable option ENABLE_UPDATE_DAEMON in config.php\n");
c6784aea
AD
27 }
28
de696427
AD
29 require_once "db.php";
30 require_once "db-prefs.php";
31 require_once "functions.php";
32 require_once "magpierss/rss_fetch.inc";
33
34e420fb 34 error_reporting(DEFAULT_ERROR_LEVEL);
219bd8fc 35
6eafcac6
AD
36 function sigint_handler() {
37 unlink("update_daemon.lock");
38 die("Received SIGINT. Exiting.\n");
39 }
40
34e420fb
AD
41// function sigalrm_handler() {
42// print "SIGALARM\n";
43// pcntl_alarm(3);
44// }
45
6eafcac6 46 pcntl_signal(SIGINT, sigint_handler);
34e420fb 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");
de696427
AD
69 }
70
3b3d116e
AD
71 $last_purge = 0;
72
1f4ad53c 73 while (true) {
a545b564 74
3b3d116e 75 if (time() - $last_purge > PURGE_INTERVAL) {
6f9e33e4 76 _debug("Purging old posts (random 30 feeds)...");
894ebcf5 77 global_purge_old_posts($link, true, 30);
3b3d116e
AD
78 $last_purge = time();
79 }
1f4ad53c 80
085a5a74 81 // FIXME: get all scheduled updates w/forced refetch
6eafcac6 82 // Stub, until I figure out if it is really needed.
1f4ad53c 83
085a5a74
AD
84# $result = db_query($link, "SELECT * FROM ttrss_scheduled_updates ORDER BY id");
85# while ($line = db_fetch_assoc($result)) {
86# print "Scheduled feed update: " . $line["feed_id"] . ", UID: " .
87# $line["owner_uid"] . "\n";
88# }
1f4ad53c
AD
89
90 // Process all other feeds using last_updated and interval parameters
91
894ebcf5
AD
92 $random_qpart = sql_random_function();
93
eed55fd3
AD
94/*
95 ttrss_entries.date_entered < NOW() - INTERVAL '$purge_interval days'");
96 }
97
98 $rows = pg_affected_rows($result);
99
100 } else {
101
102 $result = db_query($link, "DELETE FROM ttrss_user_entries
103 USING ttrss_user_entries, ttrss_entries
104 WHERE ttrss_entries.id = ref_id AND
105 marked = false AND
106 feed_id = '$feed_id' AND
107 ttrss_entries.date_entered < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)"); */
108
109 if (DAEMON_UPDATE_LOGIN_LIMIT > 0) {
110 if (DB_TYPE == "pgsql") {
111 $login_thresh_qpart = "AND ttrss_users.last_login >= NOW() - INTERVAL '".DAEMON_UPDATE_LOGIN_LIMIT." days'";
112 } else {
113 $login_thresh_qpart = "AND ttrss_users.last_login >= DATE_SUB(NOW(), INTERVAL ".DAEMON_UPDATE_LOGIN_LIMIT." DAY)";
114 }
115 } else {
116 $login_thresh_qpart = "";
117 }
118
119 $result = db_query($link, "SELECT feed_url,ttrss_feeds.id,owner_uid,
120 SUBSTRING(last_updated,1,19) AS last_updated,
121 update_interval
122 FROM
123 ttrss_feeds,ttrss_users
124 WHERE
125 ttrss_users.id = owner_uid $login_thresh_qpart
126 ORDER BY $random_qpart DESC");
44e241cb
AD
127
128 $user_prefs_cache = array();
129
6f9e33e4 130 _debug(sprintf("Scheduled %d feeds to update...\n", db_num_rows($result)));
eed55fd3 131
1f4ad53c
AD
132 while ($line = db_fetch_assoc($result)) {
133
1f4ad53c 134 $upd_intl = $line["update_interval"];
1f4ad53c
AD
135 $user_id = $line["owner_uid"];
136
137 if (!$upd_intl || $upd_intl == 0) {
44e241cb
AD
138 if (!$user_prefs_cache[$user_id]['DEFAULT_UPDATE_INTERVAL']) {
139 $upd_intl = get_pref($link, 'DEFAULT_UPDATE_INTERVAL', $user_id);
140 $user_prefs_cache[$user_id]['DEFAULT_UPDATE_INTERVAL'] = $upd_intl;
141 } else {
142 $upd_intl = $user_prefs_cache[$user_id]['DEFAULT_UPDATE_INTERVAL'];
143 }
1f4ad53c 144 }
c1e202b7
AD
145
146 if ($upd_intl < 0) {
1011b66a 147# print "Updates disabled.\n";
c1e202b7
AD
148 continue;
149 }
1f4ad53c 150
6f9e33e4 151 _debug("Feed: " . $line["feed_url"]);
1011b66a 152
6f9e33e4
AD
153// _debug(sprintf("\tLU: %d, INTL: %d, UID: %d) ",
154// time() - strtotime($line["last_updated"]), $upd_intl*60, $user_id));
1f4ad53c
AD
155
156 if (!$line["last_updated"] ||
157 time() - strtotime($line["last_updated"]) > ($upd_intl * 60)) {
6f9e33e4 158
072bd2cc 159 _debug("Updating...");
6f9e33e4 160
6eafcac6 161 update_rss_feed($link, $line["feed_url"], $line["id"], true);
44e241cb 162 sleep(1); // prevent flood (FIXME make this an option?)
6eafcac6 163 } else {
072bd2cc 164 _debug("Update not needed.");
1f4ad53c 165 }
de696427
AD
166 }
167
99018440 168 if (DAEMON_SENDS_DIGESTS) send_headlines_digests($link);
9cd7c995 169
6f9e33e4 170 _debug("Sleeping for " . DAEMON_SLEEP_INTERVAL . " seconds...");
1f4ad53c 171
78ea1de0 172 sleep(DAEMON_SLEEP_INTERVAL);
de696427
AD
173 }
174
de696427
AD
175 db_close($link);
176
177?>