]> git.wh0rd.org - tt-rss.git/blob - update_daemon2.php
daemon2: check proper lockfile location in reap_children()
[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 global $ctimes;
51
52 $tmp = array();
53
54 foreach ($children as $pid) {
55 if (pcntl_waitpid($pid, $status, WNOHANG) != $pid) {
56
57 if (file_is_locked("update_daemon-$pid.lock")) {
58 array_push($tmp, $pid);
59 } else {
60 _debug("[reap_children] child $pid seems active but lockfile is unlocked.");
61 }
62 } else {
63 _debug("[reap_children] child $pid reaped.");
64 unset($ctimes[$pid]);
65 }
66 }
67
68 $children = $tmp;
69
70 return count($tmp);
71 }
72
73 function check_ctimes() {
74 global $ctimes;
75
76 foreach (array_keys($ctimes) as $pid) {
77 $started = $ctimes[$pid];
78
79 if (time() - $started > MAX_CHILD_RUNTIME) {
80 _debug("[MASTER] child process $pid seems to be stuck, aborting...");
81 posix_kill($pid, SIGKILL);
82 }
83 }
84 }
85
86 function sigchld_handler($signal) {
87 $running_jobs = reap_children();
88
89 _debug("[SIGCHLD] jobs left: $running_jobs");
90
91 pcntl_waitpid(-1, $status, WNOHANG);
92 }
93
94 function shutdown() {
95 if (file_exists(LOCK_DIRECTORY . "/update_daemon.lock"))
96 unlink(LOCK_DIRECTORY . "/update_daemon.lock");
97 }
98
99 function task_shutdown() {
100 $pid = posix_getpid();
101
102 if (file_exists(LOCK_DIRECTORY . "/update_daemon-$pid.lock"))
103 unlink(LOCK_DIRECTORY . "/update_daemon-$pid.lock");
104 }
105
106 function sigint_handler() {
107 shutdown();
108 die("[SIGINT] removing lockfile and exiting.\n");
109 }
110
111 function task_sigint_handler() {
112 task_shutdown();
113 die("[SIGINT] removing lockfile and exiting.\n");
114 }
115
116 pcntl_signal(SIGCHLD, 'sigchld_handler');
117
118 if (file_is_locked("update_daemon.lock")) {
119 die("error: Can't create lockfile. ".
120 "Maybe another daemon is already running.\n");
121 }
122
123 if (!pcntl_fork()) {
124 pcntl_signal(SIGINT, 'sigint_handler');
125 register_shutdown_function('shutdown');
126
127 // Try to lock a file in order to avoid concurrent update.
128 $lock_handle = make_lockfile("update_daemon.lock");
129
130 if (!$lock_handle) {
131 die("error: Can't create lockfile. ".
132 "Maybe another daemon is already running.\n");
133 }
134
135 while (true) { sleep(100); }
136 }
137
138 // Testing database connection.
139 // It is unnecessary to start the fork loop if database is not ok.
140 $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
141
142 if (!$link) {
143 if (DB_TYPE == "mysql") {
144 print mysql_error();
145 }
146 // PG seems to display its own errors just fine by default.
147 return;
148 }
149
150 db_close($link);
151
152 while (true) {
153
154 // Since sleep is interupted by SIGCHLD, we need another way to
155 // respect the SPAWN_INTERVAL
156 $next_spawn = $last_checkpoint + SPAWN_INTERVAL - time();
157
158 if ($next_spawn % 10 == 0) {
159 $running_jobs = count($children);
160 _debug("[MASTER] active jobs: $running_jobs, next spawn at $next_spawn sec.");
161 }
162
163 if ($last_checkpoint + SPAWN_INTERVAL < time()) {
164
165 check_ctimes();
166 reap_children();
167
168 for ($j = count($children); $j < MAX_JOBS; $j++) {
169 $pid = pcntl_fork();
170 if ($pid == -1) {
171 die("fork failed!\n");
172 } else if ($pid) {
173 _debug("[MASTER] spawned client $j [PID:$pid]...");
174 array_push($children, $pid);
175 $ctimes[$pid] = time();
176 } else {
177 pcntl_signal(SIGCHLD, SIG_IGN);
178 pcntl_signal(SIGINT, 'task_sigint_handler');
179
180 register_shutdown_function('task_shutdown');
181
182 $my_pid = posix_getpid();
183 $lock_filename = "update_daemon-$my_pid.lock";
184
185 $lock_handle = make_lockfile($lock_filename);
186
187 if (!$lock_handle) {
188 die("error: Can't create lockfile ($lock_filename). ".
189 "Maybe another daemon is already running.\n");
190 }
191
192 // ****** Updating RSS code *******
193 // Only run in fork process.
194
195 $start_timestamp = time();
196
197 $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
198
199 if (!$link) {
200 if (DB_TYPE == "mysql") {
201 print mysql_error();
202 }
203 // PG seems to display its own errors just fine by default.
204 return;
205 }
206
207 init_connection($link);
208
209 // We disable stamp file, since it is of no use in a multiprocess update.
210 // not really, tho for the time being -fox
211 if (!make_stampfile('update_daemon.stamp')) {
212 print "warning: unable to create stampfile";
213 }
214
215 // Call to the feed batch update function
216 // or regenerate feedbrowser cache
217
218 if (rand(0,100) > 30) {
219 update_daemon_common($link);
220 } else {
221 $count = update_feedbrowser_cache($link);
222 _debug("Finished, $count feeds processed.");
223 }
224
225 _debug("Elapsed time: " . (time() - $start_timestamp) . " second(s)");
226
227 db_close($link);
228
229 // We are in a fork.
230 // We wait a little before exiting to avoid to be faster than our parent process.
231 sleep(1);
232
233 unlink(LOCK_DIRECTORY . "/$lock_filename");
234
235 // We exit in order to avoid fork bombing.
236 exit(0);
237 }
238
239 // We wait a little time before the next fork, in order to let the first fork
240 // mark the feeds it update :
241 sleep(1);
242 }
243 $last_checkpoint = time();
244 }
245 sleep(1);
246 }
247
248 ?>