fix restart command (thanks .net upgrade)

reworking a little bit of stuff to allow depedency injection to start creeping in... it's coming
This commit is contained in:
RaidMax 2020-01-13 20:06:57 -06:00
parent 9be7bafc53
commit ec994d51be
4 changed files with 57 additions and 35 deletions

View File

@ -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<Command> Commands;
private readonly List<MessageToken> MessageTokens;
private readonly ClientService ClientSvc;
@ -52,14 +51,15 @@ namespace IW4MAdmin.Application
public BaseConfigurationHandler<ApplicationConfiguration> ConfigHandler;
GameEventHandler Handler;
readonly IPageList PageList;
readonly Dictionary<long, ILogger> Loggers = new Dictionary<long, ILogger>();
private readonly Dictionary<long, ILogger> _loggers = new Dictionary<long, ILogger>();
private readonly MetaService _metaService;
private readonly TimeSpan _throttleTimeout = new TimeSpan(0, 1, 0);
private readonly CancellationTokenSource _tokenSource;
private readonly Dictionary<string, Task<IList>> _operationLookup = new Dictionary<string, Task<IList>>();
private ApplicationManager()
public ApplicationManager(ILogger logger, IMiddlewareActionHandler actionHandler)
{
MiddlewareActionHandler = actionHandler;
_servers = new ConcurrentBag<Server>();
Commands = new List<Command>();
MessageTokens = new List<MessageToken>();
@ -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;
}
}

View File

@ -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<IManager>();
}
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()
/// <summary>
/// Configures the dependency injection services
/// </summary>
private static IServiceCollection ConfigureServices()
{
var serviceProvider = new ServiceCollection();
serviceProvider.AddSingleton<IManager>(ServerManager);
var builder = serviceProvider.BuildServiceProvider();
builder.Dispose();
serviceProvider.AddSingleton<IManager, ApplicationManager>()
.AddSingleton<ILogger>(_serviceProvider => new Logger("IW4MAdmin-Manager"))
.AddSingleton<IMiddlewareActionHandler, MiddlewareActionHandler>();
return serviceProvider;
}
}
}

View File

@ -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<string, IList<object>> _actions = new Dictionary<string, IList<object>>();
private readonly IDictionary<string, IList<object>> _actions;
private readonly ILogger _logger;
public MiddlewareActionHandler(ILogger logger)
{
_actions = new Dictionary<string, IList<object>>();
_logger = logger;
}
/// <summary>
/// Executes the action with the given name
/// </summary>
/// <typeparam name="T">Execution return type</typeparam>
/// <param name="value">Input value</param>
/// <param name="name">Name of action to execute</param>
/// <returns></returns>
public async Task<T> Execute<T>(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<T>)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;
}
/// <summary>
/// Registers an action by name
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="actionType">Action type specifier</param>
/// <param name="action">Action to perform</param>
/// <param name="name">Name of action</param>
public void Register<T>(T actionType, IMiddlewareAction<T> action, string name = null)
{
string key = string.IsNullOrEmpty(name) ? typeof(T).ToString() : name;

View File

@ -20,7 +20,7 @@ namespace Tests
File.WriteAllText(logFile, Environment.NewLine);
Manager = ApplicationManager.GetInstance();
Manager = null;
var config = new ApplicationConfiguration
{