Unified server control
This commit is contained in:
parent
c0d04512e4
commit
611cd889ae
4 changed files with 109 additions and 130 deletions
104
LANCommander/Pages/Servers/Components/ServerControl.razor
Normal file
104
LANCommander/Pages/Servers/Components/ServerControl.razor
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
|
@ -27,42 +27,9 @@
|
||||||
<PageHeader>
|
<PageHeader>
|
||||||
<PageHeaderTitle>Logs</PageHeaderTitle>
|
<PageHeaderTitle>Logs</PageHeaderTitle>
|
||||||
<PageHeaderExtra>
|
<PageHeaderExtra>
|
||||||
<Space Size="@("large")">
|
<ServerControl ServerId="Id" />
|
||||||
<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>
|
|
||||||
</PageHeaderExtra>
|
</PageHeaderExtra>
|
||||||
</PageHeader>
|
</PageHeader>
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
@ -171,8 +138,6 @@
|
||||||
IEnumerable<Game> Games = new List<Game>();
|
IEnumerable<Game> Games = new List<Game>();
|
||||||
|
|
||||||
Server Server;
|
Server Server;
|
||||||
ServerProcessStatus Status;
|
|
||||||
Timer Timer;
|
|
||||||
Guid GameId;
|
Guid GameId;
|
||||||
|
|
||||||
protected override async Task OnInitializedAsync()
|
protected override async Task OnInitializedAsync()
|
||||||
|
@ -191,36 +156,6 @@
|
||||||
Games = GameService.Get().ToList();
|
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()
|
private async Task Save()
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
@page "/Servers"
|
@page "/Servers"
|
||||||
|
@using LANCommander.Pages.Servers.Components
|
||||||
@attribute [Authorize]
|
@attribute [Authorize]
|
||||||
@inject ServerService ServerService
|
@inject ServerService ServerService
|
||||||
@inject ServerProcessService ServerProcessService
|
@inject ServerProcessService ServerProcessService
|
||||||
|
@ -25,45 +26,12 @@
|
||||||
<PropertyColumn Property="g => g.UpdatedBy" Sortable>
|
<PropertyColumn Property="g => g.UpdatedBy" Sortable>
|
||||||
@context.UpdatedBy?.UserName
|
@context.UpdatedBy?.UserName
|
||||||
</PropertyColumn>
|
</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">
|
<ActionColumn Title="" Style="text-align: right">
|
||||||
|
<ServerControl ServerId="context.Id" />
|
||||||
<Space Direction="DirectionVHType.Horizontal">
|
<Space Direction="DirectionVHType.Horizontal">
|
||||||
<SpaceItem>
|
<SpaceItem>
|
||||||
<Button OnClick="() => Edit(context)">Edit</Button>
|
<Button OnClick="() => Edit(context)">Edit</Button>
|
||||||
<Button OnClick="() => Logs(context)">Logs</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>
|
||||||
<SpaceItem>
|
<SpaceItem>
|
||||||
<Popconfirm OnConfirm="() => Delete(context)" Title="Are you sure you want to delete this server?">
|
<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>();
|
IEnumerable<Server> Servers { get; set; } = new List<Server>();
|
||||||
|
|
||||||
bool Loading = true;
|
bool Loading = true;
|
||||||
Dictionary<Guid, ServerProcessStatus> Status = new Dictionary<Guid, ServerProcessStatus>();
|
|
||||||
Timer Timer;
|
|
||||||
|
|
||||||
protected override void OnAfterRender(bool firstRender)
|
protected override void OnAfterRender(bool firstRender)
|
||||||
{
|
{
|
||||||
|
@ -89,16 +55,6 @@
|
||||||
|
|
||||||
Loading = false;
|
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();
|
StateHasChanged();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -118,23 +74,6 @@
|
||||||
NavigationManager.NavigateTo($"/Servers/{server.Id}/Edit/Logs");
|
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)
|
private async Task Delete(Server server)
|
||||||
{
|
{
|
||||||
Servers = new List<Server>();
|
Servers = new List<Server>();
|
||||||
|
|
|
@ -13,6 +13,7 @@ namespace LANCommander.Services
|
||||||
Retrieving,
|
Retrieving,
|
||||||
Stopped,
|
Stopped,
|
||||||
Starting,
|
Starting,
|
||||||
|
Stopping,
|
||||||
Running,
|
Running,
|
||||||
Error
|
Error
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue