Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
bobimicroweber committed Apr 25, 2024
1 parent e10d549 commit 8ca89c3
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 17 deletions.
5 changes: 2 additions & 3 deletions web/app/Console/Commands/RunBackup.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ class RunBackup extends Command
*/
public function handle()
{

// Find Hosting Subscriptions
$findHostingSubscriptions = HostingSubscription::all();
if ($findHostingSubscriptions->count() > 0) {
Expand Down Expand Up @@ -68,8 +67,8 @@ public function handle()
}
}

// Check for running backups
$getRunningBackups = Backup::where('status', 'running')->get();
// Check for processing backups
$getRunningBackups = Backup::where('status', 'processing')->get();
if ($getRunningBackups->count() > 0) {
foreach ($getRunningBackups as $runningBackup) {
$runningBackup->checkBackup();
Expand Down
49 changes: 49 additions & 0 deletions web/app/Filament/Enums/BackupStatus.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace App\Filament\Enums;

use Filament\Support\Contracts\HasColor;
use Filament\Support\Contracts\HasIcon;
use Filament\Support\Contracts\HasLabel;

enum BackupStatus: string implements HasColor, HasIcon, HasLabel
{
case Pending = 'pending';
case Processing = 'processing';
case Completed = 'completed';
case Failed = 'failed';
case Cancelled = 'cancelled';

public function getLabel(): string
{
return match ($this) {
self::Pending => 'Pending',
self::Processing => 'Processing',
self::Completed => 'Completed',
self::Failed => 'Failed',
self::Cancelled => 'Cancelled',
};
}

public function getColor(): string | array | null
{
return match ($this) {
self::Pending => 'info',
self::Cancelled => 'warning',
self::Processing => 'warning',
self::Completed => 'success',
self::Failed => 'danger',
};
}

public function getIcon(): ?string
{
return match ($this) {
self::Pending => 'heroicon-m-sparkles',
self::Processing => 'heroicon-m-arrow-path',
self::Cancelled => 'heroicon-m-truck',
self::Completed => 'heroicon-m-check-badge',
self::Failed => 'heroicon-m-x-circle',
};
}
}
9 changes: 1 addition & 8 deletions web/app/Filament/Resources/BackupResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,7 @@ public static function table(Table $table): Table
Tables\Columns\TextColumn::make('backup_type'),
Tables\Columns\TextColumn::make('backupRelated'),
Tables\Columns\BadgeColumn::make('status')
->badge()
->color(fn (string $state): string => match ($state) {
'pending' => 'gray',
'running' => 'primary',
'completed' => 'success',
'failed' => 'danger',
default => 'gray',
}),
->badge(),

Tables\Columns\TextColumn::make('completed_at')
->state(function (Backup $backup) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public function getTabs(): array
return [
null => Tab::make('All'),
'completed' => Tab::make()->query(fn ($query) => $query->where('status', 'completed')),
'processing' => Tab::make()->query(fn ($query) => $query->where('status', 'processing')),
];
}
}
23 changes: 17 additions & 6 deletions web/app/Models/Backup.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Models;

use App\Filament\Enums\BackupStatus;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Casts\Attribute;
Expand All @@ -11,6 +12,12 @@ class Backup extends Model
{
use HasFactory;

const STATUS_PENDING = 'pending';
const STATUS_PROCESSING = 'processing';
const STATUS_COMPLETED = 'completed';
const STATUS_FAILED = 'failed';
const STATUS_CANCELLED = 'cancelled';

protected $fillable = [
'hosting_subscription_id',
'backup_type',
Expand All @@ -20,6 +27,10 @@ class Backup extends Model
'disk',
];

protected $casts = [
'status' => BackupStatus::class,
];

public static function boot()
{
parent::boot();
Expand Down Expand Up @@ -66,7 +77,7 @@ protected function backupRelated() : Attribute

public function checkBackup()
{
if ($this->status == 'running') {
if ($this->status == 'processing') {
$backupDoneFile = $this->path.'/backup.done';
if (file_exists($backupDoneFile)) {
$this->size = filesize($this->filepath);
Expand All @@ -81,10 +92,10 @@ public function checkBackup()
public function startBackup()
{

if ($this->status == 'running') {
if ($this->status == 'processing') {
return [
'status' => 'running',
'message' => 'Backup already running'
'status' => 'processing',
'message' => 'Backup is already processing'
];
}

Expand Down Expand Up @@ -125,7 +136,7 @@ public function startBackup()

$this->path = $backupPath;
$this->filepath = $backupFilePath;
$this->status = 'running';
$this->status = 'processing';
$this->queued = true;
$this->queued_at = now();
$this->save();
Expand All @@ -134,7 +145,7 @@ public function startBackup()
shell_exec('bash '.$backupTempScript.' >> ' . $backupLogFilePath . ' &');

return [
'status' => 'running',
'status' => 'processing',
'message' => 'Backup started'
];

Expand Down

0 comments on commit 8ca89c3

Please sign in to comment.