This commit is contained in:
Bozhidar 2024-05-07 16:40:06 +03:00
parent 4320aa029c
commit b77f0ad0ff
4 changed files with 10 additions and 90 deletions

View file

@ -2,7 +2,7 @@
namespace App\Console\Commands; namespace App\Console\Commands;
use App\Jobs\CreateHostingSubscriptionBackup; use App\Jobs\ProcessHostingSubscriptionBackup;
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,7 +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

@ -1,65 +0,0 @@
<?php
namespace App\Console\Commands;
use App\Models\Backup;
use App\Models\HostingSubscription;
use App\Models\HostingSubscriptionBackup;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class RunHostingSubscriptionsBackupChecks extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'phyre:run-hosting-subscriptions-backup-checks';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';
/**
* Execute the console command.
*/
public function handle()
{
// Delete backups older than 7 days
$findBackupsForDeleting = HostingSubscriptionBackup::where('created_at', '<', now()->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...');
}
}
}
}

View file

@ -11,18 +11,18 @@ use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels; use Illuminate\Queue\SerializesModels;
class CreateHostingSubscriptionBackup implements ShouldQueue class ProcessHostingSubscriptionBackup implements ShouldQueue
{ {
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $hostingSubscriptionId; protected $id;
/** /**
* Create a new job instance. * 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 public function handle(): void
{ {
echo "Backup hosting subscription with ID: {$this->hostingSubscriptionId}\n"; echo "Backup hosting subscription backup with ID: {$this->id}\n";
$backup = new HostingSubscriptionBackup();
$backup->hosting_subscription_id = $this->hostingSubscriptionId;
$backup->backup_type = 'full';
$backup->save();
$backupDone = false; $backupDone = false;
$findHostingSubscriptionBackup = HostingSubscriptionBackup::where('id', $backup->id)->first(); $findHostingSubscriptionBackup = HostingSubscriptionBackup::where('id', $this->id)->first();
if ($findHostingSubscriptionBackup) { if ($findHostingSubscriptionBackup) {
for ($i = 0; $i < 200; $i++) { for ($i = 0; $i < 200; $i++) {

View file

@ -5,6 +5,7 @@ namespace App\Models;
use App\BackupStorage; use App\BackupStorage;
use App\Filament\Enums\BackupStatus; use App\Filament\Enums\BackupStatus;
use App\Helpers; use App\Helpers;
use App\Jobs\ProcessHostingSubscriptionBackup;
use App\ShellApi; use App\ShellApi;
use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Casts\Attribute; use Illuminate\Database\Eloquent\Casts\Attribute;
@ -55,7 +56,7 @@ class HostingSubscriptionBackup extends Model
}); });
static::created(function ($model) { static::created(function ($model) {
$model->startBackup(); ProcessHostingSubscriptionBackup::dispatch($model->id);
}); });
static::deleting(function ($model) { static::deleting(function ($model) {
@ -67,17 +68,6 @@ class HostingSubscriptionBackup extends Model
public function checkCronJob() 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'; $cronJobCommand = 'phyre-php /usr/local/phyre/web/artisan phyre:create-daily-full-hosting-subscriptions-backup';
$findCronJob = CronJob::where('command', $cronJobCommand)->first(); $findCronJob = CronJob::where('command', $cronJobCommand)->first();
if (! $findCronJob) { if (! $findCronJob) {