@inject DatabaseServiceFactory DatabaseServiceFactory
@inject ServerProcessService ServerProcessService
@inject IMessageService MessageService
@inject INotificationService NotificationService
@implements IAsyncDisposable
@switch (Status)
{
case ServerProcessStatus.Running:
break;
case ServerProcessStatus.Starting:
break;
case ServerProcessStatus.Error:
break;
case ServerProcessStatus.Stopped:
break;
case ServerProcessStatus.Stopping:
break;
default:
break;
}
@if (Status != ServerProcessStatus.Running)
{
}
else
{
}
@code {
[Parameter] public Guid ServerId { get; set; }
Server Server;
Timer Timer;
ServerProcessStatus Status = ServerProcessStatus.Retrieving;
protected override async Task OnInitializedAsync()
{
using (var serverService = DatabaseServiceFactory.Create())
{
Server = await serverService.GetAsync(ServerId);
}
ServerProcessService.OnStatusUpdate += OnStatusUpdate;
}
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);
}
}
void OnStatusUpdate(object sender, ServerStatusUpdateEventArgs args)
{
if (args?.Server?.Id == ServerId)
{
Status = args.Status;
if (Status == ServerProcessStatus.Error)
{
NotificationService.Error(new NotificationConfig
{
Message = $"Error starting server {args.Server.Name}",
Description = args.Exception.Message,
});
}
}
}
async Task Start()
{
await ServerProcessService.StartServerAsync(Server.Id);
}
void Stop()
{
ServerProcessService.StopServerAsync(Server.Id);
}
public async ValueTask DisposeAsync()
{
if (Timer != null)
await Timer.DisposeAsync();
}
}