Skip to content

Commit

Permalink
fixed the purge command
Browse files Browse the repository at this point in the history
  • Loading branch information
AsemAlalami authored and romanzipp committed Oct 3, 2024
1 parent 3174447 commit 932211e
Showing 1 changed file with 23 additions and 13 deletions.
36 changes: 23 additions & 13 deletions src/Console/Commands/PurgeOldMonitorsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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') ?? ''));

Expand All @@ -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;
}
Expand Down

0 comments on commit 932211e

Please sign in to comment.