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