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