ThinkPHP5 自定义指令守护进程模式运行

事件前瞻:就是公司买了一套聊天的代码,用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' => '',  // 连接密码,如果有设置密码的话
         

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值