Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[9.x] Add ability to prune cancelled job batches #45034

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.");
}
}
}