Fix possible exception when PTM last run file is being written

This commit is contained in:
djbadders 2021-08-08 17:29:37 +01:00
parent 4724f938f1
commit 9c9b6c5749
1 changed files with 178 additions and 161 deletions

View File

@ -1,5 +1,6 @@
using System; using System;
using System.IO; using System.IO;
using System.Threading;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.RazorPages; using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
@ -79,7 +80,23 @@ namespace Monitor._Internal
IServiceProvider logProvider = ServiceHelper.BuildLoggerService(PTMagicBasePath); IServiceProvider logProvider = ServiceHelper.BuildLoggerService(PTMagicBasePath);
Log = logProvider.GetRequiredService<LogHelper>(); Log = logProvider.GetRequiredService<LogHelper>();
bool exitLoop = false;
while (!exitLoop)
{
try
{
// Try to read the current runtime summary, but may be being written, so retry if necessary.
Summary = JsonConvert.DeserializeObject<Summary>(System.IO.File.ReadAllText(PTMagicBasePath + Constants.PTMagicPathData + Path.DirectorySeparatorChar + "LastRuntimeSummary.json")); Summary = JsonConvert.DeserializeObject<Summary>(System.IO.File.ReadAllText(PTMagicBasePath + Constants.PTMagicPathData + Path.DirectorySeparatorChar + "LastRuntimeSummary.json"));
exitLoop = true;
}
catch (IOException e)
{
// Squash exception and try again, as file was locked.
Thread.Sleep(250);
}
}
if (Summary.CurrentGlobalSetting == null) Summary.CurrentGlobalSetting = PTMagicConfiguration.AnalyzerSettings.GlobalSettings.Find(s => s.SettingName.IndexOf("default", StringComparison.InvariantCultureIgnoreCase) > -1); if (Summary.CurrentGlobalSetting == null) Summary.CurrentGlobalSetting = PTMagicConfiguration.AnalyzerSettings.GlobalSettings.Find(s => s.SettingName.IndexOf("default", StringComparison.InvariantCultureIgnoreCase) > -1);
MainFiatCurrencySymbol = SystemHelper.GetCurrencySymbol(Summary.MainFiatCurrency); MainFiatCurrencySymbol = SystemHelper.GetCurrencySymbol(Summary.MainFiatCurrency);