Skip to content
Permalink
Browse files

Logging reason when xml data files cannot be read and added at least …

…one retry - in order to better guard against / identify 3rd party processes intefering with file access.
  • Loading branch information...
mawtex committed Apr 4, 2019
1 parent dbb189e commit 0efd3845e1a657a06cb84cd7e041b9bfb6a78570
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
@@ -269,24 +269,28 @@ private static FileRecord LoadFileRecordFromDisk(string filePath, string element

IList<C1FileInfo> candidateFiles = GetCandidateFiles(filePath);

string errorCause = "";

foreach (C1FileInfo candidateFile in candidateFiles)
{
bool tryLoad = true;
bool retriedLoad = false;

while (tryLoad && dataDocument == null)
{
dataDocument = TryLoad(candidateFile);
dataDocument = TryLoad(candidateFile, ref errorCause);

if (dataDocument == null)
{
if ((DateTime.Now - candidateFile.LastWriteTime).TotalSeconds > 30)
if (retriedLoad && (DateTime.Now - candidateFile.LastWriteTime).TotalSeconds > 30)
{
tryLoad = false;
tryLoad = false;
}
else
{
Thread.Sleep(250); // other processes/servers may be writing to this file at the moment. Patience young padawan!
}
retriedLoad = true;
}
}

@@ -300,7 +304,7 @@ private static FileRecord LoadFileRecordFromDisk(string filePath, string element
if (dataDocument == null)
{
dataDocument = new XDocument(new XElement("fallback"));
Log.LogWarning(LogTitle, "Did not find a healthy XML document for '{0}' - creating an empty store.", filePath);
Log.LogWarning(LogTitle, "Did not find a healthy XML document for '{0}' - creating an empty store. {1}", filePath, errorCause);
}

List<XElement> elements = ExtractElements(dataDocument);
@@ -350,7 +354,7 @@ private static FileRecord LoadFileRecordFromDisk(string filePath, string element



private static XDocument TryLoad(C1FileInfo candidateFile)
private static XDocument TryLoad(C1FileInfo candidateFile, ref string errorCause)
{
XDocument dataDocument = null;
try
@@ -362,8 +366,9 @@ private static XDocument TryLoad(C1FileInfo candidateFile)
dataDocument = XDocument.Load(xmlReader);
}
}
catch (Exception)
catch (Exception ex)
{
errorCause = ex.Message;
// broken file - should not stop us...
}

0 comments on commit 0efd384

Please sign in to comment.
You can’t perform that action at this time.