事件前瞻:就是公司买了一套聊天的代码,用gateway推送消息,但是基本每天要掉一下消息。但是gateway推送的消息,成没成功推送,成没成功接收,都没有返回值。所以打算写个循环,把所有消息存redis循环推一次,成功了就清掉redis,失败推送3次,3次都推送失败,就直接干掉!没有返回值,只能写定时器,通过自定义指令来触发,于是开整。。。。。。。。
1、首先看了tp5开发文档一大堆,最后还是得用shell script。先看看tp5开发文档介绍:https://www.kancloud.cn/manual/thinkphp5_1/354146/354146
2、首先找到自己得项目,看看结构。
3、在项目根目录下创建指令,执行命令:php think make:command SelfTimer
备注:SelfTimer这个是要创建指令的文件名,可以随意取。
我是做完了采写的,所以线上的看不到了,就看看本地的吧,附上截图:
其实线上与本地一样,执行这条命令后,你会发现,在你的app下会多一个command文件夹,该文件夹下会多一个SelfTimer.php文件,内容如下:
<?php
namespace app\command;
use think\console\Command;
use think\console\Input;
use think\console\Output;
class SelfTimer extends Command
{
protected function configure()
{
// 指令配置
$this->setName('selftimer');
// 设置参数
}
protected function execute(Input $input, Output $output)
{
// 指令输出
$output->writeln('selftimer');
}
}
4、修改SelfTimer.php文件内容如下:
<?php
namespace app\command;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use Workerman\Worker;
use \app\common\controller\SendData;
use redis\Redis;
use \app\im\model\mongo\Chat;
class SelfTimer extends Command
{
protected $interval = 5;
protected $redis = null;
protected function configure()
{
// 指令配置
$this->setName('timer')
->setDescription('开启/关闭/重启 定时任务');
// 指令配置
// $this->setName('app\command\selftimer');
}
protected function execute(Input $input, Output $output)
{
$config = [
'host' => '127.0.0.1', //服务器连接地址。默认='127.0.0.1'
'port' => '6379', //端口号。默认='6379'
'password' => '', // 连接密码,如果有设置密码的话