TP6消息队列使用

tp6中提供了很多种消息队列的链接方式,这里使用Redis数据库链接方式

免费小程序+公众号源码获取:点击获取

1.配置消息队列链接方式
首先配置/config/queue.php中的redis配置,这个配置可从.env当中读取如下图
在这里插入图片描述
2.下发消息队列
把tp原来的方法稍作调整,把延迟消息队列和正常的放在一起方便调用
整理出多任务和单任务的快捷使用方式

<?php
namespace crmeb\utils;

use crmeb\traits\ErrorTrait;
use think\facade\Config;
use think\facade\Queue as QueueThink;

/**
 * Class Queue
 * @package crmeb\utils
 * @method $this do(string $do) 设置任务执行方法
 * @method $this job(string $job) 设置任务执行类名
 * @method $this errorCount(int $errorCount) 执行失败次数
 * @method $this data(...$data) 执行数据
 * @method $this secs(int $secs) 延迟执行秒数
 * @method $this log($log) 记录日志
 */
class Queue
{

    use ErrorTrait;

    /**
     * 任务执行
     * @var string
     */
    protected $do = 'doJob';

    /**
     * 默认任务执行方法名
     * @var string
     */
    protected $defaultDo;

    /**
     * 任务类名
     * @var string
     */
    protected $job;

    /**
     * 错误次数
     * @var int
     */
    protected $errorCount = 3;

    /**
     * 数据
     * @var array|string
     */
    protected $data;

    /**
     * 任务名
     * @var null
     */
    protected $queueName = null;

    /**
     * 延迟执行秒数
     * @var int
     */
    protected $secs = 0;

    /**
     * 记录日志
     * @var string|callable|array
     */
    protected $log;

    /**
     * @var array
     */
    protected $rules = ['do', 'data', 'errorCount', 'job', 'secs', 'log'];

    /**
     * @var static
     */
    protected static $instance;

    /**
     * Queue constructor.
     */
    protected function __construct()
    {
        $this->defaultDo = $this->do;
    }

    /**
     * @return static
     */
    public static function instance()
    {
        if (is_null(self::$instance)) {
            self::$instance = new static();
        }
        return self::$instance;
    }

    /**
     * 放入消息队列
     * @param array|null $data
     * @return mixed
     */
    public function push(?array $data = null)
    {
        if (!$this->job) {
            return $this->setError('需要执行的队列类必须存在');
        }
        $res = QueueThink::{$this->action()}(...$this->getValues($data));
        $this->clean();
        return $res;
    }

    /**
     * 清除数据
     */
    public function clean()
    {
        $this->secs = 0;
        $this->data = [];
        $this->log = null;
        $this->queueName = null;
        $this->errorCount = 3;
        $this->do = $this->defaultDo;
    }

    /**
     * 获取任务方式
     * @return string
     */
    protected function action()
    {
        return $this->secs ? 'later' : 'push';
    }

    /**
     * 获取参数
     * @param $data
     * @return array
     */
    protected function getValues($data)
    {
        $jobData['data'] = $data ?: $this->data;
        $jobData['do'] = $this->do;
        $jobData['errorCount'] = $this->errorCount;
        $jobData['log'] = $this->log;
        if ($this->do != $this->defaultDo) {
            $this->job .= '@' . Config::get('queue.prefix', 'eb_') . $this->do;
        }
        if ($this->secs) {
            return [$this->secs, $this->job, $jobData, $this->queueName];
        } else {
            return [$this->job, $jobData, $this->queueName];
        }
    }

    /**
     * @param $name
     * @param $arguments
     * @return $this
     */
    public function __call($name, $arguments)
    {
        if (in_array($name, $this->rules)) {
            if ($name === 'data') {
                $this->{$name} = $arguments;
            } else {
                $this->{$name} = $arguments[0] ?? null;
            }
            return $this;
        } else {
            throw new \RuntimeException('Method does not exist' . __CLASS__ . '->' . $name . '()');
        }
    }
}


3.放入任务

<?php
namespace app\adminapi\controller;

use crmeb\utils\Queue;

class Test{
	public function index()
	{
		//单例调用
		Queue::instance()
		->job(WechatTemplateJob::class)//执行任务类名
		->data()//执行任务需要的参数,可无限制传数
		//->secs(1) 延迟1秒后执行任务
		->push();//放入任务
	}
}

4.写消费任务逻辑

<?php
/**
 * @author: liaofei<136327134@qq.com>
 * @day: 2020/5/21
 */

namespace crmeb\jobs;


use crmeb\basic\BaseJob;
use crmeb\services\template\Template;
use think\facade\Route;

/**
 * Class WechatTemplateJob
 * @package crmeb\jobs
 */
class WechatTemplateJob
{
	/**
     * @param $name
     * @param $arguments
     */
    public function __call($name, $arguments)
    {
        $this->fire(...$arguments);
    }

    /**
     * @param Job $job
     * @param $data
     */
    public function fire(Job $job, $data): void
    {
        try {
            $action = $data['do'] ?? 'doJob';//任务名
            $infoData = $data['data'] ?? [];//执行数据
            $errorCount = $data['errorCount'] ?? 0;//最大错误次数
            $log = $data['log'] ?? null;
            if (method_exists($this, $action)) {
                if ($this->{$action}(...$infoData)) {
                    //删除任务
                    $job->delete();
                    //记录日志
                    $this->info($log);
                } else {
                    if ($job->attempts() >= $errorCount && $errorCount) {
                        //删除任务
                        $job->delete();
                        //记录日志
                        $this->info($log);
                    } else {
                        //从新放入队列
                        $job->release();
                    }
                }
            } else {
                $job->delete();
            }
        } catch (\Throwable $e) {
            $job->delete();
            Log::error('执行消息队列发成错误,错误原因:' . $e->getMessage());
        }
    }

    /**
     * 打印出成功提示
     * @param $log
     * @return bool
     */
    protected function info($log)
    {
        try {
            if (is_callable($log)) {
                print_r($log() . "\r\n");
            } else if (is_string($log) || is_array($log)) {
                print_r($log . "\r\n");
            }
        } catch (\Throwable $e) {
            print_r($e->getMessage());
        }
    }

    /**
     * 任务失败执行方法
     * @param $data
     * @param $e
     */
    public function failed($data, $e)
    {

    }
    //可把面的几个方法写成另外一个类里面继承给当前类
	
	//doJob的参数由放入任务方法中的data决定
	public function doJob()
	{
		//执行逻辑
		//成功返回true,失败返回fasle,会再次加入队列。执行到最大错误后自动删除
		return true;
	}
}

5.启动消息队列
windows用户可在项目根目录开启命令行输入命令

php think queue:listen --queue CRMEB

进行启动消息队列
Linux需要守护进程才可以,推荐使用Supervisor命令都是一样的

启动报错,按照提示解禁对应函数就行了

启动后就行看到队列加入和消费的日志

  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值