]> git.wh0rd.org - tt-rss.git/blame - update_daemon.php
cleanup notify box on successful refetch
[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
AD
7
8 define('MAGPIE_CACHE_DIR', '/var/tmp/magpie-ttrss-cache-daemon');
de696427
AD
9 define('DISABLE_SESSIONS', true);
10
11 require_once "sanity_check.php";
12 require_once "config.php";
c6784aea
AD
13
14 if (!ENABLE_UPDATE_DAEMON) {
15 die("Please enable option ENABLE_UPDATE_DAEMON in config.php");
16 }
17
de696427
AD
18 require_once "db.php";
19 require_once "db-prefs.php";
20 require_once "functions.php";
21 require_once "magpierss/rss_fetch.inc";
22
6eafcac6
AD
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
de696427
AD
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
1f4ad53c 51 while (true) {
a545b564
AD
52
53 print "Purging old posts...\n";
54
55 global_purge_old_posts($link, false);
1f4ad53c 56
085a5a74 57 // FIXME: get all scheduled updates w/forced refetch
6eafcac6 58 // Stub, until I figure out if it is really needed.
1f4ad53c 59
085a5a74
AD
60# $result = db_query($link, "SELECT * FROM ttrss_scheduled_updates ORDER BY id");
61# while ($line = db_fetch_assoc($result)) {
62# print "Scheduled feed update: " . $line["feed_id"] . ", UID: " .
63# $line["owner_uid"] . "\n";
64# }
1f4ad53c
AD
65
66 // Process all other feeds using last_updated and interval parameters
67
68 $result = db_query($link, "SELECT feed_url,id,owner_uid,
69 SUBSTRING(last_updated,1,19) AS last_updated,
70 update_interval FROM ttrss_feeds ORDER BY last_updated DESC");
71
72 while ($line = db_fetch_assoc($result)) {
73
1f4ad53c 74 $upd_intl = $line["update_interval"];
1f4ad53c
AD
75 $user_id = $line["owner_uid"];
76
77 if (!$upd_intl || $upd_intl == 0) {
78 $upd_intl = get_pref($link, 'DEFAULT_UPDATE_INTERVAL', $user_id);
79 }
c1e202b7
AD
80
81 if ($upd_intl < 0) {
1011b66a 82# print "Updates disabled.\n";
c1e202b7
AD
83 continue;
84 }
1f4ad53c 85
1011b66a
AD
86 print "Feed: " . $line["feed_url"] . ": ";
87
78ea1de0
AD
88 printf("(%d/%d, %d) ", time() - strtotime($line["last_updated"]),
89 $upd_intl*60, $owner_uid);
1f4ad53c
AD
90
91 if (!$line["last_updated"] ||
92 time() - strtotime($line["last_updated"]) > ($upd_intl * 60)) {
93
6eafcac6
AD
94 print "Updating...\n";
95 update_rss_feed($link, $line["feed_url"], $line["id"], true);
13def219 96 sleep(3); // prevent flood (FIXME make this an option?)
6eafcac6
AD
97 } else {
98 print "Update not needed.\n";
1f4ad53c 99 }
de696427
AD
100 }
101
78ea1de0 102 print "Sleeping for " . DAEMON_SLEEP_INTERVAL . " seconds...\n";
1f4ad53c 103
78ea1de0 104 sleep(DAEMON_SLEEP_INTERVAL);
de696427
AD
105 }
106
de696427
AD
107 db_close($link);
108
109?>