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