X-Git-Url: https://git.wh0rd.org/?a=blobdiff_plain;f=update_daemon2.php;h=d7141c87815a0d8a784b0e099502363dbb7fcdcd;hb=7aabaa09e13482291194c3c8d86a0e8421ea009c;hp=d113fc9562f19eb7c0c420dc46f9ac51ed39a845;hpb=0708291db408c0fea03e304c203179ebdff9b7f7;p=tt-rss.git diff --git a/update_daemon2.php b/update_daemon2.php index d113fc95..d7141c87 100755 --- a/update_daemon2.php +++ b/update_daemon2.php @@ -47,14 +47,20 @@ function reap_children() { global $children; + global $ctimes; $tmp = array(); foreach ($children as $pid) { if (pcntl_waitpid($pid, $status, WNOHANG) != $pid) { - array_push($tmp, $pid); + + if (file_is_locked("update_daemon-$pid.lock")) { + array_push($tmp, $pid); + } else { + _debug("[reap_children] child $pid seems active but lockfile is unlocked."); + } } else { - _debug("[SIGCHLD] child $pid reaped."); + _debug("[reap_children] child $pid reaped."); unset($ctimes[$pid]); } } @@ -72,7 +78,7 @@ if (time() - $started > MAX_CHILD_RUNTIME) { _debug("[MASTER] child process $pid seems to be stuck, aborting..."); - posix_kill($pid, SIGINT); + posix_kill($pid, SIGKILL); } } } @@ -85,10 +91,27 @@ pcntl_waitpid(-1, $status, WNOHANG); } + function shutdown() { + if (file_exists(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() { - unlink(LOCK_DIRECTORY . "/update_daemon.lock"); + shutdown(); die("[SIGINT] removing lockfile and exiting.\n"); - } + } + + function task_sigint_handler() { + task_shutdown(); + die("[SIGINT] removing lockfile and exiting.\n"); + } pcntl_signal(SIGCHLD, 'sigchld_handler'); @@ -99,6 +122,7 @@ if (!pcntl_fork()) { pcntl_signal(SIGINT, 'sigint_handler'); + register_shutdown_function('shutdown'); // Try to lock a file in order to avoid concurrent update. $lock_handle = make_lockfile("update_daemon.lock"); @@ -151,7 +175,19 @@ $ctimes[$pid] = time(); } else { 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 ******* // Only run in fork process. @@ -176,15 +212,10 @@ print "warning: unable to create stampfile"; } - // FIXME : $last_purge is of no use in a multiprocess update. - // FIXME : We ALWAYS purge old posts. - //_debug("Purging old posts (random 30 feeds)..."); - //global_purge_old_posts($link, true, 30); - // Call to the feed batch update function // or regenerate feedbrowser cache - if (rand(0,100) > 50) { + if (rand(0,100) > 30) { update_daemon_common($link); } else { $count = update_feedbrowser_cache($link); @@ -198,6 +229,9 @@ // We are in a fork. // We wait a little before exiting to avoid to be faster than our parent process. sleep(1); + + unlink(LOCK_DIRECTORY . "/$lock_filename"); + // We exit in order to avoid fork bombing. exit(0); }