This commit is contained in:
Bozhidar 2024-05-07 15:42:17 +03:00
parent f81cf44978
commit 4320aa029c
3 changed files with 69 additions and 44 deletions

View file

@ -2,7 +2,7 @@
namespace App\Console\Commands; namespace App\Console\Commands;
use App\Jobs\ProcessHostingSubscriptionBackup; use App\Jobs\CreateHostingSubscriptionBackup;
use App\Models\Backup; use App\Models\Backup;
use App\Models\HostingSubscription; use App\Models\HostingSubscription;
use App\Models\HostingSubscriptionBackup; use App\Models\HostingSubscriptionBackup;
@ -46,9 +46,7 @@ class CreateDailyFullHostingSubscriptionsBackup extends Command
->where('created_at', '>=', Carbon::now()->subHours(24)) ->where('created_at', '>=', Carbon::now()->subHours(24))
->first(); ->first();
if (! $findBackup) { if (! $findBackup) {
CreateHostingSubscriptionBackup::dispatch($hostingSubscription->id);
ProcessHostingSubscriptionBackup::dispatch($hostingSubscription->id);
} else { } else {
$this->error('Backup already exists for ' . $hostingSubscription->domain); $this->error('Backup already exists for ' . $hostingSubscription->domain);
$this->error('Created before: ' . $findBackup->created_at->diffForHumans()); $this->error('Created before: ' . $findBackup->created_at->diffForHumans());

View file

@ -0,0 +1,67 @@
<?php
namespace App\Jobs;
use App\Filament\Enums\BackupStatus;
use App\Models\HostingSubscription;
use App\Models\HostingSubscriptionBackup;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
class CreateHostingSubscriptionBackup implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $hostingSubscriptionId;
/**
* Create a new job instance.
*/
public function __construct($hostingSubscriptionId)
{
$this->hostingSubscriptionId = $hostingSubscriptionId;
}
/**
* Execute the job.
*/
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();
$backupDone = false;
$findHostingSubscriptionBackup = HostingSubscriptionBackup::where('id', $backup->id)->first();
if ($findHostingSubscriptionBackup) {
for ($i = 0; $i < 200; $i++) {
echo "Check: ".$i." | Checking backup status...\n";
$findHostingSubscriptionBackup->checkBackup();
if ($findHostingSubscriptionBackup->status == BackupStatus::Completed) {
echo "Backup completed\n";
$backupDone = true;
break;
}
sleep(4);
}
if (! $backupDone) {
echo "Backup failed\n";
$findHostingSubscriptionBackup->status = BackupStatus::Failed;
$findHostingSubscriptionBackup->save();
}
}
}
}

View file

@ -1,40 +0,0 @@
<?php
namespace App\Jobs;
use App\Models\HostingSubscriptionBackup;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
class ProcessHostingSubscriptionBackup implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $hostingSubscriptionId;
/**
* Create a new job instance.
*/
public function __construct($hostingSubscriptionId)
{
$this->hostingSubscriptionId = $hostingSubscriptionId;
}
/**
* Execute the job.
*/
public function handle(): void
{
echo "Backing up hosting subscription with ID: {$this->hostingSubscriptionId}\n";
sleep(3);
//
// $backup = new HostingSubscriptionBackup();
// $backup->hosting_subscription_id = $this->hostingSubscriptionId;
// $backup->backup_type = 'full';
// $backup->save();
}
}