master

laravel/framework

Last updated at: 29/12/2023 09:23

FlushFailedCommand.php

TLDR

This file is a part of the Illuminate\Queue\Console namespace in the Demo Projects project. It contains the FlushFailedCommand class which is used to flush all of the failed queue jobs.

Classes

FlushFailedCommand

The FlushFailedCommand class extends the Command class and is annotated with the #[AsCommand(name: 'queue:flush')] attribute, which sets the command name to "queue:flush". This class is responsible for handling the console command to flush all of the failed queue jobs. It has the following properties and methods:

  • Properties:

    • $signature: The console command signature. It is set to 'queue:flush {--hours= : The number of hours to retain failed job data}'.
    • $description: The console command description. It is set to 'Flush all of the failed queue jobs'.
  • Methods:

    • handle(): This method is called when the console command is executed. It flushes the failed job data using the flush() method of the queue.failer service. If the --hours option is provided, it displays a success message indicating the number of hours and jobs deleted. Otherwise, it displays a general success message.
<?php

namespace Illuminate\Queue\Console;

use Illuminate\Console\Command;
use Symfony\Component\Console\Attribute\AsCommand;

#[AsCommand(name: 'queue:flush')]
class FlushFailedCommand extends Command
{
    /**
     * The console command name.
     *
     * @var string
     */
    protected $signature = 'queue:flush {--hours= : The number of hours to retain failed job data}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Flush all of the failed queue jobs';

    /**
     * Execute the console command.
     *
     * @return void
     */
    public function handle()
    {
        $this->laravel['queue.failer']->flush($this->option('hours'));

        if ($this->option('hours')) {
            $this->components->info("All jobs that failed more than {$this->option('hours')} hours ago have been deleted successfully.");

            return;
        }

        $this->components->info('All failed jobs deleted successfully.');
    }
}