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