1
0
mirror of https://github.com/RaidMax/IW4M-Admin.git synced 2025-06-08 06:08:20 -05:00

Reduce possibility of race condition reading updated config

This commit is contained in:
RaidMax 2024-06-23 16:13:30 -05:00
parent dbb5a9117a
commit f8f6ca2c0d
2 changed files with 16 additions and 14 deletions

View File

@ -26,6 +26,7 @@ using Data.Abstractions;
using Data.Context; using Data.Context;
using Data.Models; using Data.Models;
using IW4MAdmin.Application.Configuration; using IW4MAdmin.Application.Configuration;
using IW4MAdmin.Application.IO;
using IW4MAdmin.Application.Migration; using IW4MAdmin.Application.Migration;
using IW4MAdmin.Application.Plugin.Script; using IW4MAdmin.Application.Plugin.Script;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
@ -68,6 +69,7 @@ namespace IW4MAdmin.Application
private readonly ClientService ClientSvc; private readonly ClientService ClientSvc;
readonly PenaltyService PenaltySvc; readonly PenaltyService PenaltySvc;
private readonly IAlertManager _alertManager; private readonly IAlertManager _alertManager;
private readonly ConfigurationWatcher _watcher;
public IConfigurationHandler<ApplicationConfiguration> ConfigHandler; public IConfigurationHandler<ApplicationConfiguration> ConfigHandler;
readonly IPageList PageList; readonly IPageList PageList;
private readonly TimeSpan _throttleTimeout = new TimeSpan(0, 1, 0); private readonly TimeSpan _throttleTimeout = new TimeSpan(0, 1, 0);
@ -94,7 +96,8 @@ namespace IW4MAdmin.Application
IEnumerable<IPlugin> plugins, IParserRegexFactory parserRegexFactory, IEnumerable<IRegisterEvent> customParserEvents, IEnumerable<IPlugin> plugins, IParserRegexFactory parserRegexFactory, IEnumerable<IRegisterEvent> customParserEvents,
ICoreEventHandler coreEventHandler, IScriptCommandFactory scriptCommandFactory, IDatabaseContextFactory contextFactory, ICoreEventHandler coreEventHandler, IScriptCommandFactory scriptCommandFactory, IDatabaseContextFactory contextFactory,
IMetaRegistration metaRegistration, IScriptPluginServiceResolver scriptPluginServiceResolver, ClientService clientService, IServiceProvider serviceProvider, IMetaRegistration metaRegistration, IScriptPluginServiceResolver scriptPluginServiceResolver, ClientService clientService, IServiceProvider serviceProvider,
ChangeHistoryService changeHistoryService, ApplicationConfiguration appConfig, PenaltyService penaltyService, IAlertManager alertManager, IInteractionRegistration interactionRegistration, IEnumerable<IPluginV2> v2PLugins) ChangeHistoryService changeHistoryService, ApplicationConfiguration appConfig, PenaltyService penaltyService, IAlertManager alertManager, IInteractionRegistration interactionRegistration, IEnumerable<IPluginV2> v2PLugins,
ConfigurationWatcher watcher)
{ {
MiddlewareActionHandler = actionHandler; MiddlewareActionHandler = actionHandler;
_servers = new ConcurrentBag<Server>(); _servers = new ConcurrentBag<Server>();
@ -102,6 +105,7 @@ namespace IW4MAdmin.Application
ClientSvc = clientService; ClientSvc = clientService;
PenaltySvc = penaltyService; PenaltySvc = penaltyService;
_alertManager = alertManager; _alertManager = alertManager;
_watcher = watcher;
ConfigHandler = appConfigHandler; ConfigHandler = appConfigHandler;
StartTime = DateTime.UtcNow; StartTime = DateTime.UtcNow;
PageList = new PageList(); PageList = new PageList();
@ -529,6 +533,7 @@ namespace IW4MAdmin.Application
Console.WriteLine(_translationLookup["MANAGER_COMMUNICATION_INFO"]); Console.WriteLine(_translationLookup["MANAGER_COMMUNICATION_INFO"]);
await InitializeServers(); await InitializeServers();
_watcher.Enable();
IsInitialized = true; IsInitialized = true;
} }

View File

@ -20,7 +20,6 @@ public sealed class ConfigurationWatcher : IDisposable
}; };
_watcher.Changed += WatcherOnChanged; _watcher.Changed += WatcherOnChanged;
_watcher.EnableRaisingEvents = true;
} }
public void Dispose() public void Dispose()
@ -31,30 +30,28 @@ public sealed class ConfigurationWatcher : IDisposable
public void Register(string fileName, Action<string> fileUpdated) public void Register(string fileName, Action<string> fileUpdated)
{ {
if (_registeredActions.ContainsKey(fileName)) _registeredActions.TryAdd(fileName, fileUpdated);
{
return;
}
_registeredActions.Add(fileName, fileUpdated);
} }
public void Unregister(string fileName) public void Unregister(string fileName)
{ {
if (_registeredActions.ContainsKey(fileName)) _registeredActions.Remove(fileName);
{ }
_registeredActions.Remove(fileName);
} public void Enable()
{
_watcher.EnableRaisingEvents = true;
} }
private void WatcherOnChanged(object sender, FileSystemEventArgs eventArgs) private void WatcherOnChanged(object sender, FileSystemEventArgs eventArgs)
{ {
if (!_registeredActions.ContainsKey(eventArgs.FullPath) || eventArgs.ChangeType != WatcherChangeTypes.Changed || if (!_registeredActions.TryGetValue(eventArgs.FullPath, out var value) ||
eventArgs.ChangeType != WatcherChangeTypes.Changed ||
new FileInfo(eventArgs.FullPath).Length == 0) new FileInfo(eventArgs.FullPath).Length == 0)
{ {
return; return;
} }
_registeredActions[eventArgs.FullPath].Invoke(eventArgs.FullPath); value.Invoke(eventArgs.FullPath);
} }
} }