• 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

  • 相关阅读:
    在Idea中使用Eclipse编译器
    Idea切换svn分支,类似Eclipse的Switch功能
    Spring AOP详解
    CGLib动态代理原理及实现
    IDEA下搜狗输入法输入中文时卡着不动的参考解决方法
    Nginx反向代理丢失cookie的问题
    redis连接池自动释放
    Redis常用命令
    waitpid之status意义解析
    bash中管道命令返回值如何确定(下)
  • 原文地址:https://www.cnblogs.com/laijinquan/p/16256986.html
Copyright © 2020-2023  润新知