【问题标题】:Clear laravel queued jobs in Redis for Laravel 5.x/6.x/7.x/8+为 Laravel 5.x/6.x/7.x/8+ 清除 Redis 中的 laravel 排队作业
【发布时间】:2021-12-18 14:43:41
【问题描述】:

Laravel 8 之前的 Laravel 版本中,如何清空 redis 数据库中的所有排队作业

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

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

【问题讨论】:

    标签: php laravel redis queue laravel-artisan


    【解决方案1】:

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

    php artisan queue:clear redis --queue=queue_name
    

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

    对于 laravel ,我创建了这个特定于 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_name
    

    【讨论】:

      猜你喜欢
      • 2022-07-25
      • 2023-03-16
      • 2020-11-14
      • 2016-03-24
      • 2021-02-16
      • 2011-09-06
      • 1970-01-01
      • 2021-12-21
      • 2023-03-14
      相关资源
      最近更新 更多