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
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $signature = 'p:maintenance:prune-backups {--since-minutes=30}';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2020-08-21 04:07:53 +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
|
|
|
|
|
|
|
public function handle(BackupRepository $repository)
|
|
|
|
{
|
|
|
|
$since = $this->option('since-minutes');
|
2021-01-23 20:33:34 +00:00
|
|
|
if (!is_digit($since)) {
|
2020-08-21 03:48:08 +00:00
|
|
|
throw new InvalidArgumentException('The --since-minutes option must be a valid numeric digit.');
|
|
|
|
}
|
|
|
|
|
|
|
|
$query = $repository->getBuilder()
|
|
|
|
->whereNull('completed_at')
|
|
|
|
->whereDate('created_at', '<=', CarbonImmutable::now()->subMinutes($since));
|
|
|
|
|
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
2020-08-21 04:07:53 +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
|
|
|
}
|
|
|
|
}
|