2020-08-21 03:48:08 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Console\Commands\Maintenance;
|
|
|
|
|
|
|
|
use Carbon\CarbonImmutable;
|
|
|
|
use InvalidArgumentException;
|
|
|
|
use Illuminate\Console\Command;
|
|
|
|
use Pterodactyl\Repositories\Eloquent\BackupRepository;
|
|
|
|
|
|
|
|
class PruneOrphanedBackupsCommand extends Command
|
|
|
|
{
|
2021-01-23 22:12:15 +00:00
|
|
|
protected $signature = 'p:maintenance:prune-backups {--prune-age=}';
|
2020-08-21 03:48:08 +00:00
|
|
|
|
2022-10-14 16:59:20 +00:00
|
|
|
protected $description = 'Marks all backups that have not completed in the last "n" minutes as being failed.';
|
|
|
|
|
2020-08-21 03:48:08 +00:00
|
|
|
/**
|
2022-10-14 16:59:20 +00:00
|
|
|
* PruneOrphanedBackupsCommand constructor.
|
2020-08-21 03:48:08 +00:00
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
public function __construct(private BackupRepository $backupRepository)
|
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
}
|
2020-08-21 03:48:08 +00:00
|
|
|
|
2022-10-14 16:59:20 +00:00
|
|
|
public function handle()
|
2020-08-21 03:48:08 +00:00
|
|
|
{
|
2021-01-23 22:12:15 +00:00
|
|
|
$since = $this->option('prune-age') ?? config('backups.prune_age', 360);
|
|
|
|
if (!$since || !is_digit($since)) {
|
|
|
|
throw new InvalidArgumentException('The "--prune-age" argument must be a value greater than 0.');
|
2020-08-21 03:48:08 +00:00
|
|
|
}
|
|
|
|
|
2022-10-14 16:59:20 +00:00
|
|
|
$query = $this->backupRepository->getBuilder()
|
2020-08-21 03:48:08 +00:00
|
|
|
->whereNull('completed_at')
|
2021-05-27 22:33:43 +00:00
|
|
|
->where('created_at', '<=', CarbonImmutable::now()->subMinutes($since)->toDateTimeString());
|
2020-08-21 03:48:08 +00:00
|
|
|
|
|
|
|
$count = $query->count();
|
2021-01-23 20:33:34 +00:00
|
|
|
if (!$count) {
|
2020-08-21 04:07:53 +00:00
|
|
|
$this->info('There are no orphaned backups to be marked as failed.');
|
2020-08-21 03:48:08 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-10-14 16:59:20 +00:00
|
|
|
$this->warn("Marking $count backups that have not been marked as completed in the last $since minutes as failed.");
|
2020-08-21 03:48:08 +00:00
|
|
|
|
2020-08-21 04:07:53 +00:00
|
|
|
$query->update([
|
|
|
|
'is_successful' => false,
|
|
|
|
'completed_at' => CarbonImmutable::now(),
|
|
|
|
'updated_at' => CarbonImmutable::now(),
|
|
|
|
]);
|
2020-08-21 03:48:08 +00:00
|
|
|
}
|
|
|
|
}
|