Prune server backups from the DB after 30 minutes if they still have not completed

This commit is contained in:
Dane Everitt 2020-08-20 20:48:08 -07:00
parent f144ba8394
commit 6066fa40f4
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
2 changed files with 56 additions and 0 deletions

View file

@ -0,0 +1,47 @@
<?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
*/
protected $description = 'Removes all backups that have existed for more than "n" minutes which are not marked as completed.';
/**
* @param \Pterodactyl\Repositories\Eloquent\BackupRepository $repository
*/
public function handle(BackupRepository $repository)
{
$since = $this->option('since-minutes');
if (!is_digit($since)) {
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();
if (!$count) {
$this->info('There are no orphaned backups to be removed.');
return;
}
$this->warn("Deleting {$count} backups that have not been marked as completed in the last {$since} minutes.");
$query->delete();
}
}

View file

@ -22,7 +22,16 @@ class Kernel extends ConsoleKernel
*/ */
protected function schedule(Schedule $schedule) protected function schedule(Schedule $schedule)
{ {
// Execute scheduled commands for servers every minute, as if there was a normal cron running.
$schedule->command('p:schedule:process')->everyMinute()->withoutOverlapping(); $schedule->command('p:schedule:process')->everyMinute()->withoutOverlapping();
// Every 30 minutes, run the backup pruning command so that any abandoned backups can be removed
// from the UI view for the server.
$schedule->command('p:maintenance:prune-backups', [
'--since-minutes' => '30',
])->everyThirtyMinutes();
// Every day cleanup any internal backups of service files.
$schedule->command('p:maintenance:clean-service-backups')->daily(); $schedule->command('p:maintenance:clean-service-backups')->daily();
} }
} }