Unified server control

dhcp-server
Pat Hartl 2023-08-17 01:32:27 -05:00
parent c0d04512e4
commit 611cd889ae
4 changed files with 109 additions and 130 deletions

View File

@ -0,0 +1,104 @@
@inject ServerService ServerService
@inject ServerProcessService ServerProcessService
@inject IMessageService MessageService
@implements IAsyncDisposable
<Space Size="@("large")">
<SpaceItem>
@switch (Status)
{
case ServerProcessStatus.Running:
<Badge Status="success" Text="Running" />
break;
case ServerProcessStatus.Starting:
<Badge Status="processing" Text="Starting" />
break;
case ServerProcessStatus.Error:
<Badge Status="error" Text="Error" />
break;
case ServerProcessStatus.Stopped:
<Badge Status="default" Text="Stopped" />
break;
case ServerProcessStatus.Stopping:
<Badge Status="error" Text="Stopping" />
break;
default:
<Badge Status="warning" Text="Retrieving" />
break;
}
</SpaceItem>
<SpaceItem>
@if (Status != ServerProcessStatus.Running)
{
<Button Type="@ButtonType.Primary" OnClick="() => Start()" Disabled="Status != ServerProcessStatus.Stopped && Status != ServerProcessStatus.Error">Start</Button>
}
else
{
<Popconfirm OnConfirm="() => Stop()" Title="Are you sure you want to kill this server process?">
<Button Danger Type="@ButtonType.Primary">Stop</Button>
</Popconfirm>
}
</SpaceItem>
</Space>
@code {
[Parameter] public Guid ServerId { get; set; }
Server Server;
Timer Timer;
ServerProcessStatus Status = ServerProcessStatus.Retrieving;
protected override async Task OnInitializedAsync()
{
Server = await ServerService.Get(ServerId);
}
protected override void OnAfterRender(bool firstRender)
{
if (firstRender)
{
Timer = new Timer(async (object? stateInfo) =>
{
Status = ServerProcessService.GetStatus(Server);
await InvokeAsync(StateHasChanged);
}, new AutoResetEvent(false), 1000, 1000);
}
}
private async Task Start()
{
try
{
Status = ServerProcessStatus.Starting;
await ServerProcessService.StartServerAsync(Server);
}
catch (Exception ex)
{
Status = ServerProcessStatus.Error;
await MessageService.Error("There was an unexpected error while trying to start the server.");
}
}
private void Stop()
{
Status = ServerProcessStatus.Stopping;
ServerProcessService.StopServer(Server);
}
public async ValueTask DisposeAsync()
{
if (Timer != null)
await Timer.DisposeAsync();
}
}

View File

