]> git.wh0rd.org Git - tt-rss.git/blob - update_daemon2_client.php
add multiprocess update daemon
[tt-rss.git] / update_daemon2_client.php
1 #!/usr/bin/php
2 <?php
3         // define('DEFAULT_ERROR_LEVEL', E_ALL);
4         define('DEFAULT_ERROR_LEVEL', E_ERROR | E_WARNING | E_PARSE);
5
6         if ($argv[1] != "SRV_RUN_OK") {
7                 die("This script should be started by update_daemon2.php.\n");
8         }
9
10         declare(ticks = 1);
11
12         define('MAGPIE_CACHE_DIR', '/var/tmp/magpie-ttrss-cache-daemon');
13         define('SIMPLEPIE_CACHE_DIR',   '/var/tmp/simplepie-ttrss-cache-daemon');
14         define('DISABLE_SESSIONS', true);
15
16         require_once "version.php";
17
18         if (strpos(VERSION, ".99") !== false) {
19                 define('DAEMON_EXTENDED_DEBUG', true);
20         }
21
22         define('PURGE_INTERVAL', 3600); // seconds
23
24         require_once "sanity_check.php";
25         require_once "config.php";
26
27         if (!ENABLE_UPDATE_DAEMON) {
28                 die("Please enable option ENABLE_UPDATE_DAEMON in config.php\n");
29         }
30         
31         require_once "db.php";
32         require_once "db-prefs.php";
33         require_once "functions.php";
34         require_once "magpierss/rss_fetch.inc";
35
36         error_reporting(DEFAULT_ERROR_LEVEL);
37
38         function sigalrm_handler() {
39                 die("received SIGALRM, hang in feed update?\n");
40         }
41
42         pcntl_signal(SIGALRM, sigalrm_handler);
43
44         $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME); 
45
46         if (!$link) {
47                 if (DB_TYPE == "mysql") {
48                         print mysql_error();
49                 }
50                 // PG seems to display its own errors just fine by default.             
51                 return;
52         }
53
54         if (DB_TYPE == "pgsql") {
55                 pg_query("set client_encoding = 'utf-8'");
56                 pg_set_client_encoding("UNICODE");
57         } else {
58                 if (defined('MYSQL_CHARSET') && MYSQL_CHARSET) {
59                         db_query($link, "SET NAMES " . MYSQL_CHARSET);
60 //                      db_query($link, "SET CHARACTER SET " . MYSQL_CHARSET);
61                 }
62         }
63
64         $last_purge = 0;
65
66 //      if (!make_stampfile('update_daemon.stamp')) {
67 //              print "error: unable to create stampfile";
68 //              die;
69 //      }
70
71         if (time() - $last_purge > PURGE_INTERVAL) {
72                 _debug("Purging old posts (random 30 feeds)...");
73                 global_purge_old_posts($link, true, 30);
74                 $last_purge = time();
75         }
76
77         // Process all other feeds using last_updated and interval parameters
78
79         $random_qpart = sql_random_function();
80                 
81         if (DAEMON_UPDATE_LOGIN_LIMIT > 0) {
82                 if (DB_TYPE == "pgsql") {
83                         $login_thresh_qpart = "AND ttrss_users.last_login >= NOW() - INTERVAL '".DAEMON_UPDATE_LOGIN_LIMIT." days'";
84                 } else {
85                         $login_thresh_qpart = "AND ttrss_users.last_login >= DATE_SUB(NOW(), INTERVAL ".DAEMON_UPDATE_LOGIN_LIMIT." DAY)";
86                 }                       
87         } else {
88                 $login_thresh_qpart = "";
89         }
90
91         if (DB_TYPE == "pgsql") {
92                 $update_limit_qpart = "AND ttrss_feeds.last_updated < NOW() - INTERVAL '".(DAEMON_SLEEP_INTERVAL*2)." seconds'";
93         } else {
94                 $update_limit_qpart = "AND ttrss_feeds.last_updated < DATE_SUB(NOW(), INTERVAL ".(DAEMON_SLEEP_INTERVAL*2)." SECOND)";
95         }
96
97         if (DB_TYPE == "pgsql") {
98                         $updstart_thresh_qpart = "AND (ttrss_feeds.last_update_started IS NULL OR ttrss_feeds.last_update_started >= NOW() - INTERVAL '120 seconds')";
99                 } else {
100                         $updstart_thresh_qpart = "AND (ttrss_feeds.last_update_started IS NULL OR ttrss_feeds.last_update_started >= DATE_SUB(NOW(), INTERVAL 120 SECOND))";
101                 }                       
102
103         $result = db_query($link, "SELECT feed_url,ttrss_feeds.id,owner_uid,
104                         SUBSTRING(last_updated,1,19) AS last_updated,
105                         update_interval 
106                 FROM 
107                         ttrss_feeds,ttrss_users 
108                 WHERE 
109                         ttrss_users.id = owner_uid $login_thresh_qpart $update_limit_qpart 
110                         $updstart_thresh_qpart
111                 ORDER BY $random_qpart DESC LIMIT " . DAEMON_FEED_LIMIT);
112
113         $user_prefs_cache = array();
114
115         _debug(sprintf("Scheduled %d feeds to update...\n", db_num_rows($result)));
116         
117         while ($line = db_fetch_assoc($result)) {
118
119                 $upd_intl = $line["update_interval"];
120                 $user_id = $line["owner_uid"];
121
122                 if (!$upd_intl || $upd_intl == 0) {
123                         if (!$user_prefs_cache[$user_id]['DEFAULT_UPDATE_INTERVAL']) {                  
124                                 $upd_intl = get_pref($link, 'DEFAULT_UPDATE_INTERVAL', $user_id);
125                                 $user_prefs_cache[$user_id]['DEFAULT_UPDATE_INTERVAL'] = $upd_intl;
126                         } else {
127                                 $upd_intl = $user_prefs_cache[$user_id]['DEFAULT_UPDATE_INTERVAL'];
128                         }
129                 }
130
131                 if ($upd_intl < 0) { 
132 #                               print "Updates disabled.\n";
133                         continue; 
134                 }
135
136                 _debug("Feed: " . $line["feed_url"] . ", " . $line["last_updated"]);
137
138 //                      _debug(sprintf("\tLU: %d, INTL: %d, UID: %d) ", 
139 //                              time() - strtotime($line["last_updated"]), $upd_intl*60, $user_id));
140
141                 if (!$line["last_updated"] || 
142                         time() - strtotime($line["last_updated"]) > ($upd_intl * 60)) {
143
144                         _debug("Updating...");
145
146                         pcntl_alarm(300);
147
148                         update_rss_feed($link, $line["feed_url"], $line["id"], true);   
149
150                         pcntl_alarm(0);
151
152                         sleep(1); // prevent flood (FIXME make this an option?)
153                 } else {
154                         _debug("Update not needed.");
155                 }
156         }
157
158         if (DAEMON_SENDS_DIGESTS) send_headlines_digests($link);
159
160         db_close($link);
161 ?>