如何在Laravel 8之前的Laravel版本中为给定队列清空redis数据库中的所有排队作业?
有时,当您的队列正在填充dev环境时,您希望清理所有排队的作业,以便重新开始。
在8.x之前,Laravel并没有提供一种简单的方法来完成这个任务,Redis数据库不是手动完成这个任务的最直观的方法。
发布于 2021-11-04 10:52:31
Laravel 8+使用以下命令简化了操作:
php artisan queue:clear redis --queue=queue_name其中队列名是要清除的特定队列的名称。默认队列称为default
对于laravel <8,我创建了这个针对redis的命令:
<?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_namehttps://stackoverflow.com/questions/69838081
复制相似问题