]> git.wh0rd.org Git - tt-rss.git/blob - update_daemon2.php
daemon2: unbreak spawn interval
[tt-rss.git] / update_daemon2.php
1 #!/usr/bin/php
2 <?php
3         // This is an experimental multiprocess update daemon.
4         // Some configurable variable may be found below.
5
6         // define('DEFAULT_ERROR_LEVEL', E_ALL);
7         define('DEFAULT_ERROR_LEVEL', E_ERROR | E_WARNING | E_PARSE);
8
9         declare(ticks = 1);
10
11         define('DISABLE_SESSIONS', true);
12
13         require_once "version.php";
14
15         if (strpos(VERSION, ".99") !== false || getenv('DAEMON_XDEBUG')) {
16                 define('DAEMON_EXTENDED_DEBUG', true);
17         }
18
19         define('PURGE_INTERVAL', 3600); // seconds
20         define('MAX_CHILD_RUNTIME', 600); // seconds
21
22         require_once "sanity_check.php";
23         require_once "config.php";
24
25         define('MAX_JOBS', 2);
26         define('SPAWN_INTERVAL', DAEMON_SLEEP_INTERVAL);
27
28         if (!function_exists('pcntl_fork')) {
29                 die("error: This script requires PHP compiled with PCNTL module.\n");
30         }
31
32         if (!ENABLE_UPDATE_DAEMON) {
33                 die("error: Please enable option ENABLE_UPDATE_DAEMON in config.php\n");
34         }
35         
36         require_once "db.php";
37         require_once "db-prefs.php";
38         require_once "functions.php";
39         require_once "lib/magpierss/rss_fetch.inc";
40
41         error_reporting(DEFAULT_ERROR_LEVEL);
42
43         $children = array();
44         $ctimes = array();
45
46         $last_checkpoint = -1;
47
48         function reap_children() {
49                 global $children;
50
51                 $tmp = array();
52
53                 foreach ($children as $pid) {
54                         if (pcntl_waitpid($pid, $status, WNOHANG) != $pid) {
55                                 array_push($tmp, $pid);
56                         } else {
57                                 _debug("[SIGCHLD] child $pid reaped.");
58                                 unset($ctimes[$pid]);
59                         }
60                 }
61
62                 $children = $tmp;
63
64                 return count($tmp);
65         }
66
67         function check_ctimes() {
68                 global $ctimes;
69                 
70                 foreach (array_keys($ctimes) as $pid) {
71                         $started = $ctimes[$pid];
72
73                         if (time() - $started > MAX_CHILD_RUNTIME) {
74                                 _debug("[MASTER] child process $pid seems to be stuck, aborting...");
75                                 posix_kill($pid, SIGINT);
76                         }
77                 }
78         }
79
80         function sigchld_handler($signal) {
81                 $running_jobs = reap_children();
82
83                 _debug("[SIGCHLD] jobs left: $running_jobs");
84
85                 pcntl_waitpid(-1, $status, WNOHANG);
86         }
87
88         function sigint_handler() {
89                 unlink(LOCK_DIRECTORY . "/update_daemon.lock");
90                 die("[SIGINT] removing lockfile and exiting.\n");
91         }
92
93         pcntl_signal(SIGCHLD, 'sigchld_handler');
94
95         if (file_is_locked("update_daemon.lock")) {
96                 die("error: Can't create lockfile. ".
97                         "Maybe another daemon is already running.\n");
98         }
99
100         if (!pcntl_fork()) {
101                 pcntl_signal(SIGINT, 'sigint_handler');
102
103                 // Try to lock a file in order to avoid concurrent update.
104                 $lock_handle = make_lockfile("update_daemon.lock");
105
106                 if (!$lock_handle) {
107                         die("error: Can't create lockfile. ".
108                                 "Maybe another daemon is already running.\n");
109                 }
110
111                 while (true) { sleep(100); }
112         }
113
114         // Testing database connection.
115         // It is unnecessary to start the fork loop if database is not ok.
116         $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME); 
117
118         if (!$link) {
119                 if (DB_TYPE == "mysql") {
120                         print mysql_error();
121                 }
122                 // PG seems to display its own errors just fine by default.             
123                 return;
124         }
125
126         db_close($link);
127
128         while (true) {
129
130                 // Since sleep is interupted by SIGCHLD, we need another way to
131                 // respect the SPAWN_INTERVAL
132                 $next_spawn = $last_checkpoint + SPAWN_INTERVAL - time();
133
134                 if ($next_spawn % 10 == 0) {
135                         $running_jobs = count($children);
136                         _debug("[MASTER] active jobs: $running_jobs, next spawn at $next_spawn sec.");
137                 }
138
139                 if ($last_checkpoint + SPAWN_INTERVAL < time()) {
140
141                         check_ctimes();
142                         reap_children();
143
144                         for ($j = count($children); $j < MAX_JOBS; $j++) {
145                                 $pid = pcntl_fork();
146                                 if ($pid == -1) {
147                                         die("fork failed!\n");
148                                 } else if ($pid) {
149                                         _debug("[MASTER] spawned client $j [PID:$pid]...");
150                                         array_push($children, $pid);
151                                         $ctimes[$pid] = time();
152                                 } else {
153                                         pcntl_signal(SIGCHLD, SIG_IGN);
154                                         pcntl_signal(SIGINT, SIG_DFL);
155
156                                         // ****** Updating RSS code *******
157                                         // Only run in fork process.
158
159                                         $start_timestamp = time();
160
161                                         $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME); 
162
163                                         if (!$link) {
164                                                 if (DB_TYPE == "mysql") {
165                                                         print mysql_error();
166                                                 }
167                                                 // PG seems to display its own errors just fine by default.             
168                                                 return;
169                                         }
170
171                                         init_connection($link);
172
173                                         // We disable stamp file, since it is of no use in a multiprocess update.
174                                         // not really, tho for the time being -fox
175                                         if (!make_stampfile('update_daemon.stamp')) {
176                                                 print "warning: unable to create stampfile";
177                                         }       
178
179                                         // FIXME : $last_purge is of no use in a multiprocess update.
180                                         // FIXME : We ALWAYS purge old posts.
181                                         //_debug("Purging old posts (random 30 feeds)...");
182                                         //global_purge_old_posts($link, true, 30);
183
184                                         // Call to the feed batch update function 
185                                         // or regenerate feedbrowser cache
186
187                                         if (rand(0,100) > 50) {
188                                                 update_daemon_common($link);
189                                         } else {
190                                                 $count = update_feedbrowser_cache($link);
191                                                 _debug("Finished, $count feeds processed.");
192                                         }
193
194                                         _debug("Elapsed time: " . (time() - $start_timestamp) . " second(s)");
195  
196                                         db_close($link);
197
198                                         // We are in a fork.
199                                         // We wait a little before exiting to avoid to be faster than our parent process.
200                                         sleep(1);
201                                         // We exit in order to avoid fork bombing.
202                                         exit(0);
203                                 }
204
205                                 // We wait a little time before the next fork, in order to let the first fork
206                                 // mark the feeds it update :
207                                 sleep(1);
208                         }
209                         $last_checkpoint = time();
210                 }
211                 sleep(1);
212         }
213
214 ?>