add some more lockfiles to update_daemon2

master
Andrew Dolgov 14 years ago
parent f9b2d27c70
commit 8ccaff02b5

@ -53,9 +53,13 @@
foreach ($children as $pid) { foreach ($children as $pid) {
if (pcntl_waitpid($pid, $status, WNOHANG) != $pid) { if (pcntl_waitpid($pid, $status, WNOHANG) != $pid) {
if (file_is_locked(LOCK_DIRECTORY . "/update_daemon-$pid.lock")) {
array_push($tmp, $pid); array_push($tmp, $pid);
} else { } else {
_debug("[SIGCHLD] child $pid reaped."); _debug("[reap_children] child $pid seems active but lockfile is unlocked.");
}
} else {
_debug("[reap_children] child $pid reaped.");
unset($ctimes[$pid]); unset($ctimes[$pid]);
} }
} }
@ -86,8 +90,25 @@
pcntl_waitpid(-1, $status, WNOHANG); pcntl_waitpid(-1, $status, WNOHANG);
} }
function sigint_handler() { function shutdown() {
if (file_exists(LOCK_DIRECTORY . "/update_daemon.lock"))
unlink(LOCK_DIRECTORY . "/update_daemon.lock"); unlink(LOCK_DIRECTORY . "/update_daemon.lock");
}
function task_shutdown() {
$pid = posix_getpid();
if (file_exists(LOCK_DIRECTORY . "/update_daemon-$pid.lock"))
unlink(LOCK_DIRECTORY . "/update_daemon-$pid.lock");
}
function sigint_handler() {
shutdown();
die("[SIGINT] removing lockfile and exiting.\n");
}
function task_sigint_handler() {
task_shutdown();
die("[SIGINT] removing lockfile and exiting.\n"); die("[SIGINT] removing lockfile and exiting.\n");
} }
@ -100,6 +121,7 @@
if (!pcntl_fork()) { if (!pcntl_fork()) {
pcntl_signal(SIGINT, 'sigint_handler'); pcntl_signal(SIGINT, 'sigint_handler');
register_shutdown_function('shutdown');
// Try to lock a file in order to avoid concurrent update. // Try to lock a file in order to avoid concurrent update.
$lock_handle = make_lockfile("update_daemon.lock"); $lock_handle = make_lockfile("update_daemon.lock");
@ -152,7 +174,19 @@
$ctimes[$pid] = time(); $ctimes[$pid] = time();
} else { } else {
pcntl_signal(SIGCHLD, SIG_IGN); pcntl_signal(SIGCHLD, SIG_IGN);
pcntl_signal(SIGINT, SIG_DFL); pcntl_signal(SIGINT, 'task_sigint_handler');
register_shutdown_function('task_shutdown');
$my_pid = posix_getpid();
$lock_filename = "update_daemon-$my_pid.lock";
$lock_handle = make_lockfile($lock_filename);
if (!$lock_handle) {
die("error: Can't create lockfile ($lock_filename). ".
"Maybe another daemon is already running.\n");
}
// ****** Updating RSS code ******* // ****** Updating RSS code *******
// Only run in fork process. // Only run in fork process.
@ -180,7 +214,7 @@
// Call to the feed batch update function // Call to the feed batch update function
// or regenerate feedbrowser cache // or regenerate feedbrowser cache
if (rand(0,100) > 50) { if (rand(0,100) > 30) {
update_daemon_common($link); update_daemon_common($link);
} else { } else {
$count = update_feedbrowser_cache($link); $count = update_feedbrowser_cache($link);
@ -194,6 +228,9 @@
// We are in a fork. // We are in a fork.
// We wait a little before exiting to avoid to be faster than our parent process. // We wait a little before exiting to avoid to be faster than our parent process.
sleep(1); sleep(1);
unlink(LOCK_DIRECTORY . "/$lock_filename");
// We exit in order to avoid fork bombing. // We exit in order to avoid fork bombing.
exit(0); exit(0);
} }

Loading…
Cancel
Save