]> git.wh0rd.org - tt-rss.git/blame - update_daemon.php
UNDO: add (disabled) dialog to recategorize feeds via draganddrop
[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
4052b32d
AD
41 function sigalrm_handler() {
42 die("received SIGALRM, hang in feed update?\n");
43 }
44
6eafcac6 45 pcntl_signal(SIGINT, sigint_handler);
4052b32d 46 pcntl_signal(SIGALRM, sigalrm_handler);
6eafcac6
AD
47
48 $lock_handle = make_lockfile("update_daemon.lock");
49
50 if (!$lock_handle) {
51 die("error: Can't create lockfile ($lock_filename). ".
69ff793a 52 "Maybe another daemon is already running.\n");
6eafcac6
AD
53 }
54
de696427
AD
55 $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
56
57 if (!$link) {
58 if (DB_TYPE == "mysql") {
59 print mysql_error();
60 }
61 // PG seems to display its own errors just fine by default.
62 return;
63 }
64
65 if (DB_TYPE == "pgsql") {
66 pg_query("set client_encoding = 'utf-8'");
ef063748 67 pg_set_client_encoding("UNICODE");
70dcff6b
AD
68 } else {
69 db_query($link, "SET NAMES utf8");
70 db_query($link, "SET CHARACTER SET utf8");
de696427
AD
71 }
72
3b3d116e
AD
73 $last_purge = 0;
74
1f4ad53c 75 while (true) {
a545b564 76
bf7fcde8
AD
77 if (!make_stampfile('update_daemon.stamp')) {
78 print "error: unable to create stampfile";
79 die;
80 }
81
3b3d116e 82 if (time() - $last_purge > PURGE_INTERVAL) {
6f9e33e4 83 _debug("Purging old posts (random 30 feeds)...");
894ebcf5 84 global_purge_old_posts($link, true, 30);
3b3d116e
AD
85 $last_purge = time();
86 }
1f4ad53c 87
085a5a74 88 // FIXME: get all scheduled updates w/forced refetch
6eafcac6 89 // Stub, until I figure out if it is really needed.
1f4ad53c 90
085a5a74
AD
91# $result = db_query($link, "SELECT * FROM ttrss_scheduled_updates ORDER BY id");
92# while ($line = db_fetch_assoc($result)) {
93# print "Scheduled feed update: " . $line["feed_id"] . ", UID: " .
94# $line["owner_uid"] . "\n";
95# }
1f4ad53c
AD
96
97 // Process all other feeds using last_updated and interval parameters
98
894ebcf5
AD
99 $random_qpart = sql_random_function();
100
eed55fd3
AD
101/*
102 ttrss_entries.date_entered < NOW() - INTERVAL '$purge_interval days'");
103 }
104
105 $rows = pg_affected_rows($result);
106
107 } else {
108
109 $result = db_query($link, "DELETE FROM ttrss_user_entries
110 USING ttrss_user_entries, ttrss_entries
111 WHERE ttrss_entries.id = ref_id AND
112 marked = false AND
113 feed_id = '$feed_id' AND
114 ttrss_entries.date_entered < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)"); */
115
116 if (DAEMON_UPDATE_LOGIN_LIMIT > 0) {
117 if (DB_TYPE == "pgsql") {
118 $login_thresh_qpart = "AND ttrss_users.last_login >= NOW() - INTERVAL '".DAEMON_UPDATE_LOGIN_LIMIT." days'";
119 } else {
120 $login_thresh_qpart = "AND ttrss_users.last_login >= DATE_SUB(NOW(), INTERVAL ".DAEMON_UPDATE_LOGIN_LIMIT." DAY)";
121 }
122 } else {
123 $login_thresh_qpart = "";
124 }
125
126 $result = db_query($link, "SELECT feed_url,ttrss_feeds.id,owner_uid,
127 SUBSTRING(last_updated,1,19) AS last_updated,
128 update_interval
129 FROM
130 ttrss_feeds,ttrss_users
131 WHERE
132 ttrss_users.id = owner_uid $login_thresh_qpart
133 ORDER BY $random_qpart DESC");
44e241cb
AD
134
135 $user_prefs_cache = array();
136
6f9e33e4 137 _debug(sprintf("Scheduled %d feeds to update...\n", db_num_rows($result)));
eed55fd3 138
1f4ad53c
AD
139 while ($line = db_fetch_assoc($result)) {
140
1f4ad53c 141 $upd_intl = $line["update_interval"];
1f4ad53c
AD
142 $user_id = $line["owner_uid"];
143
144 if (!$upd_intl || $upd_intl == 0) {
44e241cb
AD
145 if (!$user_prefs_cache[$user_id]['DEFAULT_UPDATE_INTERVAL']) {
146 $upd_intl = get_pref($link, 'DEFAULT_UPDATE_INTERVAL', $user_id);
147 $user_prefs_cache[$user_id]['DEFAULT_UPDATE_INTERVAL'] = $upd_intl;
148 } else {
149 $upd_intl = $user_prefs_cache[$user_id]['DEFAULT_UPDATE_INTERVAL'];
150 }
1f4ad53c 151 }
c1e202b7
AD
152
153 if ($upd_intl < 0) {
1011b66a 154# print "Updates disabled.\n";
c1e202b7
AD
155 continue;
156 }
1f4ad53c 157
6f9e33e4 158 _debug("Feed: " . $line["feed_url"]);
1011b66a 159
6f9e33e4
AD
160// _debug(sprintf("\tLU: %d, INTL: %d, UID: %d) ",
161// time() - strtotime($line["last_updated"]), $upd_intl*60, $user_id));
1f4ad53c
AD
162
163 if (!$line["last_updated"] ||
164 time() - strtotime($line["last_updated"]) > ($upd_intl * 60)) {
6f9e33e4 165
072bd2cc 166 _debug("Updating...");
6f9e33e4 167
8e9c121b 168 pcntl_alarm(300);
4052b32d 169
6eafcac6 170 update_rss_feed($link, $line["feed_url"], $line["id"], true);
4052b32d
AD
171
172 pcntl_alarm(0);
173
44e241cb 174 sleep(1); // prevent flood (FIXME make this an option?)
6eafcac6 175 } else {
072bd2cc 176 _debug("Update not needed.");
1f4ad53c 177 }
de696427
AD
178 }
179
99018440 180 if (DAEMON_SENDS_DIGESTS) send_headlines_digests($link);
9cd7c995 181
6f9e33e4 182 _debug("Sleeping for " . DAEMON_SLEEP_INTERVAL . " seconds...");
1f4ad53c 183
78ea1de0 184 sleep(DAEMON_SLEEP_INTERVAL);
de696427
AD
185 }
186
de696427
AD
187 db_close($link);
188
189?>