@ -27,42 +27,9 @@
<PageHeader>
<PageHeaderTitle>Logs</PageHeaderTitle>
<PageHeaderExtra>
<Space Size="@("large")">
<SpaceItem Style="margin-right: 24px;">
@switch (Status)
{
case ServerProcessStatus.Running:
<Badge Status="success" Text="Running" />
break;
case ServerProcessStatus.Starting:
<Badge Status="processing" Text="Starting" />
break;
case ServerProcessStatus.Error:
<Badge Status="error" Text="Error" />
break;
case ServerProcessStatus.Stopped:
default:
<Badge Status="default" Text="Stopped" />
break;
}
</SpaceItem>
@if (Status == ServerProcessStatus.Error || Status == ServerProcessStatus.Stopped)
{
<Button Type="@ButtonType.Primary" OnClick="() => Start()">Start</Button>
}
else
{
<Popconfirm OnConfirm="() => Stop()" Title="Are you sure you want to kill this server process?">
<Button Danger Type="@ButtonType.Primary">Stop</Button>
</Popconfirm>
}
</Space>
<ServerControl ServerId="Id" />
</PageHeaderExtra>
</PageHeader>
</PageHeader>
}
else
{
@ -171,8 +138,6 @@
IEnumerable<Game> Games = new List<Game>();
Server Server;
ServerProcessStatus Status;
Timer Timer;
Guid GameId;
protected override async Task OnInitializedAsync()
@ -191,36 +156,6 @@
Games = GameService.Get().ToList();
}
protected override void OnAfterRender(bool firstRender)
{
if (firstRender)
{
Timer = new Timer(async (object? stateInfo) =>
{
Status = ServerProcessService.GetStatus(Server);
await InvokeAsync(StateHasChanged);
}, new AutoResetEvent(false), 1000, 1000);
}
}
private async Task Start()
{
try
{
await ServerProcessService.StartServerAsync(Server);
}
catch (Exception ex)
{
await MessageService.Error("There was an unexpected error while trying to start the server.");
}
}
private void Stop()
{
ServerProcessService.StopServer(Server);
}
private async Task Save()
{
try

View File

@ -1,4 +1,5 @@
@page "/Servers"
@using LANCommander.Pages.Servers.Components
@attribute [Authorize]
@inject ServerService ServerService
@inject ServerProcessService ServerProcessService
@ -25,45 +26,12 @@
<PropertyColumn Property="g => g.UpdatedBy" Sortable>
@context.UpdatedBy?.UserName
</PropertyColumn>
<Column TData="string" Title="Status">
@switch (Status.GetValueOrDefault(context.Id))
{
case ServerProcessStatus.Running:
<Badge Status="success" Text="Running" />
break;
case ServerProcessStatus.Starting:
<Badge Status="processing" Text="Starting" />
break;
case ServerProcessStatus.Error:
<Badge Status="error" Text="Error" />
break;
case ServerProcessStatus.Stopped:
default:
<Badge Status="default" Text="Stopped" />
break;
}
</Column>
<ActionColumn Title="" Style="text-align: right">
<ServerControl ServerId="context.Id" />
<Space Direction="DirectionVHType.Horizontal">
<SpaceItem>
<Button OnClick="() => Edit(context)">Edit</Button>
<Button OnClick="() => Logs(context)">Logs</Button>
@if (Status.GetValueOrDefault(context.Id) != ServerProcessStatus.Running)
{
<Button Type="@ButtonType.Primary" OnClick="() => Start(context)">Start</Button>
}
@if (Status.GetValueOrDefault(context.Id) == ServerProcessStatus.Running)
{
<Popconfirm OnConfirm="() => Stop(context)" Title="Are you sure you want to kill this server process?">
<Button Danger Type="@ButtonType.Primary">Stop</Button>
</Popconfirm>
}
</SpaceItem>
<SpaceItem>
<Popconfirm OnConfirm="() => Delete(context)" Title="Are you sure you want to delete this server?">
@ -78,8 +46,6 @@
IEnumerable<Server> Servers { get; set; } = new List<Server>();
bool Loading = true;
Dictionary<Guid, ServerProcessStatus> Status = new Dictionary<Guid, ServerProcessStatus>();
Timer Timer;
protected override void OnAfterRender(bool firstRender)
{
@ -89,16 +55,6 @@
Loading = false;
Timer = new Timer(async (object? stateInfo) =>
{
foreach (var server in Servers)
{
Status[server.Id] = ServerProcessService.GetStatus(server);
}
await InvokeAsync(StateHasChanged);
}, new AutoResetEvent(false), 1000, 1000);
StateHasChanged();
}
}
@ -118,23 +74,6 @@
NavigationManager.NavigateTo($"/Servers/{server.Id}/Edit/Logs");
}
private async Task Start(Server server)
{
try
{
await ServerProcessService.StartServerAsync(server);
}
catch (Exception ex)
{
await MessageService.Error("There was an unexpected error while trying to start the server.");
}
}
private void Stop(Server server)
{
ServerProcessService.StopServer(server);
}
private async Task Delete(Server server)
{
Servers = new List<Server>();

View File

@ -13,6 +13,7 @@ namespace LANCommander.Services
Retrieving,
Stopped,
Starting,
Stopping,
Running,
Error
}