| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
namespace Illuminate\Queue\Console;
|
|
|
4 |
|
|
|
5 |
use Illuminate\Bus\BatchRepository;
|
|
|
6 |
use Illuminate\Bus\DatabaseBatchRepository;
|
|
|
7 |
use Illuminate\Bus\PrunableBatchRepository;
|
|
|
8 |
use Illuminate\Console\Command;
|
|
|
9 |
use Illuminate\Support\Carbon;
|
|
|
10 |
use Symfony\Component\Console\Attribute\AsCommand;
|
|
|
11 |
|
|
|
12 |
#[AsCommand(name: 'queue:prune-batches')]
|
|
|
13 |
class PruneBatchesCommand extends Command
|
|
|
14 |
{
|
|
|
15 |
/**
|
|
|
16 |
* The console command signature.
|
|
|
17 |
*
|
|
|
18 |
* @var string
|
|
|
19 |
*/
|
|
|
20 |
protected $signature = 'queue:prune-batches
|
|
|
21 |
{--hours=24 : The number of hours to retain batch data}
|
|
|
22 |
{--unfinished= : The number of hours to retain unfinished batch data }
|
|
|
23 |
{--cancelled= : The number of hours to retain cancelled batch data }';
|
|
|
24 |
|
|
|
25 |
/**
|
|
|
26 |
* The name of the console command.
|
|
|
27 |
*
|
|
|
28 |
* This name is used to identify the command during lazy loading.
|
|
|
29 |
*
|
|
|
30 |
* @var string|null
|
|
|
31 |
*
|
|
|
32 |
* @deprecated
|
|
|
33 |
*/
|
|
|
34 |
protected static $defaultName = 'queue:prune-batches';
|
|
|
35 |
|
|
|
36 |
/**
|
|
|
37 |
* The console command description.
|
|
|
38 |
*
|
|
|
39 |
* @var string
|
|
|
40 |
*/
|
|
|
41 |
protected $description = 'Prune stale entries from the batches database';
|
|
|
42 |
|
|
|
43 |
/**
|
|
|
44 |
* Execute the console command.
|
|
|
45 |
*
|
|
|
46 |
* @return void
|
|
|
47 |
*/
|
|
|
48 |
public function handle()
|
|
|
49 |
{
|
|
|
50 |
$repository = $this->laravel[BatchRepository::class];
|
|
|
51 |
|
|
|
52 |
$count = 0;
|
|
|
53 |
|
|
|
54 |
if ($repository instanceof PrunableBatchRepository) {
|
|
|
55 |
$count = $repository->prune(Carbon::now()->subHours($this->option('hours')));
|
|
|
56 |
}
|
|
|
57 |
|
|
|
58 |
$this->components->info("{$count} entries deleted.");
|
|
|
59 |
|
|
|
60 |
if ($this->option('unfinished')) {
|
|
|
61 |
$count = 0;
|
|
|
62 |
|
|
|
63 |
if ($repository instanceof DatabaseBatchRepository) {
|
|
|
64 |
$count = $repository->pruneUnfinished(Carbon::now()->subHours($this->option('unfinished')));
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
$this->components->info("{$count} unfinished entries deleted.");
|
|
|
68 |
}
|
|
|
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 |
}
|
|
|
79 |
}
|
|
|
80 |
}
|