Path: blob/1.0-develop/app/Console/Commands/Maintenance/PruneOrphanedBackupsCommand.php
7461 views
<?php12namespace Pterodactyl\Console\Commands\Maintenance;34use Carbon\CarbonImmutable;5use Illuminate\Console\Command;6use Pterodactyl\Repositories\Eloquent\BackupRepository;78class PruneOrphanedBackupsCommand extends Command9{10protected $signature = 'p:maintenance:prune-backups {--prune-age=}';1112protected $description = 'Marks all backups older than "n" minutes that have not yet completed as being failed.';1314/**15* PruneOrphanedBackupsCommand constructor.16*/17public function __construct(private BackupRepository $backupRepository)18{19parent::__construct();20}2122public function handle()23{24$since = $this->option('prune-age') ?? config('backups.prune_age', 360);25if (!$since || !is_digit($since)) {26throw new \InvalidArgumentException('The "--prune-age" argument must be a value greater than 0.');27}2829$query = $this->backupRepository->getBuilder()30->whereNull('completed_at')31->where('created_at', '<=', CarbonImmutable::now()->subMinutes($since)->toDateTimeString());3233$count = $query->count();34if (!$count) {35$this->info('There are no orphaned backups to be marked as failed.');3637return;38}3940$this->warn("Marking $count uncompleted backups that are older than $since minutes as failed.");4142$query->update([43'is_successful' => false,44'completed_at' => CarbonImmutable::now(),45'updated_at' => CarbonImmutable::now(),46]);47}48}495051