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