Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/app/Console/Commands/Maintenance/PruneOrphanedBackupsCommand.php
7461 views
1
<?php
2
3
namespace Pterodactyl\Console\Commands\Maintenance;
4
5
use Carbon\CarbonImmutable;
6
use Illuminate\Console\Command;
7
use Pterodactyl\Repositories\Eloquent\BackupRepository;
8
9
class PruneOrphanedBackupsCommand extends Command
10
{
11
protected $signature = 'p:maintenance:prune-backups {--prune-age=}';
12
13
protected $description = 'Marks all backups older than "n" minutes that have not yet completed as being failed.';
14
15
/**
16
* PruneOrphanedBackupsCommand constructor.
17
*/
18
public function __construct(private BackupRepository $backupRepository)
19
{
20
parent::__construct();
21
}
22
23
public function handle()
24
{
25
$since = $this->option('prune-age') ?? config('backups.prune_age', 360);
26
if (!$since || !is_digit($since)) {
27
throw new \InvalidArgumentException('The "--prune-age" argument must be a value greater than 0.');
28
}
29
30
$query = $this->backupRepository->getBuilder()
31
->whereNull('completed_at')
32
->where('created_at', '<=', CarbonImmutable::now()->subMinutes($since)->toDateTimeString());
33
34
$count = $query->count();
35
if (!$count) {
36
$this->info('There are no orphaned backups to be marked as failed.');
37
38
return;
39
}
40
41
$this->warn("Marking $count uncompleted backups that are older than $since minutes as failed.");
42
43
$query->update([
44
'is_successful' => false,
45
'completed_at' => CarbonImmutable::now(),
46
'updated_at' => CarbonImmutable::now(),
47
]);
48
}
49
}
50
51