From 932211ec95a46f1083ee957d47b39e9b87a1fbc6 Mon Sep 17 00:00:00 2001 From: Asem Alalami Date: Thu, 3 Oct 2024 11:39:16 +0300 Subject: [PATCH] fixed the purge command --- .../Commands/PurgeOldMonitorsCommand.php | 36 ++++++++++++------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/src/Console/Commands/PurgeOldMonitorsCommand.php b/src/Console/Commands/PurgeOldMonitorsCommand.php index 3d358da..bd2d1e5 100644 --- a/src/Console/Commands/PurgeOldMonitorsCommand.php +++ b/src/Console/Commands/PurgeOldMonitorsCommand.php @@ -13,7 +13,7 @@ class PurgeOldMonitorsCommand extends Command { use HandlesDateInputs; - protected $signature = 'queue-monitor:purge {--before=} {--beforeDays=} {--beforeInterval=} {--only-succeeded} {--queue=} {--dry}'; + protected $signature = 'queue-monitor:purge {--before=} {--beforeDays=} {--beforeInterval=} {--only-succeeded} {--queue=} {--dry} {--chunk}'; public function handle(): int { @@ -26,7 +26,7 @@ public function handle(): int $query = QueueMonitor::getModel() ->newQuery() - ->where('started_at', '<', $beforeDate); + ->where('queued_at', '<', $beforeDate); $queues = array_filter(explode(',', $this->option('queue') ?? '')); @@ -44,19 +44,29 @@ public function handle(): int sprintf('Purging %d jobs before %s.', $count, $beforeDate->format('Y-m-d H:i:s')) ); - $query->chunk(200, function (Collection $models, int $page) use ($count) { - $this->info( - sprintf('Deleted chunk %d / %d', $page, abs($count / 200)) - ); - - if ($this->option('dry')) { - return; + if ($this->option('chunk')) { + $query->chunkById(200, function (Collection $models, int $page) use ($count) { + $this->info( + sprintf('Deleted chunk %d / %d', $page, ceil($count / 200)) + ); + + if ($this->option('dry')) { + return; + } + + DB::table(QueueMonitor::getModel()->getTable()) + ->whereIn('id', $models->pluck('id')) + ->delete(); + }); + } else { + if (!$this->option('dry')) { + $query->delete(); } - DB::table(QueueMonitor::getModel()->getTable()) - ->whereIn('id', $models->pluck('id')) - ->delete(); - }); + $this->info( + sprintf('Deleted %d jobs before %s.', $count, $beforeDate->format('Y-m-d H:i:s')) + ); + } return 1; }