1.修改根目录 .env 文件的QUEUE_CONNECTION字段配置 为database

Laravel可配置多种队列驱动,包括 "sync", "database", "beanstalkd", "sqs", "redis", "null"(具体参见app/config/queue.php)
其中sync为同步,database为使用数据库,后面三种为第三方队列服务,最后一种为不使用队列。
通过在 .env 中的 QUEUE_CONNECTION 选项,来决定选择何种驱动。
如 QUEUE_CONNECTION=database 即为选择数据库驱动队列。

2.创建数据库队列表(database方式)

为了使用 database 队列驱动,你需要一张数据表来存储任务。
php artisan queue:table
生产一张队列表
php artisan migrate

1059579-20191024095453196-1671638965.png

3.生成任务类

在你的应用程序中,队列的任务类都默认放在 app/Jobs 目录下。

如果这个目录不存在,那当你运行 make:job Artisan 命令时目录就会被自动创建。

你可以用以下的 Artisan 命令来生成一个新的队列任务:

php artisan make:job SynUser

微信截图_20220222173402.png

4.在队列类里面的handle方法中写上业务逻辑(比如发送网络请求,发送邮件等等)

<?php

namespace App\Jobs;

use App\Models\Mobile;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;


class SynUser implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    protected $nums;
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($nums)
    {
        //
        $this->nums = $nums;

    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        for ($i = 1; $i <= $this->nums;$i++) {
            $arr = [//组建数组
                'mobile' =>"137".mt_rand(10000000,99999999),
                'operator' => '运营商',
                'qcellcore' => '归属地',
                'low_price'=>mt_rand(100,999),
                'price'=>(mt_rand(100,999)+10),
                'status'=>'1',
                'telephone_bill'=>mt_rand(10,99),
                'owner_id'=>mt_rand(0,3),
                'low_consumption'=>0,
                'is_sign'=>mt_rand(0,3),
                'remarks'=>'',
                'is_markup'=>mt_rand(0,1),
            ];
            $res = DB::table('mobile')->insert($arr);
            if($res){
                Log::info('测试',[$i]);

            }
        }

    }

    public function failed(\Exception $exception)
    {
//        TODO 给用户发送任务失败的通知,等等……
        Log::error('失败了');
    }

}

5.在控制器中加入一个测试方法,直接调用队列类的 dispatch方法,类似生产了一个任务

public function ceshi(\Illuminate\Http\Request $request){

        //php artisan queue:restart 队列重启
        //php artisan queue:work --stop-when-empty  执行队列,加这个--stop-when-empty是完毕后自动退出
        $maxNums = 1000; //一次插入1000条
        //验证
        //生成队列任务
        $nums = $request->input('num');

        for ($i = 1; $i >= 0; $i += $maxNums) {

            if ($nums <= 0) {
                break;
            }

            if ($nums >= $maxNums) {
                //如果大于1000个要添加则添加任务延迟执行
                !empty($lestTime) ?: $lestTime = now();//初始化时间
                $nextTime = $lestTime->addMilliseconds(100);
                //添加队列任务
                SynUser::dispatch($maxNums)
                    ->delay($nextTime);//延迟执行
                //更新时间
                $nums -= $maxNums;
                $lestTime = $nextTime;
            } else {
                //如果小于一千个则立即执行

                //SynUser::dispatchNow($nums);
                SynUser::dispatch($nums);
                $nums -= $nums;
            }


        }

        return "exit";
    }

这时,由于队列还没开启,数据库的队列表中会有一个任务,等待队列启动后执行

微信截图_20220222174053.png

6.执行命令开启队列

php artisan queue:work

这时,队列表中的任务执行后就会自动删除了,类似消费了任务
微信截图_20220222173402.png
更多可以参考laravel的队列文档
测试100分钟插入数据大约82000条,速度还是有点慢,可能是我的本地电脑配置不高


本文由 来鹏飞 创作,采用 知识共享署名 3.0,可自由转载、引用,但需署名作者且注明文章出处。

还不快抢沙发

添加新评论