walkor 10 lat temu
rodzic
commit
ba343f1bb7
1 zmienionych plików z 53 dodań i 4 usunięć
  1. 53 4
      Worker.php

+ 53 - 4
Worker.php

@@ -34,7 +34,7 @@ class Worker
      * 版本号
      * @var string
      */
-    const VERSION = '3.2.0';
+    const VERSION = '3.2.1';
     
     /**
      * 状态 启动中
@@ -80,6 +80,12 @@ class Worker
     const MAX_UDP_PACKEG_SIZE = 65535;
     
     /**
+     * worker id
+     * @var int
+     */
+    public $id = 0;
+    
+    /**
      * worker的名称,用于在运行status命令时标记进程
      * @var string
      */
@@ -261,6 +267,13 @@ class Worker
     protected static $_pidsToRestart = array();
     
     /**
+     * 所有进程pid到id的映射
+     * 格式为[worker_id=>[0=>$pid, 1=>$pid, ..], ..]
+     * @var array
+     */
+    protected static $_idMap = array();
+    
+    /**
      * 当前worker状态
      * @var int
      */
@@ -339,7 +352,7 @@ class Worker
      * 初始化一些环境变量
      * @return void
      */
-    public static function init()
+    protected static function init()
     {
         // 如果没设置$pidFile,则生成默认值
         if(empty(self::$pidFile))
@@ -361,7 +374,8 @@ class Worker
         self::$_statisticsFile = sys_get_temp_dir().'/workerman.status';
         // 尝试设置进程名称(需要php>=5.5或者安装了proctitle扩展)
         self::setProcessTitle('WorkerMan: master process  start_file=' . self::$_startFile);
-        
+        // 初始化id
+        self::initId();
         // 初始化定时器
         Timer::init();
     }
@@ -408,6 +422,18 @@ class Worker
     }
     
     /**
+     * 初始化idMap
+     * return void
+     */
+    protected static function initId()
+    {
+        foreach(self::$_workers as $worker_id=>$worker)
+        {
+            self::$_idMap[$worker_id] = array_fill(0, $woker->count, 0);
+        }
+    }
+    
+    /**
      * 获得运行当前进程的用户名
      * @return string
      */
@@ -450,7 +476,7 @@ class Worker
      * php yourfile.php start | stop | restart | reload | status
      * @return void
      */
-    public static function parseCommand()
+    protected static function parseCommand()
     {
         // 检查运行命令的参数
         global $argv;
@@ -763,10 +789,13 @@ class Worker
     protected static function forkOneWorker($worker)
     {
         $pid = pcntl_fork();
+        // 获得可用的id
+        $id = self::getId($worker->workerId, 0);
         // 主进程记录子进程pid
         if($pid > 0)
         {
             self::$_pidMap[$worker->workerId][$pid] = $pid;
+            self::$_idMap[$worker->workerId][$id] = $pid;
         }
         // 子进程运行
         elseif(0 === $pid)
@@ -781,6 +810,7 @@ class Worker
             Timer::delAll();
             self::setProcessTitle('WorkerMan: worker process  ' . $worker->name . ' ' . $worker->getSocketName());
             self::setProcessUser($worker->user);
+            $worker->id = $id;
             $worker->run();
             exit(250);
         }
@@ -789,6 +819,21 @@ class Worker
             throw new Exception("forkOneWorker fail");
         }
     }
+    
+    /**
+     * 获得可用的worker->id,以便传递给子进程
+     * @param int $worker_id
+     * @param int $pid
+     */
+    protected static function getId($worker_id, $pid)
+    {
+        $id = array_search($pid, self::$_idMap[$worker_id]);
+        if($id === false)
+        {
+            echo "{$worker->name} getId fail\n";
+        }
+        return $id;
+    }
 
     /**
      * 尝试设置运行当前进程的用户
@@ -873,6 +918,10 @@ class Worker
                         // 清除子进程信息
                         unset(self::$_pidMap[$worker_id][$pid]);
                         
+                        // 标记$id为可用id
+                        $id = self::getId($worker_id, $pid);
+                        self::$_idMap[$worker_id][$id] = 0;
+                        
                         break;
                     }
                 }