Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamic server status #803

Merged
merged 9 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions app/Enums/ContainerStatus.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,35 @@ public function color(): string
self::Offline => 'gray',
};
}

public function isStartingOrStopping(): bool
{
return in_array($this, [ContainerStatus::Starting, ContainerStatus::Stopping, ContainerStatus::Restarting]);
}

public function isStartable(): bool
{
return !in_array($this, [ContainerStatus::Running, ContainerStatus::Starting, ContainerStatus::Stopping, ContainerStatus::Restarting]);
}

public function isRestartable(): bool
{
if ($this->isStartable()) {
return true;
}

return !in_array($this, [ContainerStatus::Offline]);
}

public function isStoppable(): bool
{
return !in_array($this, [ContainerStatus::Starting, ContainerStatus::Stopping, ContainerStatus::Restarting, ContainerStatus::Exited, ContainerStatus::Offline]);
}

public function isKillable(): bool
{
// [ContainerStatus::Restarting, ContainerStatus::Removing, ContainerStatus::Dead, ContainerStatus::Created]

return !in_array($this, [ContainerStatus::Offline, ContainerStatus::Running, ContainerStatus::Exited]);
}
}
21 changes: 11 additions & 10 deletions app/Filament/Server/Pages/Console.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class Console extends Page

protected static string $view = 'filament.server.pages.console';

public ContainerStatus $status = ContainerStatus::Missing;
public ContainerStatus $status = ContainerStatus::Offline;

public function getWidgetData(): array
{
Expand Down Expand Up @@ -54,10 +54,12 @@ public function getColumns(): int|string|array
return 3;
}

#[On('powerChanged')]
public function powerChanged(string $state): void
#[On('console-status')]
public function receivedConsoleUpdate(?string $state = null): void
{
$this->status = ContainerStatus::from($state);
if ($state) {
$this->status = ContainerStatus::from($state);
}

$this->cachedHeaderActions = [];

Expand All @@ -74,18 +76,18 @@ protected function getHeaderActions(): array
->color('primary')
->size(ActionSize::ExtraLarge)
->action(fn () => $this->dispatch('setServerState', state: 'start'))
->disabled(fn () => $server->isInConflictState() || in_array($this->status, [ContainerStatus::Running, ContainerStatus::Starting, ContainerStatus::Stopping, ContainerStatus::Restarting])),
->disabled(fn () => $server->isInConflictState() || !$this->status->isStartable()),
Action::make('restart')
->color('gray')
->size(ActionSize::ExtraLarge)
->action(fn () => $this->dispatch('setServerState', state: 'restart'))
->disabled(fn () => $server->isInConflictState() || $this->status !== ContainerStatus::Running),
->disabled(fn () => $server->isInConflictState() || !$this->status->isRestartable()),
Action::make('stop')
->color('danger')
->size(ActionSize::ExtraLarge)
->action(fn () => $this->dispatch('setServerState', state: 'stop'))
->hidden(fn () => in_array($this->status, [ContainerStatus::Stopping, ContainerStatus::Restarting, ContainerStatus::Starting]))
->disabled(fn () => $server->isInConflictState() || in_array($this->status, [ContainerStatus::Starting, ContainerStatus::Stopping, ContainerStatus::Restarting, ContainerStatus::Exited, ContainerStatus::Offline])),
->hidden(fn () => $this->status->isStartingOrStopping() || $this->status->isKillable())
->disabled(fn () => $server->isInConflictState() || !$this->status->isStoppable()),
Action::make('kill')
->color('danger')
->requiresConfirmation()
Expand All @@ -94,8 +96,7 @@ protected function getHeaderActions(): array
->modalSubmitActionLabel('Kill Server')
->size(ActionSize::ExtraLarge)
->action(fn () => $this->dispatch('setServerState', state: 'kill'))
->hidden(fn () => $server->isInConflictState() || in_array($this->status, [ContainerStatus::Running, ContainerStatus::Restarting, ContainerStatus::Offline, ContainerStatus::Removing, ContainerStatus::Dead, ContainerStatus::Exited, ContainerStatus::Created]))
->disabled(fn () => $server->isInConflictState() || $this->status === ContainerStatus::Offline),
->hidden(fn () => $server->isInConflictState() || !$this->status->isKillable()),
];
}
}
2 changes: 1 addition & 1 deletion app/Filament/Server/Widgets/ServerConsole.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public function enter(): void
}
}

