Przeglądaj źródła

Check master is alive

her-cat 4 lat temu
rodzic
commit
5aa54e0c7a
1 zmienionych plików z 26 dodań i 2 usunięć
  1. 26 2
      Worker.php

+ 26 - 2
Worker.php

@@ -909,9 +909,8 @@ class Worker
 
         // Get master process PID.
         $master_pid      = \is_file(static::$pidFile) ? \file_get_contents(static::$pidFile) : 0;
-        $master_is_alive = $master_pid && \posix_kill($master_pid, 0) && \posix_getpid() !== $master_pid;
         // Master is still alive?
-        if ($master_is_alive) {
+        if (static::checkMasterIsAlive($master_pid)) {
             if ($command === 'start') {
                 static::log("Workerman[$start_file] already running");
                 exit;
@@ -2561,4 +2560,29 @@ class Worker
         }
         return true;
     }
+
+    /**
+     * Check master process is alive
+     *
+     * @param $master_pid
+     * @return bool
+     */
+    protected static function checkMasterIsAlive($master_pid)
+    {
+        if (empty($master_pid)) {
+            return false;
+        }
+
+        $master_is_alive = $master_pid && \posix_kill($master_pid, 0) && \posix_getpid() !== $master_pid;
+        if (!$master_is_alive) {
+            return false;
+        }
+
+        // Master process will send SIGUSR2 signal to all child processes.
+        \posix_kill($master_pid, SIGUSR2);
+        // Sleep 1 second.
+        \sleep(1);
+
+        return stripos(static::formatStatusData(), 'PROCESS STATUS') !== false;
+    }
 }