thinkphp6.0 消息队列

think-queue的使用方法

 

使用thinkphp6

composer require topthink/think-queue

定义任务

app/job/RegEmailJob.php

<?php


namespace app\job;

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





class RegEmailJob
{
    public function fire(Job $job, $data){



        //....这里执行具体的任务



        // 日志
        //Log::info("我被执行了");

        $body = '请于24小时点击该链接完成验证. ' . env('app.host') . '/validate/email'
            . '?user_id=' . $data['user_id'] . '&code=' . $data['uuid'];

        //发送邮件
        sendmail($data['email'], '执笔书写成功博客验证', $body);


        if ($job->attempts() > 3) {
            //通过这个方法可以检查这个任务已经重试了几次了
        }


        //如果任务执行成功后 记得删除任务,不然这个任务会重复执行,直到达到最大重试次数后失败后,执行failed方法
        $job->delete();


        // 也可以重新发布这个任务
        //$job->release(0); //$delay为延迟时间

    }

    public function failed($data){

        // ...任务达到最大重试次数后,失败了
    }


}

 

消费

在控制器中调用

Queue::push('app\job\regEmailJob', ['user_id'=>$res['id'],'uuid'=>$uuid,'email'=>$res['email']]);

启动
php think queue:listen
php think queue:work

php think queue:work与php think queue:listen区别
参考地址:
https://www.iecz.com/note/1810.html

官网教程
https://github.com/top-think/think-queue

 

来源:https://blog.csdn.net/qq_35081380/article/details/118243229

参考:http://t.zoukankan.com/fps2tao-p-15145470.html

posted @ 2022-05-11 10:45  1O(∩_∩)O1  阅读(452)  评论(0编辑  收藏  举报