Itinysun / laraman

Run laravel with workman ,1 artisan command, 10x speed up
Other
97 stars 9 forks source link

v2.0.2 同样报错 #6

Closed Lany-w closed 1 year ago

Lany-w commented 1 year ago
Undefined array key 2 in file /Users/xxx/www/mes-from-laravel-edge/laraman on line 20
laraman v2.0.0 beta
-------------------------------------------- WORKERMAN --------------------------------------------
Workerman version:4.1.13          PHP version:8.1.22           Event-Loop:\Workerman\Events\Select
--------------------------------------------- WORKERS ---------------------------------------------
proto   user            worker          listen                   processes    status
tcp       xxx      web             http://127.0.0.1:8000    16            [OK]
tcp       xxx      monitor         none                     1             [OK]
---------------------------------------------------------------------------------------------------
Press Ctrl+C to stop. Start success.
Undefined array key "00000000000000040000000000000000" in file /Users/xxx/www/mes-from-laravel-edge/vendor/workerman/workerman/Worker.php on line 1657
发生异常:array_search(): Argument #2 ($haystack) must be of type array, null given
异常代码:0
异常文件:/Users/xxx/www/mes-from-laravel-edge/vendor/workerman/workerman/Worker.php
异常行号:1657
异常追踪:#0 /Users/xxx/www/mes-from-laravel-edge/vendor/workerman/workerman/Worker.php(1657): array_search(0, NULL)
#1 /Users/xxx/www/mes-from-laravel-edge/vendor/workerman/workerman/Worker.php(1584): Workerman\Worker::getId('000000000000000...', 0)
#2 /Users/xxx/www/mes-from-laravel-edge/vendor/workerman/workerman/Worker.php(1426): Workerman\Worker::forkOneWorkerForLinux(Object(Itinysun\Laraman\Server\LaramanWorker))
#3 /Users/xxx/www/mes-from-laravel-edge/vendor/workerman/workerman/Worker.php(1400): Workerman\Worker::forkWorkersForLinux()
#4 /Users/xxx/www/mes-from-laravel-edge/vendor/itinysun/laraman/src/Server/LaramanWorker.php(26): Workerman\Worker::forkWorkers()
#5 /Users/xxx/www/mes-from-laravel-edge/vendor/itinysun/laraman/src/Command/Laraman.php(65): Itinysun\Laraman\Server\LaramanWorker::RunAll()
#6 /Users/xxx/www/mes-from-laravel-edge/laraman(25): Itinysun\Laraman\Command\Laraman::run()
#7 {main}

不是start.php 启动参数那里的问题

Lany-w commented 1 year ago
class LaramanWorker extends Worker
{
    /**
     * 去掉了默认的resetStd()
     * @throws Exception
     */
    public static function RunAll(): void
    {
        static::lock();
        static::daemonize();
        static::initWorkers();
        static::installSignal();
        static::saveMasterPid();
        static::lock(\LOCK_UN);
        static::displayUI();
        //static::forkWorkers();
        //static::resetStd();
        static::monitorWorkers();
    }

static::forkWorkers这个方法引起的问题,但是我看webman里也调用了Worker的这个方法,是可以正常启动的, 我注释掉这个方法可以正常启动了,但是请求响应很慢很慢

Lany-w commented 1 year ago

我应该是找到问题了. webman在runAll之前有过这个动作

$worker = new Worker($config['listen'], $config['context']);  

这是Worker的构造方法

 public function __construct($socket_name = '', array $context_option = array())
    {
        // Save all worker instances.
        $this->workerId                    = \spl_object_hash($this);
        static::$_workers[$this->workerId] = $this;

laraman好像没有实例化Worker的动作,static::$_workers也就是空的, 导致initId这个方法是不会给 static::$_idMap赋值

protected static function initId()
    {
        foreach (static::$_workers as $worker_id => $worker) {
            $new_id_map = array();
            $worker->count = $worker->count < 1 ? 1 : $worker->count;
            for($key = 0; $key < $worker->count; $key++) {
                $new_id_map[$key] = isset(static::$_idMap[$worker_id][$key]) ? static::$_idMap[$worker_id][$key] : 0;
            }
            static::$_idMap[$worker_id] = $new_id_map;
        }
    }

所以这里就出问题了

 protected static function forkOneWorkerForLinux(self $worker)
    {
        // Get available worker id.
        $id = static::getId($worker->workerId, 0);
 protected static function getId($worker_id, $pid)
    {
        return \array_search($pid, static::$_idMap[$worker_id]);
    }
Itinysun commented 1 year ago

修复了一系列问题请使用v2.0.6