]> git.wh0rd.org - tt-rss.git/blob - update_daemon.php
daemon: rate limit in-between feed updates
[tt-rss.git] / update_daemon.php
1 #!/usr/bin/php4
2 <?
3 // this daemon runs in the background and updates all feeds
4 // continuously
5
6 declare(ticks = 1);
7
8 define('MAGPIE_CACHE_DIR', '/var/tmp/magpie-ttrss-cache-daemon');
9 define('DISABLE_SESSIONS', true);
10
11 require_once "sanity_check.php";
12 require_once "config.php";
13
14 if (!ENABLE_UPDATE_DAEMON) {
15 die("Please enable option ENABLE_UPDATE_DAEMON in config.php");
16 }
17
18 require_once "db.php";
19 require_once "db-prefs.php";
20 require_once "functions.php";
21 require_once "magpierss/rss_fetch.inc";
22
23 function sigint_handler() {
24 unlink("update_daemon.lock");
25 die("Received SIGINT. Exiting.\n");
26 }
27
28 pcntl_signal(SIGINT, sigint_handler);
29
30 $lock_handle = make_lockfile("update_daemon.lock");
31
32 if (!$lock_handle) {
33 die("error: Can't create lockfile ($lock_filename). ".
34 "Maybe another daemon is already running.");
35 }
36
37 $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
38
39 if (!$link) {
40 if (DB_TYPE == "mysql") {
41 print mysql_error();
42 }
43 // PG seems to display its own errors just fine by default.
44 return;
45 }
46
47 if (DB_TYPE == "pgsql") {
48 pg_query("set client_encoding = 'utf-8'");
49 }
50
51 while (true) {
52
53 // FIXME: get all scheduled updates w/forced refetch
54 // Stub, until I figure out if it is really needed.
55
56 # $result = db_query($link, "SELECT * FROM ttrss_scheduled_updates ORDER BY id");
57 # while ($line = db_fetch_assoc($result)) {
58 # print "Scheduled feed update: " . $line["feed_id"] . ", UID: " .
59 # $line["owner_uid"] . "\n";
60 # }
61
62 // Process all other feeds using last_updated and interval parameters
63
64 $result = db_query($link, "SELECT feed_url,id,owner_uid,
65 SUBSTRING(last_updated,1,19) AS last_updated,
66 update_interval FROM ttrss_feeds ORDER BY last_updated DESC");
67
68 while ($line = db_fetch_assoc($result)) {
69
70 $upd_intl = $line["update_interval"];
71 $user_id = $line["owner_uid"];
72
73 if (!$upd_intl || $upd_intl == 0) {
74 $upd_intl = get_pref($link, 'DEFAULT_UPDATE_INTERVAL', $user_id);
75 }
76
77 if ($upd_intl < 0) {
78 # print "Updates disabled.\n";
79 continue;
80 }
81
82 print "Feed: " . $line["feed_url"] . ": ";
83
84 printf("(%d/%d, %d) ", time() - strtotime($line["last_updated"]),
85 $upd_intl*60, $owner_uid);
86
87 if (!$line["last_updated"] ||
88 time() - strtotime($line["last_updated"]) > ($upd_intl * 60)) {
89
90 print "Updating...\n";
91 update_rss_feed($link, $line["feed_url"], $line["id"], true);
92 sleep(3); // prevent flood (FIXME make this an option?)
93 } else {
94 print "Update not needed.\n";
95 }
96 }
97
98 print "Sleeping for " . DAEMON_SLEEP_INTERVAL . " seconds...\n";
99
100 sleep(DAEMON_SLEEP_INTERVAL);
101 }
102
103 db_close($link);
104
105 ?>