cd tp5
php think make:command commom/TestJobs
生成指令
文件TestJobs.php
<?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
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(如果不存在则创建)文件中添加如下内容
return [
\app\command\TestJobs::class
];
查看命令
$ 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 标识配置成功
执行命令
$ php think hello hello:add
查看日志1
[2019-07-22T10:57:57+08:00][ error ] 自定义命令行:app\command\TestJobs::helloAdd
error
编辑命令:hello:edit
$ php think hello hello:edit
查看日志2
[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
命令行挂起守护进程执行
nohup /usr/bin/php /var/www/tp5/think hello hello:edit > /dev/null 2> /dev/null &
以上命令适合做一个阻塞的操作,如Redis发布订阅的,订阅操作。关于更多的nohup命令请看历史记录相关接受,快门地址:
命令行扩展
Redis不是有发布订阅吗?
是啊,有啊。要做订单过期事件吗?
命令行代码
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 类
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分钟之内之未支付取消)未支付需要取消的订单事件。