#[On('storeStats')]
#[On('store-stats')]
public function storeStats(string $data): void
{
$data = json_decode($data);
Expand Down
34 changes: 34 additions & 0 deletions app/Livewire/ServerConflictBanner.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace App\Livewire;

use App\Models\Server;
use Filament\Facades\Filament;
use Illuminate\View\View;
use Livewire\Attributes\On;
use Livewire\Component;

class ServerConflictBanner extends Component
{
public ?Server $server = null;

public function mount(): void
{
/** @var Server $server */
$server = Filament::getTenant();
$this->server = $server;
}

#[On('console-install-completed')]
#[On('console-install-started')]
#[On('console-status')]
public function refresh(?string $state = null): void
{
$this->server->fresh();
}

public function render(): View
{
return view('livewire.server-conflict-banner');
}
}
10 changes: 8 additions & 2 deletions resources/views/filament/components/server-console.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ class="w-full focus:outline-none focus:ring-0 border-none"
case 'status':
handlePowerChangeEvent(args[0]);

$wire.dispatch('powerChanged', {state: args[0]})
$wire.dispatch('console-status', {state: args[0]})
break;
case 'transfer status':
handleTransferStatus(args[0]);
Expand All @@ -135,14 +135,20 @@ class="w-full focus:outline-none focus:ring-0 border-none"
handleDaemonErrorOutput(args[0]);
break;
case 'stats':
$wire.dispatchSelf('storeStats', { data: args[0] });
$wire.dispatchSelf('store-stats', { data: args[0] });
break;
case 'auth success':
socket.send(JSON.stringify({
'event': 'send logs',
'args': [null]
}));
break;
case 'install started':
$wire.dispatch('console-install-started');
break;
case 'install completed':
$wire.dispatch('console-install-completed');
break;
case 'token expiring':
case 'token expired':
token = '{{ $this->getToken() }}';
Expand Down
20 changes: 1 addition & 19 deletions resources/views/filament/server-conflict-banner.blade.php
Original file line number Diff line number Diff line change
@@ -1,19 +1 @@
@php
$shouldShow = false;

try {
\Filament\Facades\Filament::getTenant()->validateCurrentState();
} catch (\App\Exceptions\Http\Server\ServerStateConflictException $exception) {
$shouldShow = true;
$message = $exception->getMessage();
}
@endphp

@if ($shouldShow)
<div class="mt-2 p-2 rounded-lg text-white" style="background-color: #D97706;">
<div class="flex items-center">
<x-filament::icon icon="tabler-alert-triangle" class="h-6 w-6 mr-2 text-gray-500 dark:text-gray-400 text-white" />
<p>{!! $message !!}</p>
</div>
</div>
@endif
<livewire:server-conflict-banner />
21 changes: 21 additions & 0 deletions resources/views/livewire/server-conflict-banner.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
@php
$shouldShow = false;

try {
$server->validateCurrentState();
} catch (\App\Exceptions\Http\Server\ServerStateConflictException $exception) {
$shouldShow = true;
$message = $exception->getMessage();
}
@endphp

<div id="server-conflict-banner">
@if ($shouldShow)
<div class="mt-2 p-2 rounded-lg text-white" style="background-color: #D97706;">
<div class="flex items-center">
<x-filament::icon icon="tabler-alert-triangle" class="h-6 w-6 mr-2 text-gray-500 dark:text-gray-400 text-white" />
<p>{!! $message !!}</p>
</div>
</div>
@endif
</div>
Loading