From ec994d51be975daf9bc590a3f4fddf9bed445d61 Mon Sep 17 00:00:00 2001 From: RaidMax Date: Mon, 13 Jan 2020 20:06:57 -0600 Subject: [PATCH] fix restart command (thanks .net upgrade) reworking a little bit of stuff to allow depedency injection to start creeping in... it's coming --- Application/ApplicationManager.cs | 32 ++++++------------- Application/Main.cs | 24 +++++++++++---- Application/Misc/MiddlewareActionHandler.cs | 34 ++++++++++++++++++--- Plugins/Tests/ManagerFixture.cs | 2 +- 4 files changed, 57 insertions(+), 35 deletions(-) diff --git a/Application/ApplicationManager.cs b/Application/ApplicationManager.cs index 7a1167d96..5d1973eea 100644 --- a/Application/ApplicationManager.cs +++ b/Application/ApplicationManager.cs @@ -42,8 +42,7 @@ namespace IW4MAdmin.Application public CancellationToken CancellationToken => _tokenSource.Token; public string ExternalIPAddress { get; private set; } public bool IsRestartRequested { get; private set; } - public IMiddlewareActionHandler MiddlewareActionHandler { get; private set; } = new MiddlewareActionHandler(); - static ApplicationManager Instance; + public IMiddlewareActionHandler MiddlewareActionHandler { get; } private readonly List Commands; private readonly List MessageTokens; private readonly ClientService ClientSvc; @@ -52,14 +51,15 @@ namespace IW4MAdmin.Application public BaseConfigurationHandler ConfigHandler; GameEventHandler Handler; readonly IPageList PageList; - readonly Dictionary Loggers = new Dictionary(); + private readonly Dictionary _loggers = new Dictionary(); private readonly MetaService _metaService; private readonly TimeSpan _throttleTimeout = new TimeSpan(0, 1, 0); private readonly CancellationTokenSource _tokenSource; private readonly Dictionary> _operationLookup = new Dictionary>(); - private ApplicationManager() + public ApplicationManager(ILogger logger, IMiddlewareActionHandler actionHandler) { + MiddlewareActionHandler = actionHandler; _servers = new ConcurrentBag(); Commands = new List(); MessageTokens = new List(); @@ -74,6 +74,7 @@ namespace IW4MAdmin.Application TokenAuthenticator = new TokenAuthentication(); _metaService = new MetaService(); _tokenSource = new CancellationTokenSource(); + _loggers.Add(0, logger); } public async Task ExecuteEvent(GameEvent newEvent) @@ -156,11 +157,6 @@ namespace IW4MAdmin.Application return Commands; } - public static ApplicationManager GetInstance() - { - return Instance ?? (Instance = new ApplicationManager()); - } - public async Task UpdateServerStates() { // store the server hash code and task for it @@ -681,7 +677,6 @@ namespace IW4MAdmin.Application { _tokenSource.Cancel(); Running = false; - Instance = null; } public void Restart() @@ -692,25 +687,16 @@ namespace IW4MAdmin.Application public ILogger GetLogger(long serverId) { - if (Loggers.ContainsKey(serverId)) + if (_loggers.ContainsKey(serverId)) { - return Loggers[serverId]; + return _loggers[serverId]; } else { - Logger newLogger; + var newLogger = new Logger($"IW4MAdmin-Server-{serverId}"); - if (serverId == 0) - { - newLogger = new Logger("IW4MAdmin-Manager"); - } - else - { - newLogger = new Logger($"IW4MAdmin-Server-{serverId}"); - } - - Loggers.Add(serverId, newLogger); + _loggers.Add(serverId, newLogger); return newLogger; } } diff --git a/Application/Main.cs b/Application/Main.cs index eda7c6fbc..0c1b31af5 100644 --- a/Application/Main.cs +++ b/Application/Main.cs @@ -1,4 +1,5 @@ using IW4MAdmin.Application.Migration; +using IW4MAdmin.Application.Misc; using Microsoft.Extensions.DependencyInjection; using SharedLibraryCore; using SharedLibraryCore.Helpers; @@ -60,7 +61,13 @@ namespace IW4MAdmin.Application restart: try { - ServerManager = ApplicationManager.GetInstance(); + var services = ConfigureServices(); + + using (var builder = services.BuildServiceProvider()) + { + ServerManager = (ApplicationManager)builder.GetRequiredService(); + } + var configuration = ServerManager.GetApplicationSettings().Configuration(); Localization.Configure.Initialize(configuration?.EnableCustomLocale ?? false ? (configuration.CustomLocale ?? "en-US") : "en-US"); @@ -70,7 +77,7 @@ namespace IW4MAdmin.Application ServerManager.Logger.WriteInfo(Utilities.CurrentLocalization.LocalizationIndex["MANAGER_VERSION"].FormatExt(Version)); - ConfigureServices(); + await CheckVersion(); await ServerManager.Init(); } @@ -235,12 +242,17 @@ namespace IW4MAdmin.Application { } } - private static void ConfigureServices() + /// + /// Configures the dependency injection services + /// + private static IServiceCollection ConfigureServices() { var serviceProvider = new ServiceCollection(); - serviceProvider.AddSingleton(ServerManager); - var builder = serviceProvider.BuildServiceProvider(); - builder.Dispose(); + serviceProvider.AddSingleton() + .AddSingleton(_serviceProvider => new Logger("IW4MAdmin-Manager")) + .AddSingleton(); + + return serviceProvider; } } } diff --git a/Application/Misc/MiddlewareActionHandler.cs b/Application/Misc/MiddlewareActionHandler.cs index 8b5a12ada..b038f23ad 100644 --- a/Application/Misc/MiddlewareActionHandler.cs +++ b/Application/Misc/MiddlewareActionHandler.cs @@ -1,15 +1,29 @@ -using SharedLibraryCore.Interfaces; +using SharedLibraryCore; +using SharedLibraryCore.Interfaces; using System; using System.Collections.Generic; -using System.Text; using System.Threading.Tasks; namespace IW4MAdmin.Application.Misc { class MiddlewareActionHandler : IMiddlewareActionHandler { - private static readonly IDictionary> _actions = new Dictionary>(); + private readonly IDictionary> _actions; + private readonly ILogger _logger; + public MiddlewareActionHandler(ILogger logger) + { + _actions = new Dictionary>(); + _logger = logger; + } + + /// + /// Executes the action with the given name + /// + /// Execution return type + /// Input value + /// Name of action to execute + /// public async Task Execute(T value, string name = null) { string key = string.IsNullOrEmpty(name) ? typeof(T).ToString() : name; @@ -22,8 +36,11 @@ namespace IW4MAdmin.Application.Misc { value = await ((IMiddlewareAction)action).Invoke(value); } - // todo: probably log this somewhere - catch { } + catch (Exception e) + { + _logger.WriteWarning($"Failed to invoke middleware action {name}"); + _logger.WriteDebug(e.GetExceptionInfo()); + } } return value; @@ -32,6 +49,13 @@ namespace IW4MAdmin.Application.Misc return value; } + /// + /// Registers an action by name + /// + /// + /// Action type specifier + /// Action to perform + /// Name of action public void Register(T actionType, IMiddlewareAction action, string name = null) { string key = string.IsNullOrEmpty(name) ? typeof(T).ToString() : name; diff --git a/Plugins/Tests/ManagerFixture.cs b/Plugins/Tests/ManagerFixture.cs index 824e46df2..353ce3f7c 100644 --- a/Plugins/Tests/ManagerFixture.cs +++ b/Plugins/Tests/ManagerFixture.cs @@ -20,7 +20,7 @@ namespace Tests File.WriteAllText(logFile, Environment.NewLine); - Manager = ApplicationManager.GetInstance(); + Manager = null; var config = new ApplicationConfiguration {