master

laravel/framework

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

PruneBatchesCommand.php

TLDR

This file contains the PruneBatchesCommand class, which is a command for pruning stale entries from the batches database. It allows users to specify the number of hours to retain batch data, unfinished batch data, and cancelled batch data.

Methods

There are no additional methods in this file.

Classes

PruneBatchesCommand

This class is a command for pruning stale entries from the batches database. It extends the Illuminate\Console\Command class and is used to create the queue:prune-batches command.

The class has the following attributes:

  • $signature: The console command signature, which specifies the command's name and options.
  • $description: The console command description.
  • $repository: The batch repository instance.

The class has the following method:

  • handle(): This method contains the logic for executing the console command. It retrieves the batch repository instance from the Laravel container and uses it to prune stale entries from the batches database. The method also prunes unfinished and cancelled entries if the respective options were provided. The number of deleted entries is displayed as output.
<?php

namespace Illuminate\Queue\Console;

use Illuminate\Bus\BatchRepository;
use Illuminate\Bus\DatabaseBatchRepository;
use Illuminate\Bus\PrunableBatchRepository;
use Illuminate\Console\Command;
use Illuminate\Support\Carbon;
use Symfony\Component\Console\Attribute\AsCommand;

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

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

    /**
     * Execute the console command.
     *
     * @return void
     */
    public function handle()
    {
        $repository = $this->laravel[BatchRepository::class];

        $count = 0;

        if ($repository instanceof PrunableBatchRepository) {
            $count = $repository->prune(Carbon::now()->subHours($this->option('hours')));
        }

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

        if ($this->option('unfinished') !== null) {
            $count = 0;

            if ($repository instanceof DatabaseBatchRepository) {
                $count = $repository->pruneUnfinished(Carbon::now()->subHours($this->option('unfinished')));
            }

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

        if ($this->option('cancelled') !== null) {
            $count = 0;

            if ($repository instanceof DatabaseBatchRepository) {
                $count = $repository->pruneCancelled(Carbon::now()->subHours($this->option('cancelled')));
            }

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