]> git.wh0rd.org - tt-rss.git/blame - update_daemon.php
add missing DISABLE_SESSIONS checks
[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 57 if (time() - $last_purge > PURGE_INTERVAL) {
894ebcf5
AD
58 print "Purging old posts (random 30 feeds)...\n";
59 global_purge_old_posts($link, true, 30);
3b3d116e
AD
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
894ebcf5
AD
74 $random_qpart = sql_random_function();
75
1f4ad53c
AD
76 $result = db_query($link, "SELECT feed_url,id,owner_uid,
77 SUBSTRING(last_updated,1,19) AS last_updated,
894ebcf5 78 update_interval FROM ttrss_feeds ORDER BY $random_qpart DESC");
44e241cb
AD
79
80 $user_prefs_cache = array();
81
1f4ad53c
AD
82 while ($line = db_fetch_assoc($result)) {
83
1f4ad53c 84 $upd_intl = $line["update_interval"];
1f4ad53c
AD
85 $user_id = $line["owner_uid"];
86
87 if (!$upd_intl || $upd_intl == 0) {
44e241cb
AD
88 if (!$user_prefs_cache[$user_id]['DEFAULT_UPDATE_INTERVAL']) {
89 $upd_intl = get_pref($link, 'DEFAULT_UPDATE_INTERVAL', $user_id);
90 $user_prefs_cache[$user_id]['DEFAULT_UPDATE_INTERVAL'] = $upd_intl;
91 } else {
92 $upd_intl = $user_prefs_cache[$user_id]['DEFAULT_UPDATE_INTERVAL'];
93 }
1f4ad53c 94 }
c1e202b7
AD
95
96 if ($upd_intl < 0) {
1011b66a 97# print "Updates disabled.\n";
c1e202b7
AD
98 continue;
99 }
1f4ad53c 100
1011b66a
AD
101 print "Feed: " . $line["feed_url"] . ": ";
102
78ea1de0
AD
103 printf("(%d/%d, %d) ", time() - strtotime($line["last_updated"]),
104 $upd_intl*60, $owner_uid);
1f4ad53c
AD
105
106 if (!$line["last_updated"] ||
107 time() - strtotime($line["last_updated"]) > ($upd_intl * 60)) {
108
6eafcac6
AD
109 print "Updating...\n";
110 update_rss_feed($link, $line["feed_url"], $line["id"], true);
44e241cb 111 sleep(1); // prevent flood (FIXME make this an option?)
6eafcac6
AD
112 } else {
113 print "Update not needed.\n";
1f4ad53c 114 }
de696427
AD
115 }
116
78ea1de0 117 print "Sleeping for " . DAEMON_SLEEP_INTERVAL . " seconds...\n";
1f4ad53c 118
78ea1de0 119 sleep(DAEMON_SLEEP_INTERVAL);
de696427
AD
120 }
121
de696427
AD
122 db_close($link);
123
124?>