laravel中队列的使用

队列介绍
Laravel队列服务为各种不同的后台队列提供了同意的API
允许推迟耗时任务(例如发送邮件)的执行,从而大幅度提高了web
请求速度


主要步骤
1.迁移队列需要的数据表 


生成并创建数据表 php artisan queue:table
  php artisan migrate


生成并创建错误数据表 php artisan queue:failed-table
         php artisan migrate
2.编写任务类  php artisan make:job SendEmail(创建完成后在APP\Jobs下)
use Mail;
protected $email;


public function __construct($email)
    {
        //
        $this->email = $email;


    }




public function handle()
    {
        //发送邮件
        Mail::raw('队列测试',function ($message)
        {
            $message->from('[email protected]');
            $message->subject('邮件主题 测试');
            $message->to($this->email);


        });


        //用错误日志方法
        //Log::info('已发送邮件 - ',$this->email);
        


    }
3.推送任务到队列
路由访问:  http://localhost/blog1/public/queue
查看jobs表中的数据


4.运行队列监听器
php artisan queue:listen
会出现状态框


5.处理失败任务
php artisan queue:failed //查看错误的记录
php artisan queue:retry 1(这里的1为用上面的命令查出来的id)
php artisan queue:retry all //执行所有的错误记录
php artisan queue:forget 1 //删除队列中的任务 1同样是id
php artisan queue:flush //删除所有的队列中的任务




使用
配置:config/queue.php
Supported: "sync//同步驱动", "database", "beanstalkd", "sqs", "redis", "null"

猜你喜欢

转载自blog.csdn.net/qq_35472880/article/details/80086079
今日推荐