随笔 - 612, 文章 - 0, 评论 - 31, 阅读 - 74万

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

TP6:自定义Workerman指令

Posted on   eastson  阅读(542)  评论(0编辑  收藏  举报

https://www.kancloud.cn/manual/thinkphp6_0/1147857

有时候我们希望使用think的命令行运行workerman,这里做一个介绍,

通过 composer 安装

composer require topthink/think-worker

1:先新建一个指令,参考文档:自定义指令,比如新建命令:

php think make:command Hello hello

2:复制下面的代码到指令里,覆盖原始的configure和execute方法

  protected function configure()
  {
    // 指令配置
    $this->setName('convert')
      ->addArgument('action', Argument::OPTIONAL, "start|stop|restart|reload|status|connections", 'start')
      ->addOption('mode', 'm', Option::VALUE_OPTIONAL, 'Run the workerman server in daemon mode.')
      ->setDescription('the workerman command');
  }

  protected function execute(Input $input, Output $output)
  {
    // 指令输出
    $output->writeln('convert start');

    $action = $input->getArgument('action');
    $mode = $input->getOption('mode');


    // 重新构造命令行参数,以便兼容workerman的命令
    global $argv;

    $argv = [];

    array_unshift($argv, 'think', $action);
    if ($mode == 'd') {
      $argv[] = '-d';
    } else if ($mode == 'g') {
      $argv[] = '-g';
    }

    // 在这里放心的实例化worker,
    // 就像参照workerman文档写一样,
    // 无非在workerman的文档里,代码是新建纯php文件,但在这里,写到了一个方法里.
    $worker_1 = new Worker();
    $worker_2 = new Worker();

    Worker::runAll();
  }

3:运行的时候,使用如下命令:

//临时运行
php think hello start
//后台运行
php think hello start --mode d
相关博文:
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 字符编码:从基础到乱码解决
历史上的今天:
2014-05-10 How to authenticate a user by uid and password?
2014-05-10 LDAP 中 CN,OU,DC 的含意
2014-05-10 Apache Directory Studio
2014-05-10 LDAP编辑器 LDAPAdmin
2013-05-10 Windows XP下获取OpenERP源码
2013-05-10 关于OpenERP客户端字体问题的一点心得
点击右上角即可分享
微信分享提示