]> git.wh0rd.org - tt-rss.git/blobdiff - update_daemon2.php
update_daemon2: fix locking
[tt-rss.git] / update_daemon2.php
index 3721a09793d29b244cb9db810ee58b6a5d9c229d..68989c8e4a130bcfdd582fa70928a6d12c38a153 100644 (file)
@@ -7,7 +7,8 @@
 
        declare(ticks = 1);
 
-       require "config.php";
+       require_once "config.php";
+       require_once "functions.php";
 
        define('MAX_JOBS', 2);
        define('CLIENT_PROCESS', './update_daemon2_client.php SRV_RUN_OK');
                pcntl_waitpid(-1, $status, WNOHANG);
        }
 
+       function sigint_handler() {
+               unlink(LOCK_DIRECTORY . "/update_daemon.lock");
+               die("Received SIGINT. Exiting.\n");
+       }
+
        pcntl_signal(SIGCHLD, 'sigchld_handler');
+       pcntl_signal(SIGINT, 'sigint_handler');
+
+       if (file_is_locked("update_daemon.lock")) {
+               die("error: Can't create lockfile. ".
+                       "Maybe another daemon is already running.\n");
+       }
+
+       if (!pcntl_fork()) {
+               $lock_handle = make_lockfile("update_daemon.lock");
+
+               if (!$lock_handle) {
+                       die("error: Can't create lockfile. ".
+                               "Maybe another daemon is already running.\n");
+               }
+
+               while (true) { sleep(100); }
+       }
 
        while (true) {
 
                $next_spawn = $last_checkpoint + SPAWN_INTERVAL - time();
 
-               print "[MASTER] active jobs: $running_jobs, next spawn at $next_spawn sec\n";
+               if ($next_spawn % 10 == 0) {
+                       print "[MASTER] active jobs: $running_jobs, next spawn at $next_spawn sec\n";
+               }
 
                if ($last_checkpoint + SPAWN_INTERVAL < time()) {
 
@@ -43,6 +68,7 @@
                                        print "OK [$running_jobs]\n";
                                } else {
                                        pcntl_signal(SIGCHLD, SIG_IGN);
+                                       pcntl_signal(SIGINT, SIG_DFL);
                                        passthru(CLIENT_PROCESS);
                                        exit(0);
                                }