Skip to content

Commit

Permalink
[9.x] Add ability to prune cancelled job batches (#45034)
Browse files Browse the repository at this point in the history
  • Loading branch information
dammy001 authored Nov 21, 2022
1 parent e5a0ee6 commit 7add440
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
23 changes: 23 additions & 0 deletions src/Illuminate/Bus/DatabaseBatchRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,29 @@ public function pruneUnfinished(DateTimeInterface $before)
return $totalDeleted;
}

/**
* Prune all of the cancelled entries older than the given date.
*
* @param \DateTimeInterface $before
* @return int
*/
public function pruneCancelled(DateTimeInterface $before)
{
$query = $this->connection->table($this->table)
->whereNotNull('cancelled_at')
->where('created_at', '<', $before->getTimestamp());

$totalDeleted = 0;

do {
$deleted = $query->take(1000)->delete();

$totalDeleted += $deleted;
} while ($deleted !== 0);

return $totalDeleted;
}

/**
* Execute the given Closure within a storage specific transaction.
*
Expand Down
13 changes: 12 additions & 1 deletion src/Illuminate/Queue/Console/PruneBatchesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ class PruneBatchesCommand extends Command
*/
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 }';
{--unfinished= : The number of hours to retain unfinished batch data }
{--cancelled= : The number of hours to retain cancelled batch data }';

/**
* The name of the console command.
Expand Down Expand Up @@ -65,5 +66,15 @@ public function handle()

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

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

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

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

0 comments on commit 7add440

Please sign in to comment.