瀏覽代碼

forced stop master process

all worker process maybe has exit, but master process still can not stop, check worker running status and stop master process
malacca 7 年之前
父節點
當前提交
7c2e10ed8d
共有 1 個文件被更改,包括 22 次插入2 次删除
  1. 22 2
      Worker.php

+ 22 - 2
Worker.php

@@ -1421,6 +1421,7 @@ class Worker
      */
     protected static function monitorWorkersForLinux()
     {
+        $checkTimes = 0;
         static::$_status = static::STATUS_RUNNING;
         while (1) {
             // Calls signal handlers for pending signals.
@@ -1432,6 +1433,8 @@ class Worker
             pcntl_signal_dispatch();
             // If a child has already exited.
             if ($pid > 0) {
+                // reset $checkTimes
+                $checkTimes = 0;
                 // Find out witch worker process exited.
                 foreach (static::$_pidMap as $worker_id => $worker_pid_array) {
                     if (isset($worker_pid_array[$pid])) {
@@ -1472,9 +1475,26 @@ class Worker
                     }
                 }
             } else {
+
                 // If shutdown state and all child processes exited then master process exit.
-                if (static::$_status === static::STATUS_SHUTDOWN && !static::getAllWorkerPids()) {
-                    static::exitAndClearAll();
+                if (static::$_status === static::STATUS_SHUTDOWN) {
+                    $pids = static::getAllWorkerPids();
+                    if (!$pids) {
+                        static::exitAndClearAll();
+                    } elseif ($checkTimes > 500) {
+                        // forced stop if all pid exited
+                        $allExited = true;
+                        foreach ($pids as $pid) {
+                            if (posix_kill($pid, 0)) {
+                                $allExited = false;
+                                break;
+                            }
+                        }
+                        if ($allExited) {
+                            static::exitAndClearAll();
+                        }
+                    }
+                    $checkTimes++;
                 }
             }
         }