think-queue消息队列的使用方法

本文介绍了如何在ThinkPHP框架中配置消息队列,包括创建配置文件、抽象类、业务逻辑处理类以及使用命令行执行队列和Supervisor守护进程的设置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、创建配置文件:config/queue.php

<?php
return [
    'default' => 'redis',
    'connections' => [
        'sync' => [
            'type' => 'sync',
        ],
        'database' => [
            'type' => 'database',
            'queue' => 'default',
            'table' => 'jobs',
            'connection' => null,
        ],
        'redis' => [
            'type' => 'redis',
            'queue' => 'default',
            'host' => '127.0.0.1',
            'port' => 6379,
            'password' => '',
            'select' => 1,
            'timeout' => 0,
            'persistent' => false,
        ],
    ],
    'failed' => [
        'type' => 'none',
        'table' => 'failed',
    ],
];

2、创建abstract类:app/job/Queue.php

<?php
namespace app\job;

use think\App;
use think\facade\Log;
use think\queue\Job;

abstract class Queue
{
    /**
     * 创建任务
     */
    public function fire(Job $job, $data)
    {
        if (!$data) {
            Log::error(sprintf(' ==> [%s][%s] 队列无消息', __CLASS__, __FUNCTION__));
        }
        $result = $this->execute($data);
        if ($result) {
            Log::record(sprintf(' ==> [%s][%s] 队列执行成功', __CLASS__, __FUNCTION__));
            $job->delete();
        } else {
            if ($job->attempts() > 3) {
                Log::error(sprintf(' ==> [%s][%s] 队列执行重试次数超过3次,执行失败', __CLASS__, __FUNCTION__));
                $job->delete();
            } else {
                Log::error(sprintf(' ==> [%s][%s] 队列执行失败,延迟3秒后重试', __CLASS__, __FUNCTION__));
                $job->release(3);
            }
        }
    }
    /**
     * 执行业务逻辑
     */
    abstract protected function execute($data): bool;
}

3、创建业务逻辑处理类:app/job/Task.php

<?php
namespace app\job;

use app\job\Queue;
use think\App;
use think\facade\Log;

class Task extends Queue
{
    /**
     * 执行业务逻辑
     */
    protected function execute($data): bool
    {
        Log::record(sprintf(' ==> [%s][%s] 执行成功', __CLASS__, __FUNCTION__));
        return true;
    }
}

4、添加消息队列

$handle_name = 'app\job\Task';
$queue_name = 'task';
$params = [
    'type' => 'test',
    'record_id' => 123,
];
// 立即执行
$pushed = Queue::push($handle_name, json_encode($params, JSON_UNESCAPED_UNICODE), $queue_name);
// 延迟5秒执行
// $pushed = Queue::later(5, $handle_name, $params, $queue_name);

5、命令行执行队列

php think queue:listen --queue task

6、supervisor配置守护进程:/supervisor/example.conf

[program:example]
command=php think queue:work --queue task
directory=/www/example
process_name=%(program_name)s_%(process_num)02d
user=www
autostart=true
autorestart=true
startsecs=1
startretries=20
numprocs=1
priority=100
stdout_logfile_maxbytes=2MB
stderr_logfile_maxbytes=2MB
stdout_logfile=/etc/supervisor/logs/example.out.log
stderr_logfile=/etc/supervisor/logs/example.err.log
欢迎加入IT技术交流接单微信群

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xmode

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值