如何在 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/