Skip to content

Commit 7add440

Browse files
authored
[9.x] Add ability to prune cancelled job batches (#45034)
1 parent e5a0ee6 commit 7add440

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

src/Illuminate/Bus/DatabaseBatchRepository.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,29 @@ public function pruneUnfinished(DateTimeInterface $before)
277277
return $totalDeleted;
278278
}
279279

280+
/**
281+
* Prune all of the cancelled entries older than the given date.
282+
*
283+
* @param \DateTimeInterface $before
284+
* @return int
285+
*/
286+
public function pruneCancelled(DateTimeInterface $before)
287+
{
288+
$query = $this->connection->table($this->table)
289+
->whereNotNull('cancelled_at')
290+
->where('created_at', '<', $before->getTimestamp());
291+
292+
$totalDeleted = 0;
293+
294+
do {
295+
$deleted = $query->take(1000)->delete();
296+
297+
$totalDeleted += $deleted;
298+
} while ($deleted !== 0);
299+
300+
return $totalDeleted;
301+
}
302+
280303
/**
281304
* Execute the given Closure within a storage specific transaction.
282305
*

src/Illuminate/Queue/Console/PruneBatchesCommand.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ class PruneBatchesCommand extends Command
1919
*/
2020
protected $signature = 'queue:prune-batches
2121
{--hours=24 : The number of hours to retain batch data}
22-
{--unfinished= : The number of hours to retain unfinished batch data }';
22+
{--unfinished= : The number of hours to retain unfinished batch data }
23+
{--cancelled= : The number of hours to retain cancelled batch data }';
2324

2425
/**
2526
* The name of the console command.
@@ -65,5 +66,15 @@ public function handle()
6566

6667
$this->components->info("{$count} unfinished entries deleted.");
6768
}
69+
70+
if ($this->option('cancelled')) {
71+
$count = 0;
72+
73+
if ($repository instanceof DatabaseBatchRepository) {
74+
$count = $repository->pruneCancelled(Carbon::now()->subHours($this->option('cancelled')));
75+
}
76+
77+
$this->components->info("{$count} cancelled entries deleted.");
78+
}
6879
}
6980
}

0 commit comments

Comments
 (0)