问个workerman的问题?

我用的laravel 5.8
然后我在app/console/commands目录下创建两个文件test1.php,test2.php

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Workerman\Worker;
use Workerman\Lib\Timer;

class test extends Command
{
    protected $signature = 'test1  {worker_command}  {--mode=}';
    protected $worker;
    protected $events = [
        'onWorkerStart',
        'onConnect',
        'onMessage',
        'onClose',
        'onError',
        'onBufferFull',
        'onBufferDrain',
        'onWorkerStop',
        'onWorkerReload'
    ];

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
        $worker = new Worker();
        $this->worker = $worker;
        $worker->onWorkerStart = [$this,'onWorkerStart'];
        $this->worker->name = 'test1';
        $this->worker->count = 5;
    }
    public function handle()
    {
        $this->initWorker();
        $this->worker->runAll();
    }
    protected function initWorker()
    {
        global $argv;
        $argv[0] = "xxxxxx";
        $argv[1] = $command = $this->argument('worker_command');
        $mode = $this->option('mode');
        isset($mode) && $argv[2] = '-' . $mode;
        print_r($argv);
    }
}

两个差不多文件,然后去我启动
php artisan test1 ,不知道为什么总是把test2 也一起启动了,
就问下这workerman runAll时候如何只启动自己的文件

最佳答案

其实这也可以说是laravel的一个bug,我之前做队列任务的时候,遇到类似的情况。
本质原因就是 laravel 执行 php artisan 会把Command都扫一遍,然后会执行到 __construct(),导致把所有命令任务的__construct()都跑到了。

从此之后,我写Command的初始化操作,都是尽量能不在__construct()写就不在这写,基本都放到了 handle(),要是初始化代码比较多,觉得臃肿,我就封装多一个 _init(),然后在 handle()调用他

2年前 评论
李铭昕 1年前
忆往昔弹指间 (作者) 1年前
李铭昕 1年前
忆往昔弹指间 (作者) 1年前
李铭昕 1年前
讨论数量: 9

其实这也可以说是laravel的一个bug,我之前做队列任务的时候,遇到类似的情况。
本质原因就是 laravel 执行 php artisan 会把Command都扫一遍,然后会执行到 __construct(),导致把所有命令任务的__construct()都跑到了。

从此之后,我写Command的初始化操作,都是尽量能不在__construct()写就不在这写,基本都放到了 handle(),要是初始化代码比较多,觉得臃肿,我就封装多一个 _init(),然后在 handle()调用他

2年前 评论
李铭昕 1年前
忆往昔弹指间 (作者) 1年前
李铭昕 1年前
忆往昔弹指间 (作者) 1年前
李铭昕 1年前

复现论证方法,创建 Test.php,然后你不用专门执行它,只执行 php artisan,你会发现,也打印出了东西

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

/**
 * Class Test
 * @package App\Console\Commands
 * @
 */
class Test extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'test';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
        var_dump('test 1');
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        var_dump('handle 1');
    }
}
2年前 评论

因为你启动的两个workerman,使用的 pid 存储路径是相同的,所以启动第二个的时候,检测到 pid 已存在,则认为是已经启动了,想要分别启动需要重新设置两者的 pid 保存路径。
可参考 smileymrking/laravel-gateway-worker 如果喜欢可以点个小星星 :blush:

2年前 评论

Worker::$pidFile = '/www/wwwroot/test/public/test1.pid'; 分别为2个命令定义一下pid文件

2年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!