]> git.wh0rd.org - tt-rss.git/blobdiff - update_daemon2.php
Merge branch 'master' of git.tt-rss.org:fox/tt-rss
[tt-rss.git] / update_daemon2.php
index 5aea5a6eea9c7b31d1a2101f636110ffdd82fc85..af29ac18f86e0987f23cc365324cdaadadd63010 100755 (executable)
@@ -9,10 +9,9 @@
        define('DISABLE_SESSIONS', true);
 
        require_once "version.php";
-       require_once "config.php";
        require_once "autoload.php";
        require_once "functions.php";
-       require_once "rssfuncs.php";
+       require_once "config.php";
 
        // defaults
        define_default('PURGE_INTERVAL', 3600); // seconds
        require_once "db.php";
        require_once "db-prefs.php";
 
-
        if (!function_exists('pcntl_fork')) {
                die("error: This script requires PHP compiled with PCNTL module.\n");
        }
 
+       $options = getopt("");
+
+       if (!is_array($options)) {
+               die("error: getopt() failed. ".
+                       "Most probably you are using PHP CGI to run this script ".
+                       "instead of required PHP CLI. Check tt-rss wiki page on updating feeds for ".
+                       "additional information.\n");
+       }
+
+
+       $master_handlers_installed = false;
+
        $children = array();
        $ctimes = array();
 
        $last_checkpoint = -1;
 
+       /**
+        * @SuppressWarnings(unused)
+        */
        function reap_children() {
                global $children;
                global $ctimes;
@@ -74,6 +87,9 @@
                }
        }
 
+       /**
+       * @SuppressWarnings(unused)
+       */
        function sigchld_handler($signal) {
                $running_jobs = reap_children();
 
                pcntl_waitpid(-1, $status, WNOHANG);
        }
 
+       function shutdown($caller_pid) {
+               if ($caller_pid == posix_getpid()) {
+                       if (file_exists(LOCK_DIRECTORY . "/update_daemon.lock")) {
+                               _debug("removing lockfile (master)...");
+                               unlink(LOCK_DIRECTORY . "/update_daemon.lock");
+                       }
+               }
+       }
+
+       function task_shutdown() {
+               $pid = posix_getpid();
+
+               if (file_exists(LOCK_DIRECTORY . "/update_daemon-$pid.lock")) {
+                       _debug("removing lockfile ($pid)...");
+                       unlink(LOCK_DIRECTORY . "/update_daemon-$pid.lock");
+               }
+       }
+
+       function sigint_handler() {
+               _debug("[MASTER] SIG_INT received.\n");
+               shutdown(posix_getpid());
+               die;
+       }
+
+       function task_sigint_handler() {
+               _debug("[TASK] SIG_INT received.\n");
+               task_shutdown();
+               die;
+       }
+
        pcntl_signal(SIGCHLD, 'sigchld_handler');
 
        $longopts = array("log:",
                $spawn_interval = SPAWN_INTERVAL;
        }
 
+       // let's enforce a minimum spawn interval as to not forkbomb the host
+       $spawn_interval = max(60, $spawn_interval);
+       _debug("Spawn interval: $spawn_interval sec");
+
        if (isset($options["log"])) {
                _debug("Logging to " . $options["log"]);
                define('LOGFILE', $options["log"]);
                                if ($pid == -1) {
                                        die("fork failed!\n");
                                } else if ($pid) {
+
+                                       if (!$master_handlers_installed) {
+                                               _debug("[MASTER] installing shutdown handlers");
+                                               pcntl_signal(SIGINT, 'sigint_handler');
+                                               pcntl_signal(SIGTERM, 'sigint_handler');
+                                               register_shutdown_function('shutdown', posix_getpid());
+                                               $master_handlers_installed = true;
+                                       }
+
                                        _debug("[MASTER] spawned client $j [PID:$pid]...");
                                        array_push($children, $pid);
                                        $ctimes[$pid] = time();
                                } else {
                                        pcntl_signal(SIGCHLD, SIG_IGN);
+                                       pcntl_signal(SIGINT, 'task_sigint_handler');
+
+                                       register_shutdown_function('task_shutdown');
 
                                        $quiet = (isset($options["quiet"])) ? "--quiet" : "";
+                                       $log = function_exists("flock") && isset($options['log']) ? '--log '.$options['log'] : '';
 
                                        $my_pid = posix_getpid();
 
-                                       passthru(PHP_EXECUTABLE . " update.php --daemon-loop $quiet --task $j --pidlock $my_pid");
+                                       passthru(PHP_EXECUTABLE . " update.php --daemon-loop $quiet $log --task $j --pidlock $my_pid");
 
                                        sleep(1);