Re-implemented server state machine. Cleaned up code

TODO: Handle trigger errors
This commit is contained in:
2025-02-12 23:02:00 +01:00
parent 4088bfaef5
commit f45699f300
44 changed files with 913 additions and 831 deletions

View File

@@ -4,6 +4,7 @@
"requires": true,
"packages": {
"": {
"name": "Styles",
"dependencies": {
"@tailwindcss/forms": "^0.5.9"
},
@@ -610,9 +611,9 @@
}
},
"node_modules/nanoid": {
"version": "3.3.7",
"resolved": "https://registry.npmjs.org/nanoid/-/nanoid-3.3.7.tgz",
"integrity": "sha512-eSRppjcPIatRIMC1U6UngP8XFcz8MQWGQdt1MTBQ7NaAmvXDfvNxbvWV3x2y6CdEUciCSsDHDQZbhYaB8QEo2g==",
"version": "3.3.8",
"resolved": "https://registry.npmjs.org/nanoid/-/nanoid-3.3.8.tgz",
"integrity": "sha512-WNLf5Sd8oZxOm+TzppcYk8gVOgP+l58xNy58D0nbUnOxOWRWvlcCV4kUF7ltmI6PsrLl/BgKEyS4mqsGChFN0w==",
"funding": [
{
"type": "github",

View File

@@ -41,7 +41,9 @@
{
Form = new()
{
IpAddress = "0.0.0.0"
IpAddress = "0.0.0.0",
Start = 2000,
End = 3000
};
}

View File

@@ -11,23 +11,23 @@
if (IsLoaded && !IsFailed)
{
gradient = Status.PowerState switch
gradient = Status.State switch
{
ServerPowerState.Installing => "from-primary-600/20",
ServerPowerState.Offline => "from-danger-600/20",
ServerPowerState.Starting => "from-warning-600/20",
ServerPowerState.Stopping => "from-warning-600/20",
ServerPowerState.Online => "from-success-600/20",
ServerState.Installing => "from-primary-600/20",
ServerState.Offline => "from-danger-600/20",
ServerState.Starting => "from-warning-600/20",
ServerState.Stopping => "from-warning-600/20",
ServerState.Online => "from-success-600/20",
_ => "from-gray-600/20"
};
border = Status.PowerState switch
border = Status.State switch
{
ServerPowerState.Installing => "border-primary-600",
ServerPowerState.Offline => "border-danger-600",
ServerPowerState.Starting => "border-warning-600",
ServerPowerState.Stopping => "border-warning-600",
ServerPowerState.Online => "border-success-600",
ServerState.Installing => "border-primary-600",
ServerState.Offline => "border-danger-600",
ServerState.Starting => "border-warning-600",
ServerState.Stopping => "border-warning-600",
ServerState.Online => "border-success-600",
_ => "border-gray-600"
};
}
@@ -49,7 +49,7 @@
@if (
IsLoaded &&
!IsFailed &&
Status.PowerState is ServerPowerState.Starting or ServerPowerState.Stopping or ServerPowerState.Online
Status.State is ServerState.Starting or ServerState.Stopping or ServerState.Online
)
{
<div class="bg-gray-900 bg-opacity-45 py-1 px-2 rounded-lg flex flex-row">
@@ -98,7 +98,7 @@
<div class="ms-3">Unreachable</div>
</div>
}
else if (IsLoaded && !IsFailed && Status.PowerState is ServerPowerState.Offline)
else if (IsLoaded && !IsFailed && Status.State is ServerState.Offline)
{
<div class="bg-gray-900 bg-opacity-45 py-1 px-2 rounded-lg flex flex-row text-danger-500">
<div>
@@ -108,7 +108,7 @@
<div class="ms-3">Offline</div>
</div>
}
else if (IsLoaded && !IsFailed && Status.PowerState is ServerPowerState.Installing)
else if (IsLoaded && !IsFailed && Status.State is ServerState.Installing)
{
<div class="bg-gray-900 bg-opacity-45 py-1 px-2 rounded-lg flex flex-row text-primary-500">
<div>

View File

@@ -15,9 +15,11 @@
<LazyLoader Load="Load">
@if (NotFound)
{
<div class="flex flex-col justify-center text-center"><img class="h-48 mt-5 mb-3" src="/svg/notfound.svg"
alt="Not found illustration">
<h3 class="mt-2 font-semibold text-white text-lg">Server not found</h3>
<div class="flex flex-col justify-center text-center">
<img class="h-48 mt-5 mb-3" src="/svg/notfound.svg" alt="Not found illustration">
<h3 class="mt-2 font-semibold text-white text-lg">
Server not found
</h3>
<p class="mt-1 text-gray-300">
The server you requested does not exist
</p>
@@ -28,17 +30,17 @@
<div class="card card-body justify-between py-2.5 px-5 flex-row">
<div class="flex flex-row items-center">
@{
var bgColor = PowerState switch
var bgColor = State switch
{
ServerPowerState.Installing => "bg-primary-500",
ServerPowerState.Offline => "bg-danger-500",
ServerPowerState.Starting => "bg-warning-500",
ServerPowerState.Stopping => "bg-warning-500",
ServerPowerState.Online => "bg-success-500",
ServerState.Installing => "bg-primary-500",
ServerState.Offline => "bg-danger-500",
ServerState.Starting => "bg-warning-500",
ServerState.Stopping => "bg-warning-500",
ServerState.Online => "bg-success-500",
_ => "bg-gray-500"
};
}
<div class="p-2.5 rounded-full @bgColor me-3"></div>
<div class="flex flex-col">
@@ -57,29 +59,39 @@
</div>
</div>
<div class="flex flex-row items-center">
@if (!string.IsNullOrEmpty(CurrentTask))
{
<div class="hidden md:flex me-8 items-center text-gray-600">
<i class="icon-loader me-1.5 animate-spin text-lg align-middle"></i>
<span class="text-base align-middle">
@CurrentTask
</span>
</div>
}
<div class="flex gap-x-1.5">
<button class="btn btn-success">
<i class="icon-play me-1 align-middle"></i>
<span class="align-middle">Start</span>
</button>
@if (State == ServerState.Offline)
{
<WButton CssClasses="btn btn-success" OnClick="_ => Start()">
<i class="icon-play me-1 align-middle"></i>
<span class="align-middle">Start</span>
</WButton>
}
else
{
<button type="button" class="btn btn-success" disabled="disabled">
<i class="icon-play me-1 align-middle"></i>
<span class="align-middle">Start</span>
</button>
}
<button type="button" class="btn btn-primary">
<i class="icon-rotate-ccw me-1 align-middle"></i>
<span class="align-middle">Restart</span>
</button>
<button type="button" class="btn btn-danger">
<i class="icon-squircle me-1 align-middle"></i>
<span class="align-middle">Stop</span>
</button>
@if (State == ServerState.Starting || State == ServerState.Online)
{
<WButton CssClasses="btn btn-danger" OnClick="_ => Stop()">
<i class="icon-squircle me-1 align-middle"></i>
<span class="align-middle">Stop</span>
</WButton>
}
else
{
<button type="button" class="btn btn-danger" disabled="disabled">
<i class="icon-squircle me-1 align-middle"></i>
<span class="align-middle">Stop</span>
</button>
}
</div>
</div>
</div>
@@ -87,31 +99,45 @@
<div class="mt-5 mx-2 relative">
<ul class="relative text-sm font-medium flex flex-nowrap -mx-4 sm:-mx-6 lg:-mx-8 overflow-x-scroll no-scrollbar">
<li class="mr-6 last:mr-0 first:pl-4 sm:first:pl-6 lg:first:pl-8 last:pr-4 sm:last:pr-6 lg:last:pr-8"><a
href="/admin/servers/all"
class="block pb-3 text-white whitespace-nowrap border-b-2 border-primary-500">Console</a></li>
<li class="mr-6 last:mr-0 first:pl-4 sm:first:pl-6 lg:first:pl-8 last:pr-4 sm:last:pr-6 lg:last:pr-8"><a
href="/admin/servers"
class="block pb-3 text-gray-400 hover:text-white whitespace-nowrap hover:border-b-2 hover:border-primary-500">Files</a>
<li class="mr-6 last:mr-0 first:pl-4 sm:first:pl-6 lg:first:pl-8 last:pr-4 sm:last:pr-6 lg:last:pr-8">
<a
href="/admin/servers/all"
class="block pb-3 text-white whitespace-nowrap border-b-2 border-primary-500">Console</a>
</li>
<li class="mr-6 last:mr-0 first:pl-4 sm:first:pl-6 lg:first:pl-8 last:pr-4 sm:last:pr-6 lg:last:pr-8"><a
href="/admin/servers/nodes"
class="block pb-3 text-gray-400 hover:text-white whitespace-nowrap hover:border-b-2 hover:border-primary-500">Backups</a>
<li class="mr-6 last:mr-0 first:pl-4 sm:first:pl-6 lg:first:pl-8 last:pr-4 sm:last:pr-6 lg:last:pr-8">
<a
href="/admin/servers"
class="block pb-3 text-gray-400 hover:text-white whitespace-nowrap hover:border-b-2 hover:border-primary-500">
Files
</a>
</li>
<li class="mr-6 last:mr-0 first:pl-4 sm:first:pl-6 lg:first:pl-8 last:pr-4 sm:last:pr-6 lg:last:pr-8"><a
href="/admin/servers/stars"
class="block pb-3 text-gray-400 hover:text-white whitespace-nowrap hover:border-b-2 hover:border-primary-500">Networking</a>
<li class="mr-6 last:mr-0 first:pl-4 sm:first:pl-6 lg:first:pl-8 last:pr-4 sm:last:pr-6 lg:last:pr-8">
<a
href="/admin/servers/nodes"
class="block pb-3 text-gray-400 hover:text-white whitespace-nowrap hover:border-b-2 hover:border-primary-500">
Backups
</a>
</li>
<li class="mr-6 last:mr-0 first:pl-4 sm:first:pl-6 lg:first:pl-8 last:pr-4 sm:last:pr-6 lg:last:pr-8"><a
href="/admin/servers/manager"
class="block pb-3 text-gray-400 hover:text-white whitespace-nowrap hover:border-b-2 hover:border-primary-500">Variables</a>
<li class="mr-6 last:mr-0 first:pl-4 sm:first:pl-6 lg:first:pl-8 last:pr-4 sm:last:pr-6 lg:last:pr-8">
<a
href="/admin/servers/stars"
class="block pb-3 text-gray-400 hover:text-white whitespace-nowrap hover:border-b-2 hover:border-primary-500">
Networking
</a>
</li>
<li class="mr-6 last:mr-0 first:pl-4 sm:first:pl-6 lg:first:pl-8 last:pr-4 sm:last:pr-6 lg:last:pr-8">
<a
href="/admin/servers/manager"
class="block pb-3 text-gray-400 hover:text-white whitespace-nowrap hover:border-b-2 hover:border-primary-500">
Variables
</a>
</li>
</ul>
</div>
<div class="mt-3 h-44">
<XtermConsole @ref="XtermConsole" OnAfterInitialized="OnAfterConsoleInitialized" />
<XtermConsole @ref="XtermConsole" OnAfterInitialized="OnAfterConsoleInitialized"/>
</div>
}
</LazyLoader>
@@ -122,13 +148,12 @@
private ServerDetailResponse Server;
private bool NotFound = false;
private ServerPowerState PowerState;
private ServerState State;
private string InitialConsoleMessage; // TODO: When moving to a single component, fail safe when failed to load
private string CurrentTask = "";
private XtermConsole? XtermConsole;
private HubConnection ConsoleConnection;
private HubConnection WebSocketConnection;
private async Task Load(LazyLoader _)
{
@@ -144,7 +169,7 @@
$"api/servers/{ServerId}/status"
);
PowerState = status.PowerState;
State = status.State;
// Load initial messages
var initialLogs = await ApiClient.GetJson<ServerLogsResponse>(
@@ -155,45 +180,40 @@
foreach (var message in initialLogs.Messages)
InitialConsoleMessage += message;
// Load console meta
var consoleDetails = await ApiClient.GetJson<ServerConsoleResponse>(
$"api/servers/{ServerId}/console"
// Load websocket meta
var websocketDetails = await ApiClient.GetJson<ServerWebSocketResponse>(
$"api/servers/{ServerId}/ws"
);
// Build signal r
ConsoleConnection = new HubConnectionBuilder()
.WithUrl(consoleDetails.Target)
WebSocketConnection = new HubConnectionBuilder()
.WithUrl(websocketDetails.Target)
.Build();
// Define handlers
ConsoleConnection.On<string>("PowerStateChanged", async powerStateStr =>
WebSocketConnection.On<string>("StateChanged", async stateStr =>
{
if(!Enum.TryParse(powerStateStr, out ServerPowerState receivedState))
if (!Enum.TryParse(stateStr, out ServerState receivedState))
return;
PowerState = receivedState;
State = receivedState;
await InvokeAsync(StateHasChanged);
});
ConsoleConnection.On<string>("TaskNotify", async task =>
{
await AddTask(Formatter.ConvertCamelCaseToSpaces(task));
});
ConsoleConnection.On<string>("ConsoleOutput", async content =>
WebSocketConnection.On<string>("ConsoleOutput", async content =>
{
if (XtermConsole != null)
await XtermConsole.Write(content);
await InvokeAsync(StateHasChanged);
});
// Connect
await ConsoleConnection.StartAsync();
await WebSocketConnection.StartAsync();
// Authenticate
await ConsoleConnection.SendAsync("Authenticate", consoleDetails.AccessToken);
await WebSocketConnection.SendAsync("Authenticate", websocketDetails.AccessToken);
}
catch (HttpApiException e)
{
@@ -203,34 +223,27 @@
throw;
}
}
private async Task OnAfterConsoleInitialized()
{
await XtermConsole!.Write(InitialConsoleMessage);
}
private async Task AddTask(string message)
private async Task Start()
{
CurrentTask = message;
await InvokeAsync(StateHasChanged);
await ApiClient.Post($"api/servers/{Server.Id}/start");
}
Task.Run(async () =>
{
await Task.Delay(3000);
if (CurrentTask != message)
return;
CurrentTask = "";
await InvokeAsync(StateHasChanged);
});
private async Task Stop()
{
await ApiClient.Post($"api/servers/{Server.Id}/stop");
}
public async ValueTask DisposeAsync()
{
if (ConsoleConnection.State == HubConnectionState.Connected)
await ConsoleConnection.StopAsync();
await ConsoleConnection.DisposeAsync();
if (WebSocketConnection.State == HubConnectionState.Connected)
await WebSocketConnection.StopAsync();
await WebSocketConnection.DisposeAsync();
}
}