From f44b37c8bbecfc9cd8a1688580c6e8bf44e298fc Mon Sep 17 00:00:00 2001 From: Dries Vints Date: Mon, 5 Apr 2021 16:27:19 +0200 Subject: [PATCH 1/3] Updated pruning batches --- src/Illuminate/Bus/DatabaseBatchRepository.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Bus/DatabaseBatchRepository.php b/src/Illuminate/Bus/DatabaseBatchRepository.php index 03d7a3dab3cb..b8cc00c1b3cd 100644 --- a/src/Illuminate/Bus/DatabaseBatchRepository.php +++ b/src/Illuminate/Bus/DatabaseBatchRepository.php @@ -241,8 +241,8 @@ public function delete(string $batchId) public function prune(DateTimeInterface $before) { $query = $this->connection->table($this->table) - ->whereNotNull('finished_at') - ->where('finished_at', '<', $before->getTimestamp()); + ->whereNotNull('created_at') + ->where('created_at', '<', $before->getTimestamp()); $totalDeleted = 0; From 4620b298ebd1219f63a696cfb9751a67056f8e75 Mon Sep 17 00:00:00 2001 From: Dries Vints Date: Mon, 5 Apr 2021 17:49:22 +0200 Subject: [PATCH 2/3] Add unfinished option to PruneBatchesCommand --- .../Bus/DatabaseBatchRepository.php | 25 ++++++++++++++++++- .../Queue/Console/PruneBatchesCommand.php | 19 +++++++++++--- 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/src/Illuminate/Bus/DatabaseBatchRepository.php b/src/Illuminate/Bus/DatabaseBatchRepository.php index b8cc00c1b3cd..ee544b04223d 100644 --- a/src/Illuminate/Bus/DatabaseBatchRepository.php +++ b/src/Illuminate/Bus/DatabaseBatchRepository.php @@ -241,7 +241,30 @@ public function delete(string $batchId) public function prune(DateTimeInterface $before) { $query = $this->connection->table($this->table) - ->whereNotNull('created_at') + ->whereNotNull('finished_at') + ->where('finished_at', '<', $before->getTimestamp()); + + $totalDeleted = 0; + + do { + $deleted = $query->take(1000)->delete(); + + $totalDeleted += $deleted; + } while ($deleted !== 0); + + return $totalDeleted; + } + + /** + * Prune all of the unfinished entries older than the given date. + * + * @param \DateTimeInterface $before + * @return int + */ + public function pruneUnfinished(DateTimeInterface $before) + { + $query = $this->connection->table($this->table) + ->whereNull('finished_at') ->where('created_at', '<', $before->getTimestamp()); $totalDeleted = 0; diff --git a/src/Illuminate/Queue/Console/PruneBatchesCommand.php b/src/Illuminate/Queue/Console/PruneBatchesCommand.php index cc25bc53cfb9..dc2819bd1433 100644 --- a/src/Illuminate/Queue/Console/PruneBatchesCommand.php +++ b/src/Illuminate/Queue/Console/PruneBatchesCommand.php @@ -4,6 +4,7 @@ use Carbon\Carbon; use Illuminate\Bus\BatchRepository; +use Illuminate\Bus\DatabaseBatchRepository; use Illuminate\Bus\PrunableBatchRepository; use Illuminate\Console\Command; @@ -14,7 +15,9 @@ class PruneBatchesCommand extends Command * * @var string */ - protected $signature = 'queue:prune-batches {--hours=24 : The number of hours to retain batch data}'; + protected $signature = 'queue:prune-batches + {--hours=24 : The number of hours to retain batch data} + {--unfinished= : Include unfinished batches }'; /** * The console command description. @@ -30,14 +33,24 @@ class PruneBatchesCommand extends Command */ public function handle() { - $count = 0; - $repository = $this->laravel[BatchRepository::class]; + $count = 0; + if ($repository instanceof PrunableBatchRepository) { $count = $repository->prune(Carbon::now()->subHours($this->option('hours'))); } $this->info("{$count} entries deleted!"); + + if ($unfinished = $this->option('unfinished')) { + $count = 0; + + if ($repository instanceof DatabaseBatchRepository) { + $count = $repository->pruneUnfinished(Carbon::now()->subHours($this->option('unfinished'))); + } + + $this->info("{$count} unfinished entries deleted!"); + } } } From e818383df79eacf2c317bb33a9514789c6059562 Mon Sep 17 00:00:00 2001 From: Dries Vints Date: Mon, 5 Apr 2021 17:52:35 +0200 Subject: [PATCH 3/3] Fix description --- src/Illuminate/Queue/Console/PruneBatchesCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Queue/Console/PruneBatchesCommand.php b/src/Illuminate/Queue/Console/PruneBatchesCommand.php index dc2819bd1433..59ea3887b8ab 100644 --- a/src/Illuminate/Queue/Console/PruneBatchesCommand.php +++ b/src/Illuminate/Queue/Console/PruneBatchesCommand.php @@ -17,7 +17,7 @@ class PruneBatchesCommand extends Command */ protected $signature = 'queue:prune-batches {--hours=24 : The number of hours to retain batch data} - {--unfinished= : Include unfinished batches }'; + {--unfinished= : The number of hours to retain unfinished batch data }'; /** * The console command description.