Browse Source

Merge pull request #968 from twomiao/master

Update worker
walkor 2 years ago
parent
commit
1c24fe43f5
2 changed files with 24 additions and 18 deletions
  1. 2 10
      src/Events/Swoole.php
  2. 22 8
      src/Worker.php

+ 2 - 10
src/Events/Swoole.php

@@ -46,16 +46,6 @@ class Swoole implements EventInterface
     protected $errorHandler = null;
 
     /**
-     * Construct
-     */
-    public function __construct()
-    {
-        // Avoid process exit due to no listening
-        Timer::tick(100000000, function () {
-        });
-    }
-
-    /**
      * {@inheritdoc}
      * @throws Throwable
      */
@@ -219,6 +209,8 @@ class Swoole implements EventInterface
      */
     public function run(): void
     {
+        // Avoid process exit due to no listening
+        Timer::tick(100000000, static fn() => null);
         Event::wait();
     }
 

+ 22 - 8
src/Worker.php

@@ -37,6 +37,7 @@ use function stream_socket_accept;
 use function stream_socket_recvfrom;
 use function substr;
 use function array_walk;
+use function get_class;
 
 /**
  * Worker class
@@ -627,6 +628,9 @@ class Worker
         // State.
         static::$status = static::STATUS_STARTING;
 
+        // Avoiding incorrect user calls.
+        static::resetGlobalEvent();
+
         // For statistics.
         static::$globalStatistics['start_timestamp'] = time();
 
@@ -641,6 +645,20 @@ class Worker
     }
 
     /**
+     * reset globalEvent Instance.
+     *
+     * @return void
+     */
+    protected static function resetGlobalEvent(): void
+    {
+        if (static::$status === static::STATUS_STARTING &&
+        static::$globalEvent instanceof EventInterface) {
+            static::$eventLoopClass = get_class(static::$globalEvent);
+            static::$globalEvent = null;
+        }
+    }
+
+    /**
      * Lock.
      *
      * @param int $flag
@@ -1822,15 +1840,11 @@ class Worker
                 foreach (static::$pidMap as $workerId => $workerPidArray) {
                     $worker = static::$workers[$workerId];
                     if ($worker->reloadable) {
-                        foreach ($workerPidArray as $pid) {
-                            $reloadablePidArray[$pid] = $pid;
-                        }
-                    } else {
-                        foreach ($workerPidArray as $pid) {
-                            // Send reload signal to a worker process which reloadable is false.
-                            posix_kill($pid, $sig);
-                        }
+                        $reloadablePidArray += $workerPidArray;
+                        continue;
                     }
+                    // Send reload signal to a worker process which reloadable is false.
+                    array_walk($workerPidArray, static fn ($pid) => posix_kill($pid, $sig));
                 }
                 // Get all pids that are waiting reload.
                 static::$pidsToRestart = array_intersect(static::$pidsToRestart, $reloadablePidArray);