]> git.wh0rd.org - tt-rss.git/blob - update_daemon.php
update_daemon work, remove unneeded indexes, query optimizations
[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 define('PURGE_INTERVAL', 3600); // seconds
12
13 require_once "sanity_check.php";
14 require_once "config.php";
15
16 if (!ENABLE_UPDATE_DAEMON) {
17 die("Please enable option ENABLE_UPDATE_DAEMON in config.php");
18 }
19
20 require_once "db.php";
21 require_once "db-prefs.php";
22 require_once "functions.php";
23 require_once "magpierss/rss_fetch.inc";
24
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
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
53 $last_purge = 0;
54
55 while (true) {
56
57 if (time() - $last_purge > PURGE_INTERVAL) {
58 print "Purging old posts (random 30 feeds)...\n";
59 global_purge_old_posts($link, true, 30);
60 $last_purge = time();
61 }
62
63 // FIXME: get all scheduled updates w/forced refetch
64 // Stub, until I figure out if it is really needed.
65
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 # }
71
72 // Process all other feeds using last_updated and interval parameters
73
74 $random_qpart = sql_random_function();
75
76 $result = db_query($link, "SELECT feed_url,id,owner_uid,
77 SUBSTRING(last_updated,1,19) AS last_updated,
78 update_interval FROM ttrss_feeds ORDER BY $random_qpart DESC");
79
80 $user_prefs_cache = array();
81
82 while ($line = db_fetch_assoc($result)) {
83
84 $upd_intl = $line["update_interval"];
85 $user_id = $line["owner_uid"];
86
87 if (!$upd_intl || $upd_intl == 0) {
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 }
94 }
95
96 if ($upd_intl < 0) {
97 # print "Updates disabled.\n";
98 continue;
99 }
100
101 print "Feed: " . $line["feed_url"] . ": ";
102
103 printf("(%d/%d, %d) ", time() - strtotime($line["last_updated"]),
104 $upd_intl*60, $owner_uid);
105
106 if (!$line["last_updated"] ||
107 time() - strtotime($line["last_updated"]) > ($upd_intl * 60)) {
108
109 print "Updating...\n";
110 update_rss_feed($link, $line["feed_url"], $line["id"], true);
111 sleep(1); // prevent flood (FIXME make this an option?)
112 } else {
113 print "Update not needed.\n";
114 }
115 }
116
117 print "Sleeping for " . DAEMON_SLEEP_INTERVAL . " seconds...\n";
118
119 sleep(DAEMON_SLEEP_INTERVAL);
120 }
121
122 db_close($link);
123
124 ?>