Added ability to bulk start servers

dhcp-server
Pat Hartl 2023-08-31 21:00:47 -05:00
parent c248ecc4f8
commit 685f7bf91d
3 changed files with 99 additions and 19 deletions

View File

@ -58,6 +58,8 @@
protected override async Task OnInitializedAsync()
{
Server = await ServerService.Get(ServerId);
ServerProcessService.OnStatusUpdate += OnStatusUpdate;
}
protected override void OnAfterRender(bool firstRender)
@ -73,26 +75,34 @@
}
}
private void OnStatusUpdate(object sender, ServerStatusUpdateEventArgs args)
{
if (args?.Server?.Id == ServerId)
{
Status = args.Status;
StateHasChanged();
if (Status == ServerProcessStatus.Error)
{
MessageService.Error("There was an unexpected error while trying to start the server.");
}
}
}
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);
}

View File

@ -10,6 +10,15 @@
<PageHeader Title="Servers">
<PageHeaderExtra>
<Space Direction="DirectionVHType.Horizontal">
@if (SelectedServers != null && SelectedServers.Count() > 0)
{
<SpaceItem>
<Button Type="@ButtonType.Primary" OnClick="() => StartServers()">Start</Button>
<Popconfirm OnConfirm="() => StopServers()" Title="Are you sure you want to kill these server processes?">
<Button Danger Type="@ButtonType.Primary">Stop</Button>
</Popconfirm>
</SpaceItem>
}
<SpaceItem>
<Search Placeholder="Search" @bind-Value="Search" BindOnInput DebounceMilliseconds="150" OnChange="() => LoadData()" />
</SpaceItem>
@ -20,7 +29,8 @@
</PageHeaderExtra>
</PageHeader>
<Table TItem="Server" DataSource="@Servers" Loading="@Loading" PageSize="25">
<Table TItem="Server" DataSource="@Servers" Loading="@Loading" PageSize="25" @bind-SelectedRows="SelectedServers">
<Selection Key="@(context.Id.ToString())" />
<PropertyColumn Property="s => s.Name" Sortable />
<PropertyColumn Property="s => s.Game">
<Image Src="@GetIcon(context.Game)" Height="32" Width="32" Preview="false"></Image>
@ -56,6 +66,8 @@
string Search = "";
IEnumerable<Server> SelectedServers;
protected override void OnAfterRender(bool firstRender)
{
if (firstRender)
@ -102,4 +114,30 @@
{
return $"/api/Games/{game?.Id}/Icon.png";
}
private async Task StartServers()
{
foreach (var server in SelectedServers)
{
try
{
var status = ServerProcessService.GetStatus(server);
if (status == ServerProcessStatus.Stopped || status == ServerProcessStatus.Error)
{
await ServerProcessService.StartServerAsync(server);
}
}
catch { }
}
}
private void StopServers()
{
foreach (var server in SelectedServers)
{
if (ServerProcessService.GetStatus(server) == ServerProcessStatus.Running)
ServerProcessService.StopServer(server);
}
}
}

View File

@ -33,6 +33,18 @@ namespace LANCommander.Services
}
}
public class ServerStatusUpdateEventArgs : EventArgs
{
public Server Server { get; private set; }
public ServerProcessStatus Status { get; private set; }
public ServerStatusUpdateEventArgs(Server server, ServerProcessStatus status)
{
Server = server;
Status = status;
}
}
public class LogFileMonitor : IDisposable
{
private ManualResetEvent Latch;
@ -122,6 +134,9 @@ namespace LANCommander.Services
public delegate void OnLogHandler(object sender, ServerLogEventArgs e);
public event OnLogHandler OnLog;
public delegate void OnStatusUpdateHandler(object sender, ServerStatusUpdateEventArgs e);
public event OnStatusUpdateHandler OnStatusUpdate;
private IHubContext<GameServerHub> HubContext;
public ServerProcessService(IHubContext<GameServerHub> hubContext)
@ -157,27 +172,42 @@ namespace LANCommander.Services
});
}
process.Start();
if (!process.StartInfo.UseShellExecute)
try
{
process.BeginErrorReadLine();
process.BeginOutputReadLine();
OnStatusUpdate?.Invoke(this, new ServerStatusUpdateEventArgs(server, ServerProcessStatus.Starting));
process.Start();
if (!process.StartInfo.UseShellExecute)
{
process.BeginErrorReadLine();
process.BeginOutputReadLine();
}
Processes[server.Id] = process;
foreach (var logFile in server.ServerConsoles.Where(sc => sc.Type == ServerConsoleType.LogFile))
{
StartMonitoringLog(logFile, server);
}
OnStatusUpdate?.Invoke(this, new ServerStatusUpdateEventArgs(server, ServerProcessStatus.Running));
await process.WaitForExitAsync();
}
Processes[server.Id] = process;
foreach (var logFile in server.ServerConsoles.Where(sc => sc.Type == ServerConsoleType.LogFile))
catch (Exception ex)
{
StartMonitoringLog(logFile, server);
}
OnStatusUpdate?.Invoke(this, new ServerStatusUpdateEventArgs(server, ServerProcessStatus.Error));
await process.WaitForExitAsync();
Logger.Error(ex, "Could not start server process");
}
}
public void StopServer(Server server)
{
OnStatusUpdate?.Invoke(this, new ServerStatusUpdateEventArgs(server, ServerProcessStatus.Stopping));
if (Processes.ContainsKey(server.Id))
{
var process = Processes[server.Id];
@ -190,6 +220,8 @@ namespace LANCommander.Services
LogFileMonitors[server.Id].Dispose();
LogFileMonitors.Remove(server.Id);
}
OnStatusUpdate?.Invoke(this, new ServerStatusUpdateEventArgs(server, ServerProcessStatus.Stopped));
}
private void StartMonitoringLog(ServerConsole log, Server server)