provide more informative error if webfront fails to start (typical socket binding)

This commit is contained in:
RaidMax 2023-04-21 20:40:20 -05:00
parent ddfcf6e138
commit 123d84088f
2 changed files with 49 additions and 14 deletions

View File

@ -53,6 +53,7 @@ namespace IW4MAdmin
private readonly CommandConfiguration _commandConfiguration; private readonly CommandConfiguration _commandConfiguration;
private EFServer _cachedDatabaseServer; private EFServer _cachedDatabaseServer;
private readonly StatManager _statManager; private readonly StatManager _statManager;
private readonly ApplicationConfiguration _appConfig;
public IW4MServer( public IW4MServer(
ServerConfiguration serverConfiguration, ServerConfiguration serverConfiguration,
@ -77,6 +78,7 @@ namespace IW4MAdmin
_serverCache = serverCache; _serverCache = serverCache;
_commandConfiguration = commandConfiguration; _commandConfiguration = commandConfiguration;
_statManager = serviceProvider.GetRequiredService<StatManager>(); _statManager = serviceProvider.GetRequiredService<StatManager>();
_appConfig = serviceProvider.GetService<ApplicationConfiguration>();
IGameServerEventSubscriptions.MonitoringStarted += async (gameEvent, token) => IGameServerEventSubscriptions.MonitoringStarted += async (gameEvent, token) =>
{ {
@ -1269,15 +1271,13 @@ namespace IW4MAdmin
private void RunServerCollection() private void RunServerCollection()
{ {
var appConfig = _serviceProvider.GetService<ApplicationConfiguration>(); if (DateTime.Now - _lastPlayerCount < _appConfig?.ServerDataCollectionInterval)
if (DateTime.Now - _lastPlayerCount < appConfig?.ServerDataCollectionInterval)
{ {
return; return;
} }
var maxItems = Math.Ceiling(appConfig.MaxClientHistoryTime.TotalMinutes / var maxItems = Math.Ceiling(_appConfig!.MaxClientHistoryTime.TotalMinutes /
appConfig.ServerDataCollectionInterval.TotalMinutes); _appConfig.ServerDataCollectionInterval.TotalMinutes);
while (ClientHistory.ClientCounts.Count > maxItems) while (ClientHistory.ClientCounts.Count > maxItems)
{ {

View File

@ -38,6 +38,7 @@ using Microsoft.Extensions.Logging;
using ILogger = Microsoft.Extensions.Logging.ILogger; using ILogger = Microsoft.Extensions.Logging.ILogger;
using IW4MAdmin.Plugins.Stats.Client.Abstractions; using IW4MAdmin.Plugins.Stats.Client.Abstractions;
using IW4MAdmin.Plugins.Stats.Client; using IW4MAdmin.Plugins.Stats.Client;
using Microsoft.Extensions.Hosting;
using Stats.Client.Abstractions; using Stats.Client.Abstractions;
using Stats.Client; using Stats.Client;
using Stats.Config; using Stats.Config;
@ -125,8 +126,7 @@ namespace IW4MAdmin.Application
await _serverManager.Init(); await _serverManager.Init();
_applicationTask = Task.WhenAll(RunApplicationTasksAsync(logger, _serviceProvider), _applicationTask = RunApplicationTasksAsync(logger, _serverManager, _serviceProvider);
_serverManager.Start());
await _applicationTask; await _applicationTask;
logger.LogInformation("Shutdown completed successfully"); logger.LogInformation("Shutdown completed successfully");
@ -185,14 +185,49 @@ namespace IW4MAdmin.Application
/// runs the core application tasks /// runs the core application tasks
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
private static Task RunApplicationTasksAsync(ILogger logger, IServiceProvider serviceProvider) private static Task RunApplicationTasksAsync(ILogger logger, ApplicationManager applicationManager,
IServiceProvider serviceProvider)
{ {
var webfrontTask = _serverManager.GetApplicationSettings().Configuration().EnableWebFront
? WebfrontCore.Program.GetWebHostTask(_serverManager.CancellationToken)
: Task.CompletedTask;
var collectionService = serviceProvider.GetRequiredService<IServerDataCollector>(); var collectionService = serviceProvider.GetRequiredService<IServerDataCollector>();
var versionChecker = serviceProvider.GetRequiredService<IMasterCommunication>(); var versionChecker = serviceProvider.GetRequiredService<IMasterCommunication>();
var masterCommunicator = serviceProvider.GetRequiredService<IMasterCommunication>();
var webfrontLifetime = serviceProvider.GetRequiredService<IHostApplicationLifetime>();
using var onWebfrontErrored = new ManualResetEventSlim();
var webfrontTask = _serverManager.GetApplicationSettings().Configuration().EnableWebFront
? WebfrontCore.Program.GetWebHostTask(_serverManager.CancellationToken).ContinueWith(continuation =>
{
if (!continuation.IsFaulted)
{
return;
}
logger.LogCritical("Unable to start webfront task. {Message}",
continuation.Exception?.InnerException?.Message);
logger.LogDebug(continuation.Exception, "Unable to start webfront task");
onWebfrontErrored.Set();
})
: Task.CompletedTask;
if (_serverManager.GetApplicationSettings().Configuration().EnableWebFront)
{
try
{
onWebfrontErrored.Wait(webfrontLifetime.ApplicationStarted);
}
catch
{
// ignored when webfront successfully starts
}
if (onWebfrontErrored.IsSet)
{
return Task.CompletedTask;
}
}
// we want to run this one on a manual thread instead of letting the thread pool handle it, // we want to run this one on a manual thread instead of letting the thread pool handle it,
// because we can't exit early from waiting on console input, and it prevents us from restarting // because we can't exit early from waiting on console input, and it prevents us from restarting
@ -203,10 +238,10 @@ namespace IW4MAdmin.Application
var tasks = new[] var tasks = new[]
{ {
applicationManager.Start(),
versionChecker.CheckVersion(), versionChecker.CheckVersion(),
webfrontTask, webfrontTask,
serviceProvider.GetRequiredService<IMasterCommunication>() masterCommunicator.RunUploadStatus(_serverManager.CancellationToken),
.RunUploadStatus(_serverManager.CancellationToken),
collectionService.BeginCollectionAsync(cancellationToken: _serverManager.CancellationToken) collectionService.BeginCollectionAsync(cancellationToken: _serverManager.CancellationToken)
}; };