标签

Honeymoon - Thomas Ng

归档

近期文章

laravel 基于redis队列

本文环境 PHP7.3,Laravel6.0 不懂的可以评论或联系我邮箱:owen@owenzhang.com 著作权归OwenZhang所有。商业转载请联系OwenZhang获得授权,非商业转载请注明出处。

配置文件

1.config/queue.php

'default' =>env('QUEUE_CONNECTION', 'redis'),

 'redis' => [
            'driver' => 'redis',
            'connection' => 'queue',
            'queue' => env('REDIS_QUEUE', 'queueList'),
            'retry_after' => 90,
            'block_for' => 5,
        ],

'failed' => [
        'driver' => env('QUEUE_FAILED_DRIVER', 'database'),
        'database' => env('DB_CONNECTION', 'mysql'),
        'table' => 'failed_jobs',
    ],

2.config/database.php

'redis' => [
                'queue' => [
                                        'url' => env('REDIS_URL'),
                                        'host' => env('REDIS_HOST', '127.0.0.1'),
                                        'password' => env('REDIS_PASSWORD', null),
                                        'port' => env('REDIS_PORT', '6379'),
                                        'database' => env('REDIS_QUEUE_DB', '15'),
                                ],
],

添加错误记录表mysql

CREATE TABLE `failed_jobs` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `connection` text COLLATE utf8mb4_unicode_ci NOT NULL,
  `queue` text COLLATE utf8mb4_unicode_ci NOT NULL,
  `payload` longtext COLLATE utf8mb4_unicode_ci NOT NULL,
  `exception` longtext COLLATE utf8mb4_unicode_ci NOT NULL,
  `failed_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

队列文件

data = $data;
    }

    //        //调用
//        $data = [
//            [
//                'title' => 'a1',
//                'sort'  => '11',
//            ],
//            [
//                'title' => 'a2',
//                'sort'  => '22',
//            ],
//        ];
//        $res  = EmailJob::dispatch($data)->onQueue('emailQueue');
//        var_dump($res);
//        die();
    /**
     * 执行任务
     *
     * @return void
     */
    public function handle()
    {
        //处理业务逻辑
        Log::info('jobowen', $this->data);
        foreach ($this->data as $datum) {
            DB::table('copywriting_config')->insert([
                'title' => $datum['title'],
                'sort'  => $datum['sort'],
            ]);
        }
    }

    /**
     * 任务失败的处理过程
     *
     * @param \Exception $exception
     * @return void
     */
    public function failed(\Exception $exception)
    {
        // 给用户发送任务失败的通知,等等……
        Log::info('jobowenException', $exception->getMessage());
    }

}

运行命令

//清除缓存
php artisan config:clear

php artisan queue:work redis
或者
php artisan queue:work --queue=emailQueue
laravel 基于redis队列插图
laravel 基于redis队列插图1
laravel 基于redis队列插图2

Buy me a cup of coffee 🙂

觉得对你有帮助,就给我打赏吧,谢谢!

微信赞赏码链接,点击跳转:

https://www.owenzhang.com/wechat_reward.png

laravel 基于redis队列插图3