前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >TP5系列 | Console 命令行(CLI模式)

TP5系列 | Console 命令行(CLI模式)

作者头像
Tinywan
发布2019-08-06 14:21:23
2.9K0
发布2019-08-06 14:21:23
举报
文章被收录于专栏:开源技术小栈
创建指令
代码语言:javascript
复制
cd tp5
php think make:command commom/TestJobs

生成指令

文件TestJobs.php

代码语言:javascript
复制
<?php
namespace app\command;

use think\console\Command;
use think\console\Input;
use think\console\Output;

class TestJobs extends Command
{
    protected function configure()
    {
        // 指令配置
        $this->setName('app\command\testjobs');
        // 设置参数
    }

    protected function execute(Input $input, Output $output)
    {
        // 指令输出
        $output->writeln('app\command\testjobs');
    }
}

修改指令

文件TestJobs.php

代码语言:javascript
复制
class TestJobs extends Command
{
    protected function configure()
    {
        // 指令配置
        $this->setName('hello')->setDescription('This is Test cmd eg: hello:add | hello:edit');
        // 设置参数
        $this->addArgument('type', 1, 'this is test Jobs');
    }

    protected function execute(Input $input, Output $output)
    {
        // 指令输出
        $type = $input->getArgument('type');
        if ($type == 'hello:add') {
            $this->helloAdd();
        } elseif ($type == 'hello:edit') {
            $this->helloEdit();
        }
    }

    /**
     * @Desc: 自定义方法1
     * @Author: Tinywan
     */
    public function helloAdd()
    {
        Log::error('自定义命令行:'.__METHOD__);
    }

    /**
     * @Desc: 自定义方法2
     * @Author: Tinywan
     */
    public function helloEdit()
    {
        Log::error('自定义命令行:'.__METHOD__);
    }
}

配置命令

在 application 目录下面的 command.php(如果不存在则创建)文件中添加如下内容

代码语言:javascript
复制
return [
    \app\command\TestJobs::class
];

查看命令

代码语言:javascript
复制
$ php think

Available commands:
  build              Build Application Dirs
  clear              Clear runtime file
  hello              This is Test cmd eg: hello:add | hello:edit

出现hello This is Test cmd eg: hello:add | hello:edit 标识配置成功

执行命令

代码语言:javascript
复制
$ php think hello hello:add

查看日志1

代码语言:javascript
复制
[2019-07-22T10:57:57+08:00][ error ] 自定义命令行:app\command\TestJobs::helloAdd
error

编辑命令:hello:edit

代码语言:javascript
复制
$ php think hello hello:edit

查看日志2

代码语言:javascript
复制
[2019-07-22T10:57:57+08:00][ error ] 自定义命令行:app\command\TestJobs::helloAdd
error
[2019-07-22T10:59:50+08:00][ error ] 自定义命令行:app\command\TestJobs::helloEdit
error

命令行挂起守护进程执行

代码语言:javascript
复制
nohup /usr/bin/php /var/www/tp5/think hello hello:edit > /dev/null 2> /dev/null &

以上命令适合做一个阻塞的操作,如Redis发布订阅的,订阅操作。关于更多的nohup命令请看历史记录相关接受,快门地址:

命令行扩展

Redis不是有发布订阅吗?

是啊,有啊。要做订单过期事件吗?

命令行代码

代码语言:javascript
复制
class Jobs extends Command
{
    protected function configure()
    {
        $this->addArgument('type', 1, 'this is type');
        $this->setName('jobs')->setDescription('order_count|redis-notify-key-events|transaction_account_tables');
    }

    /**
     * 执行任务
     */
    protected function execute(Input $input, Output $output)
    {
        $type = $input->getArgument('type');
        if ($type == 'redis-notify-key-events') {
            $this->notifyKeySpaceEvents();
        } 
    }

    /**
     * @desc: Redis键过期事件
     */
    private function notifyKeySpaceEvents()
    {
        $service = new RedisSubscribe();
        $service->subscribe();
    }

1、可以自定义一个命令行Jobs,参数redis-notify-key-events

2、守护进程订阅消息:nohup /usr/local/php/bin/php /var/www/tp5/think jobs redis-notify-key-events > /dev/null 2> /dev/null &

RedisSubscribe 类

代码语言:javascript
复制
class RedisSubscribe
{
    public function subscribe()
    {
        $redis = BaseRedis::plocal();
        $redis->setOption(\Redis::OPT_READ_TIMEOUT, -1);
        // 这里是键空间过期是选用的1号数据库
        $redis->psubscribe(array('__keyevent@1__:expired'), function ($redis, $pattern, $chan, $msg) {
            Log::info('[订阅事件] 过期KEY ' . $msg);
            $originData = explode(':', $msg);
            $event_key = $originData[1] ?? '0';
            $event_status = $originData[0] ?? '0';
            // 根据事件类型做出业务处理既可
        });
    }
}

$msg 变量就是一个Redis的键key,如,订单过期时间(订单多长时间后延迟取消),可以设置键:S20190722100001:1001

1、S20190722100001 表示订单号

2、1001表示是具体是哪个订单事件。如:1001表示订单过期(二维码过期不能够进行支付,失效二维码),1002表示多长时间之内(30分钟之内之未支付取消)未支付需要取消的订单事件。

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2019-08-01,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Tinywan的杂货摊 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
云数据库 Redis
腾讯云数据库 Redis(TencentDB for Redis)是腾讯云打造的兼容 Redis 协议的缓存和存储服务。丰富的数据结构能帮助您完成不同类型的业务场景开发。支持主从热备,提供自动容灾切换、数据备份、故障迁移、实例监控、在线扩容、数据回档等全套的数据库服务。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档