From b77f0ad0ffc59b01f5dc9d62d3bc8098cfe5d3a5 Mon Sep 17 00:00:00 2001 From: Bozhidar Date: Tue, 7 May 2024 16:40:06 +0300 Subject: [PATCH] update --- ...ateDailyFullHostingSubscriptionsBackup.php | 4 +- .../RunHostingSubscriptionsBackupChecks.php | 65 ------------------- ...p => ProcessHostingSubscriptionBackup.php} | 17 ++--- web/app/Models/HostingSubscriptionBackup.php | 14 +--- 4 files changed, 10 insertions(+), 90 deletions(-) delete mode 100644 web/app/Console/Commands/RunHostingSubscriptionsBackupChecks.php rename web/app/Jobs/{CreateHostingSubscriptionBackup.php => ProcessHostingSubscriptionBackup.php} (72%) diff --git a/web/app/Console/Commands/CreateDailyFullHostingSubscriptionsBackup.php b/web/app/Console/Commands/CreateDailyFullHostingSubscriptionsBackup.php index 2b699ba..d2260e7 100644 --- a/web/app/Console/Commands/CreateDailyFullHostingSubscriptionsBackup.php +++ b/web/app/Console/Commands/CreateDailyFullHostingSubscriptionsBackup.php @@ -2,7 +2,7 @@ namespace App\Console\Commands; -use App\Jobs\CreateHostingSubscriptionBackup; +use App\Jobs\ProcessHostingSubscriptionBackup; use App\Models\Backup; use App\Models\HostingSubscription; use App\Models\HostingSubscriptionBackup; @@ -46,7 +46,7 @@ class CreateDailyFullHostingSubscriptionsBackup extends Command ->where('created_at', '>=', Carbon::now()->subHours(24)) ->first(); if (! $findBackup) { - CreateHostingSubscriptionBackup::dispatch($hostingSubscription->id); + ProcessHostingSubscriptionBackup::dispatch($hostingSubscription->id); } else { $this->error('Backup already exists for ' . $hostingSubscription->domain); $this->error('Created before: ' . $findBackup->created_at->diffForHumans()); diff --git a/web/app/Console/Commands/RunHostingSubscriptionsBackupChecks.php b/web/app/Console/Commands/RunHostingSubscriptionsBackupChecks.php deleted file mode 100644 index 40ac3aa..0000000 --- a/web/app/Console/Commands/RunHostingSubscriptionsBackupChecks.php +++ /dev/null @@ -1,65 +0,0 @@ -subDays(7))->get(); - foreach ($findBackupsForDeleting as $backup) { - $backup->delete(); - } - - // Check for pending backups - $getPendingBackups = HostingSubscriptionBackup::where('status', 'pending') - ->get(); - - if ($getPendingBackups->count() > 0) { - if ($getPendingBackups->count() > 1) { - $this->info('Multiple backups are pending...'); - } else { - foreach ($getPendingBackups as $pendingBackup) { - $pendingBackup->startBackup(); - $this->info('Backup started.. '); - } - } - } - - // Check for processing backups - $getRunningBackups = HostingSubscriptionBackup::where('status', 'processing')->get(); - if ($getRunningBackups->count() > 0) { - foreach ($getRunningBackups as $runningBackup) { - $runningBackup->checkBackup(); - $this->info('Checking backup status...'); - } - } - - } -} diff --git a/web/app/Jobs/CreateHostingSubscriptionBackup.php b/web/app/Jobs/ProcessHostingSubscriptionBackup.php similarity index 72% rename from web/app/Jobs/CreateHostingSubscriptionBackup.php rename to web/app/Jobs/ProcessHostingSubscriptionBackup.php index 7e0d8c3..4a646ba 100644 --- a/web/app/Jobs/CreateHostingSubscriptionBackup.php +++ b/web/app/Jobs/ProcessHostingSubscriptionBackup.php @@ -11,18 +11,18 @@ use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\SerializesModels; -class CreateHostingSubscriptionBackup implements ShouldQueue +class ProcessHostingSubscriptionBackup implements ShouldQueue { use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; - protected $hostingSubscriptionId; + protected $id; /** * Create a new job instance. */ - public function __construct($hostingSubscriptionId) + public function __construct($id) { - $this->hostingSubscriptionId = $hostingSubscriptionId; + $this->id = $id; } /** @@ -30,15 +30,10 @@ class CreateHostingSubscriptionBackup implements ShouldQueue */ public function handle(): void { - echo "Backup hosting subscription with ID: {$this->hostingSubscriptionId}\n"; - - $backup = new HostingSubscriptionBackup(); - $backup->hosting_subscription_id = $this->hostingSubscriptionId; - $backup->backup_type = 'full'; - $backup->save(); + echo "Backup hosting subscription backup with ID: {$this->id}\n"; $backupDone = false; - $findHostingSubscriptionBackup = HostingSubscriptionBackup::where('id', $backup->id)->first(); + $findHostingSubscriptionBackup = HostingSubscriptionBackup::where('id', $this->id)->first(); if ($findHostingSubscriptionBackup) { for ($i = 0; $i < 200; $i++) { diff --git a/web/app/Models/HostingSubscriptionBackup.php b/web/app/Models/HostingSubscriptionBackup.php index cc7da9c..c2fbf5c 100644 --- a/web/app/Models/HostingSubscriptionBackup.php +++ b/web/app/Models/HostingSubscriptionBackup.php @@ -5,6 +5,7 @@ namespace App\Models; use App\BackupStorage; use App\Filament\Enums\BackupStatus; use App\Helpers; +use App\Jobs\ProcessHostingSubscriptionBackup; use App\ShellApi; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Casts\Attribute; @@ -55,7 +56,7 @@ class HostingSubscriptionBackup extends Model }); static::created(function ($model) { - $model->startBackup(); + ProcessHostingSubscriptionBackup::dispatch($model->id); }); static::deleting(function ($model) { @@ -67,17 +68,6 @@ class HostingSubscriptionBackup extends Model public function checkCronJob() { - $cronJobCommand = 'phyre-php /usr/local/phyre/web/artisan phyre:run-hosting-subscriptions-backup-checks'; - $findCronJob = CronJob::where('command', $cronJobCommand)->first(); - - if (! $findCronJob) { - $cronJob = new CronJob(); - $cronJob->schedule = '*/5 * * * *'; - $cronJob->command = $cronJobCommand; - $cronJob->user = 'root'; - $cronJob->save(); - } - $cronJobCommand = 'phyre-php /usr/local/phyre/web/artisan phyre:create-daily-full-hosting-subscriptions-backup'; $findCronJob = CronJob::where('command', $cronJobCommand)->first(); if (! $findCronJob) {