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