]> git.wh0rd.org Git - tt-rss.git/blob - update_daemon.php
misc interface cleanups, only allow daemon to start when ENABLE_UPDATE_DAEMON is...
[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('SLEEP_INTERVAL', 60); // seconds between update runs
9         define('MAGPIE_CACHE_DIR', '/var/tmp/magpie-ttrss-cache-daemon');
10         define('DISABLE_SESSIONS', true);
11
12         require_once "sanity_check.php";
13         require_once "config.php";
14
15         if (!ENABLE_UPDATE_DAEMON) {
16                 die("Please enable option ENABLE_UPDATE_DAEMON in config.php");
17         }
18         
19         require_once "db.php";
20         require_once "db-prefs.php";
21         require_once "functions.php";
22         require_once "magpierss/rss_fetch.inc";
23
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
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
52         while (true) {
53
54                 // FIXME: get all scheduled updates w/forced refetch
55                 // Stub, until I figure out if it is really needed.
56
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 #               }
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         
71                         print "Feed: " . $line["feed_url"] . ": ";
72         
73                         $upd_intl = $line["update_interval"];
74         
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                         }
80
81                         if ($upd_intl < 0) { 
82                                 print "Updates disabled.\n";
83                                 continue; 
84                         }
85         
86 #                       printf("%d ? %d\n", time() - strtotime($line["last_updated"]) > $upd_intl*60,
87 #                               $upd_intl*60);
88         
89                         if (!$line["last_updated"] || 
90                                 time() - strtotime($line["last_updated"]) > ($upd_intl * 60)) {
91         
92                                 print "Updating...\n";  
93                                 update_rss_feed($link, $line["feed_url"], $line["id"], true);   
94                         } else {
95                                 print "Update not needed.\n";
96                         }
97                 }
98
99                 print "Sleeping for " . SLEEP_INTERVAL . " seconds...\n";
100                 
101                 sleep(SLEEP_INTERVAL);
102         }
103
104         db_close($link);
105
106 ?>