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