From 0188c4a50d07e7b6a482df3bb5765dca4479c84a Mon Sep 17 00:00:00 2001 From: CWX Date: Wed, 22 Jan 2025 13:33:28 +0000 Subject: [PATCH] init dict before accessing --- Libraries/Core/Utils/App.cs | 63 +++++++++++++++++++++---------------- 1 file changed, 36 insertions(+), 27 deletions(-) diff --git a/Libraries/Core/Utils/App.cs b/Libraries/Core/Utils/App.cs index 0146dd3f..ba9a63e2 100644 --- a/Libraries/Core/Utils/App.cs +++ b/Libraries/Core/Utils/App.cs @@ -11,7 +11,8 @@ namespace Core.Utils; [Injectable(InjectionType.Singleton)] public class App { - protected Dictionary _onUpdateLastRun; + protected Dictionary _onUpdateLastRun = new Dictionary(); + protected Timer _timer; protected CoreConfig _coreConfig; protected ISptLogger _logger; @@ -78,7 +79,7 @@ public class App foreach (var onLoad in _onLoad) await onLoad.OnLoad(); - new Timer(_ => Update(_onUpdate), null, TimeSpan.Zero, TimeSpan.FromMilliseconds(5000)); + _ = new Timer(_ => Update(_onUpdate), null, TimeSpan.Zero, TimeSpan.FromMilliseconds(5000)); _logger.Success(GetRandomisedStartMessage()); @@ -94,39 +95,47 @@ public class App return _localisationService.GetText("server_start_success"); } - protected async Task Update(IEnumerable onUpdateComponents) + protected void Update(IEnumerable onUpdateComponents) { - // If the server has failed to start, skip any update calls - if (!_httpServer.IsStarted() || !_databaseService.IsDatabaseValid()) return; - - foreach (var updateable in onUpdateComponents) + try { - var success = false; - if (!_onUpdateLastRun.TryGetValue(updateable.GetRoute(), out var lastRunTimeTimestamp)) - lastRunTimeTimestamp = 0; - var secondsSinceLastRun = _timeUtil.GetTimeStamp() - lastRunTimeTimestamp; + // If the server has failed to start, skip any update calls + if (!_httpServer.IsStarted() || !_databaseService.IsDatabaseValid()) return; - try + foreach (var updateable in onUpdateComponents) { - success = await updateable.OnUpdate(secondsSinceLastRun); - } - catch (Exception err) - { - LogUpdateException(err, updateable); - } + var success = false; + if (!_onUpdateLastRun.TryGetValue(updateable.GetRoute(), out var lastRunTimeTimestamp)) + lastRunTimeTimestamp = 0; + var secondsSinceLastRun = _timeUtil.GetTimeStamp() - lastRunTimeTimestamp; - if (success) - { - _onUpdateLastRun[updateable.GetRoute()] = _timeUtil.GetTimeStamp(); - } - else - { - /* temporary for debug */ - var warnTime = 20 * 60; + try + { + success = updateable.OnUpdate(secondsSinceLastRun); + } + catch (Exception err) + { + LogUpdateException(err, updateable); + } - if (secondsSinceLastRun % warnTime == 0) _logger.Debug(_localisationService.GetText("route_onupdate_no_response", updateable.GetRoute())); + if (success) + { + _onUpdateLastRun[updateable.GetRoute()] = _timeUtil.GetTimeStamp(); + } + else + { + /* temporary for debug */ + var warnTime = 20 * 60; + + if (secondsSinceLastRun % warnTime == 0) _logger.Debug(_localisationService.GetText("route_onupdate_no_response", updateable.GetRoute())); + } } } + catch (Exception e) + { + Console.WriteLine(e); + throw; + } } protected void LogUpdateException(Exception err, OnUpdate updateable)