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