[SF] Symfony 在 console 中结合 Workerman

通过llama.cpp与羊驼聊天的网页界面- 详解 Serge 的启动使用

 

在web框架的console中,命令不再是直接指定入口文件,如以往 php test.php start,而是类似 php app/console do 的形式。

workerman 对命令的解析是 parseCommand 方法,里面主要是处理 $argv 全局变量。

那么我们只需要在自己的逻辑中对其重新赋值,满足 $argv[1] 是动作 start | stop | restart | ... 即可,那么剩余workerman参数就是 $argv[2],依次类推。

 

Symfony2 command:

复制代码
namespace AppBundle\Command;

use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

use Workerman\Connection\TcpConnection;
use Workerman\Worker;

/**
 * @author farwish <farwish(a)foxmail.com>
 *
 * Class DataWorkerCommand
 * @package AppBundle\Command
 */
class DataWorkerCommand extends BaseCommand
{
    public function configure()
    {
        $this->setName('xc:data:worker')
            ->setDescription('启动服务')
            ->addArgument(
                'subcommands',
                InputArgument::IS_ARRAY,
                '可选多个参数'
            )
        ;
    }

    /**
     * app/console xc:data:worker start d [g]
     * app/console xc:data:worker stop
     * app/console xc:data:worker status
     * app/console xc:data:worker restart d [g]
     *
     * @param InputInterface $input
     * @param OutputInterface $output
     */
    public function execute(InputInterface $input, OutputInterface $output)
    {
        parent::execute($input, $output);

        global $argv;

        /* Original data like
        Array
        (
            [0] => worker.php
            [1] => start
            [2] => -d
            [3] => -g
        )
        */
        /* Console data like
        Array
        (
            [0] => app/console
            [1] => xc:data:worker
            [2] => start
            [3] => d
            [4] => g
        )
        */

        // So redefine arguments
        if (isset($argv[2])) {
            $argv[1] = $argv[2];
            if (isset($argv[3])) {
                $argv[2] = "-{$argv[3]}";
                if (isset($argv[4])) {
                    $argv[3] = "-{$argv[4]}";
                } else {
                    unset($argv[3]);
                }
            } else {
                unset($argv[2]);
            }
        }

        // worker
        $worker = new Worker("websocket://0.0.0.0:9000");

        $worker->count = 4;

        $worker->onMessage = function ($connection, $data)
        {
            /* @var TcpConnection $connection */
            $connection->send('hello');
        };

        Worker::runAll();
    }
}
复制代码

Thats all.

 

Refer:Symfony结合Workerman 

Link: http://www.cnblogs.com/farwish/p/7988617.html

posted on   ercom  阅读(962)  评论(0编辑  收藏  举报

编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架

统计

点击右上角即可分享
微信分享提示