php - 清除 Laravel 5.x/6.x/7.x/8+ 中 Redis 中的 Laravel 排队作业

标签 php laravel redis queue laravel-artisan

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

有时,当您的队列填满开发环境时,您希望清理所有排队的作业以重新开始。

在 8.x 之前,Laravel 没有提供一种简单的方法来执行此操作,并且 Redis 数据库并不是手动执行此任务的最直观的方法。

最佳答案

Laravel 8+ 使用以下命令可以轻松实现:

php artisan queue:clear redis --queue=queue_name

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

对于 laravel <8 我创建了这个特定于 redis 的 artisan 命令:

<?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 文件中添加以下命令:

protected $commands = [
    'App\Console\Commands\QueueClear'
];

然后,根据您的队列,您可以这样调用它:

默认队列

php artisan queue:clear

特定队列

php artisan queue:clear --queue=queue_name

关于php - 清除 Laravel 5.x/6.x/7.x/8+ 中 Redis 中的 Laravel 排队作业,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69838081/

相关文章:

php - Ubuntu 12.04 PHP 5.5 和 apache2 2.4

java - 在注册页面中,即使是空格也会被注册,mysql数据库-php- android

javascript - JS 滚动事件未启动

php - SQL 字段类型字段不显示

php - Laravel Voyager 菜单未更新

amazon-web-services - Cloud Redis 延迟原因(与 macbook pro 上的本地 redis 相比)

node.js - Lua从Nodejs获取参数

php - 从数据库中删除打印结果的最佳方法

php - PayPal REST API Payment::get 不返回电话

ruby-on-rails - 将 Redis 用作 Rails 的数据存储时应应用哪种设计模式