|
1 | 1 | <?php |
2 | | -/** |
| 2 | +/* |
3 | 3 | * Pterodactyl - Panel |
4 | 4 | * Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>. |
5 | 5 | * |
|
22 | 22 | * SOFTWARE. |
23 | 23 | */ |
24 | 24 |
|
25 | | -namespace Pterodactyl\Console\Commands; |
| 25 | +namespace Pterodactyl\Console\Commands\Maintenance; |
26 | 26 |
|
27 | | -use DB; |
| 27 | +use Carbon\Carbon; |
28 | 28 | use Illuminate\Console\Command; |
| 29 | +use Illuminate\Contracts\Filesystem\Factory as FilesystemFactory; |
29 | 30 |
|
30 | | -class ClearServices extends Command |
| 31 | +class CleanServiceBackupFilesCommand extends Command |
31 | 32 | { |
32 | 33 | /** |
33 | | - * The name and signature of the console command. |
34 | | - * |
| 34 | + * @var \Carbon\Carbon |
| 35 | + */ |
| 36 | + protected $carbon; |
| 37 | + |
| 38 | + /** |
35 | 39 | * @var string |
36 | 40 | */ |
37 | | - protected $signature = 'pterodactyl:clear-services'; |
| 41 | + protected $description = 'Clean orphaned .bak files created when modifying services.'; |
| 42 | + |
| 43 | + /** |
| 44 | + * @var \Illuminate\Contracts\Filesystem\Filesystem |
| 45 | + */ |
| 46 | + protected $disk; |
38 | 47 |
|
39 | 48 | /** |
40 | | - * The console command description. |
41 | | - * |
42 | 49 | * @var string |
43 | 50 | */ |
44 | | - protected $description = 'Removes all services from the database for installing updated ones as needed.'; |
| 51 | + protected $signature = 'p:maintenance:clean-service-backups'; |
45 | 52 |
|
46 | 53 | /** |
47 | | - * Create a new command instance. |
| 54 | + * CleanServiceBackupFilesCommand constructor. |
| 55 | + * |
| 56 | + * @param \Carbon\Carbon $carbon |
| 57 | + * @param \Illuminate\Contracts\Filesystem\Factory $filesystem |
48 | 58 | */ |
49 | | - public function __construct() |
| 59 | + public function __construct(Carbon $carbon, FilesystemFactory $filesystem) |
50 | 60 | { |
51 | 61 | parent::__construct(); |
| 62 | + |
| 63 | + $this->carbon = $carbon; |
| 64 | + $this->disk = $filesystem->disk(); |
52 | 65 | } |
53 | 66 |
|
54 | 67 | /** |
55 | | - * Execute the console command. |
56 | | - * |
57 | | - * @return mixed |
| 68 | + * Handle command execution. |
58 | 69 | */ |
59 | 70 | public function handle() |
60 | 71 | { |
61 | | - if (! $this->confirm('This is a destructive operation, are you sure you wish to continue?')) { |
62 | | - $this->error('Canceling.'); |
63 | | - exit(); |
64 | | - } |
65 | | - |
66 | | - $bar = $this->output->createProgressBar(3); |
67 | | - DB::beginTransaction(); |
68 | | - |
69 | | - try { |
70 | | - DB::table('services')->truncate(); |
71 | | - $bar->advance(); |
72 | | - |
73 | | - DB::table('service_options')->truncate(); |
74 | | - $bar->advance(); |
75 | | - |
76 | | - DB::table('service_variables')->truncate(); |
77 | | - $bar->advance(); |
78 | | - |
79 | | - DB::commit(); |
80 | | - } catch (\Exception $ex) { |
81 | | - DB::rollBack(); |
82 | | - } |
| 72 | + $files = $this->disk->files('services/.bak'); |
83 | 73 |
|
84 | | - $this->info("\n"); |
85 | | - $this->info('All services have been removed. Consider running `php artisan pterodactyl:service-defaults` at this time.'); |
| 74 | + collect($files)->each(function ($file) { |
| 75 | + $lastModified = $this->carbon->timestamp($this->disk->lastModified($file)); |
| 76 | + if ($lastModified->diffInMinutes($this->carbon->now()) > 5) { |
| 77 | + $this->disk->delete($file); |
| 78 | + $this->info(trans('command/messages.maintenance.deleting_service_backup', ['file' => $file])); |
| 79 | + } |
| 80 | + }); |
86 | 81 | } |
87 | 82 | } |
0 commit comments