From a346d35cf0815808123e8f4091c3c0871b7c5f64 Mon Sep 17 00:00:00 2001 From: Bozhidar Date: Wed, 24 Apr 2024 21:03:08 +0300 Subject: [PATCH] update --- web/app/Console/Commands/RunBackup.php | 20 ++++++++++++--- web/app/Filament/Resources/BackupResource.php | 12 +++++++-- web/app/Models/Backup.php | 25 +++++++++++++++++++ ...2024_04_24_144158_create_backups_table.php | 1 + 4 files changed, 52 insertions(+), 6 deletions(-) diff --git a/web/app/Console/Commands/RunBackup.php b/web/app/Console/Commands/RunBackup.php index 7d90b182..b3f839de 100644 --- a/web/app/Console/Commands/RunBackup.php +++ b/web/app/Console/Commands/RunBackup.php @@ -5,6 +5,8 @@ use App\Models\Backup; use App\Models\HostingSubscription; use Illuminate\Console\Command; +use Illuminate\Database\Schema\Blueprint; +use Illuminate\Support\Facades\Schema; class RunBackup extends Command { @@ -33,10 +35,20 @@ public function handle() $backup->delete(); } - $getBackups = Backup::where('backup_type', 'hosting_subscription')->get(); - if ($getBackups->count() > 0) { - foreach ($getBackups as $backup) { - $status = $backup->startBackup(); + // Check for pending backups + $getPendingBackups = Backup::where('status', 'pending') + ->get(); + if ($getPendingBackups->count() > 0) { + foreach ($getPendingBackups as $pendingBackup) { + $pendingBackup->startBackup(); + } + } + + // Check for running backups + $getRunningBackups = Backup::where('status', 'running')->get(); + if ($getRunningBackups->count() > 0) { + foreach ($getRunningBackups as $runningBackup) { + $runningBackup->checkBackup(); } } diff --git a/web/app/Filament/Resources/BackupResource.php b/web/app/Filament/Resources/BackupResource.php index 6d1d88b7..cf30f52d 100644 --- a/web/app/Filament/Resources/BackupResource.php +++ b/web/app/Filament/Resources/BackupResource.php @@ -65,13 +65,21 @@ public static function table(Table $table): Table ->columns([ Tables\Columns\TextColumn::make('backup_type'), Tables\Columns\TextColumn::make('backupRelated'), - Tables\Columns\TextColumn::make('status'), + Tables\Columns\BadgeColumn::make('status') + ->badge() + ->color(fn (string $state): string => match ($state) { + 'pending' => 'gray', + 'running' => 'primary', + 'completed' => 'success', + 'failed' => 'danger', + default => 'gray', + }), ]) ->filters([ // ]) ->actions([ - Tables\Actions\EditAction::make(), + Tables\Actions\ViewAction::make(), ]) ->defaultSort('id', 'desc') ->bulkActions([ diff --git a/web/app/Models/Backup.php b/web/app/Models/Backup.php index f7905954..a0b88c43 100644 --- a/web/app/Models/Backup.php +++ b/web/app/Models/Backup.php @@ -20,6 +20,16 @@ class Backup extends Model 'disk', ]; + public static function boot() + { + parent::boot(); + + static::creating(function ($model) { + $model->status = 'pending'; + }); + + } + protected function backupRelated() : Attribute { $relatedWith = $this->backup_type; @@ -35,6 +45,19 @@ protected function backupRelated() : Attribute ); } + public function checkBackup() + { + if ($this->status == 'running') { + $backupDoneFile = $this->path.'/backup.done'; + if (file_exists($backupDoneFile)) { + $this->status = 'completed'; + $this->completed = true; + $this->completed_at = now(); + $this->save(); + } + } + } + public function startBackup() { @@ -80,6 +103,8 @@ public function startBackup() $shellFileContent .= 'touch ' . $backupPath. '/backup.done' . PHP_EOL; $shellFileContent .= 'rm -rf ' . $backupTempScript; + $this->path = $backupPath; + $this->filepath = $backupFilePath; $this->status = 'running'; $this->queued = true; $this->queued_at = now(); diff --git a/web/database/migrations/2024_04_24_144158_create_backups_table.php b/web/database/migrations/2024_04_24_144158_create_backups_table.php index 87cdc65d..e5bafc62 100644 --- a/web/database/migrations/2024_04_24_144158_create_backups_table.php +++ b/web/database/migrations/2024_04_24_144158_create_backups_table.php @@ -17,6 +17,7 @@ public function up(): void $table->string('backup_type')->nullable(); $table->string('status')->nullable(); $table->string('path')->nullable(); + $table->string('filepath')->nullable(); $table->string('size')->nullable(); $table->string('disk')->nullable(); $table->longText('settings')->nullable();