首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为laravel 5.x/6.x/7.x/8+清除Redis中的Laravel排队作业

为laravel 5.x/6.x/7.x/8+清除Redis中的Laravel排队作业
EN

Stack Overflow用户
提问于 2021-11-04 10:52:31
回答 1查看 422关注 0票数 2

如何在Laravel 8之前的Laravel版本中为给定队列清空redis数据库中的所有排队作业?

有时,当您的队列正在填充dev环境时,您希望清理所有排队的作业,以便重新开始。

在8.x之前,Laravel并没有提供一种简单的方法来完成这个任务,Redis数据库不是手动完成这个任务的最直观的方法。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-11-04 10:52:31

Laravel 8+使用以下命令简化了操作:

代码语言:javascript
复制
php artisan queue:clear redis --queue=queue_name

其中队列名是要清除的特定队列的名称。默认队列称为default

对于laravel <8,我创建了这个针对redis的命令:

代码语言:javascript
复制
<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Queue;
use Illuminate\Support\Facades\Redis;

class QueueClear extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'queue:clear {--queue=}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Clear all jobs on a given queue in the redis database';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $queueName = $this->option('queue') ? $this->option('queue') : 'default';
        $queueSize = Queue::size($queueName);
        $this->warn('Removing ' . $queueSize . ' jobs from the ' . $queueName . ' queue...');
        Redis::connection()->del([
            'queues:' . $queueName,
            'queues:' . $queueName . ':notify',
            'queues:' . $queueName . ':delayed',
            'queues:' . $queueName . ':reserved'

        ]);
        $this->info($queueSize . ' jobs removed from the ' . $queueName . ' queue...');
    }
}

app/Console/Commands/Kernel.php文件中添加折叠命令:

代码语言:javascript
复制
protected $commands = [
    'App\Console\Commands\QueueClear'
];

然后,根据队列的不同,您可以这样称呼它:

默认队列

代码语言:javascript
复制
php artisan queue:clear

专用队列

代码语言:javascript
复制
php artisan queue:clear --queue=queue_name
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69838081

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档