master

laravel/framework

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

PruneFailedJobsCommand.php

TLDR

This file defines the PruneFailedJobsCommand class, which is a console command for pruning stale entries from the failed jobs table.

Methods

handle

This method is executed when the console command is run. It retrieves the failed job storage driver and checks if it supports pruning. If it does, it prunes the stale entries from the failed jobs table by calling the prune() method on the failer object. The number of hours to retain failed jobs data is determined by the --hours option passed to the command. After pruning, it displays the number of entries deleted.

Classes

PruneFailedJobsCommand

This class extends the Command class and represents a console command for pruning stale entries from the failed jobs table. It has a $signature property that defines the command name and options, and a $description property that provides a brief description of the command.

<?php

namespace Illuminate\Queue\Console;

use Illuminate\Console\Command;
use Illuminate\Queue\Failed\PrunableFailedJobProvider;
use Illuminate\Support\Carbon;
use Symfony\Component\Console\Attribute\AsCommand;

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

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Prune stale entries from the failed jobs table';

    /**
     * Execute the console command.
     *
     * @return int|null
     */
    public function handle()
    {
        $failer = $this->laravel['queue.failer'];

        if ($failer instanceof PrunableFailedJobProvider) {
            $count = $failer->prune(Carbon::now()->subHours($this->option('hours')));
        } else {
            $this->components->error('The ['.class_basename($failer).'] failed job storage driver does not support pruning.');

            return 1;
        }

        $this->components->info("{$count} entries deleted.");
    }
}