Files

44 lines
1.5 KiB
PHP
Raw Permalink Normal View History

<?php
2026-10-05 18:10:18 -04:00
declare(strict_types=1);
namespace Pterodactyl\Console\Commands\Maintenance;
use Carbon\CarbonImmutable;
2026-10-05 18:10:18 -04:00
use Illuminate\Console\Attributes\Description;
use Illuminate\Console\Attributes\Signature;
use Illuminate\Console\Command;
2026-10-05 18:10:18 -04:00
use InvalidArgumentException;
use Pterodactyl\Models\Backup;
2026-10-05 18:10:18 -04:00
#[Description('Marks all backups older than "n" minutes that have not yet completed as being failed.')]
#[Signature('p:maintenance:prune-backups {--prune-age=}')]
class PruneOrphanedBackupsCommand extends Command
{
2026-10-05 18:10:18 -04:00
public function handle(): void
{
2021-01-23 14:12:15 -08:00
$since = $this->option('prune-age') ?? config('backups.prune_age', 360);
2026-10-05 18:10:18 -04:00
$minutes = filter_var($since, FILTER_VALIDATE_INT, ['options' => ['min_range' => 1]]);
throw_if($minutes === false, InvalidArgumentException::class, 'The "--prune-age" argument must be a value greater than 0.');
2026-10-05 18:10:18 -04:00
$query = Backup::query()
->whereNull('completed_at')
2026-10-05 18:10:18 -04:00
->where('created_at', '<=', CarbonImmutable::now()->subMinutes($minutes)->toDateTimeString());
$count = $query->count();
2026-10-05 18:10:18 -04:00
if (! $count) {
$this->info('There are no orphaned backups to be marked as failed.');
return;
}
2026-10-05 18:10:18 -04:00
$this->warn("Marking $count uncompleted backups that are older than $minutes minutes as failed.");
$query->update([
'is_successful' => false,
'completed_at' => CarbonImmutable::now(),
'updated_at' => CarbonImmutable::now(),
]);
}
}