Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy is my XDocument saving an incomplete file?
    primarykey
    data
    text
    <p>I have a program that runs a series of plugins using threads. During this process, it writes the runtimes of the plugins to an XDocument so that the program knows how long it's been since the last time that plugin was run. I'm having a problem, though. About once per day (at unpredictable times) when I load the XDocument I get the following error: <br/><br/> <code>[04/01/2013 08:17:10.083] Unexpected end of file has occurred. The following elements are not closed: Database, DatabaseList. Line 4043, position 1.</code></p> <p>I ran some trace statements and found out that the time before that, the service failed with about 44 plugins left to run, which apparently caused the XDocument to close without writing the end of the file. It writes to and reads from an XML file on the hard disk, but it performs all operations on the XDocument in memory because I use Linq to do complex operations on the data. </p> <p>Does anybody know why this might happen? How can I load my XDocument so that it won't wreck the actual file if something happens during the running process? </p> <p>EDIT: Here's a sample of the code that utilizes the <code>XDocument</code> (named <code>XDoc</code>):</p> <pre><code> private void RunPlugin(object oQueuedPlugin) { PluginState oPluginState = (PluginState)oQueuedPlugin; PluginResponse oResponse = new PluginResponse(); XElement xPlugin; lock (xDoc) { xPlugin = GetPluginNode(oPluginState.ClientFusionDatabase.Name, oPluginState.Plugin.Name); } if (xPlugin == null) { API.Log.Write("ActivityTrace.ShowXMLLog", "XML for " + oPluginState.ClientFusionDatabase.Name + " was null."); XElement NewPlugin = new XElement("Plugin", new XAttribute("PluginName", oPluginState.Plugin.Name), new XAttribute("Running", "true"), new XAttribute("LastStart", DateTime.Now.ToString()), new XAttribute("LastSuccess", ""), new XAttribute("LastExitStatus","")); lock (xDoc) { var Location = from database in xDoc.Root.Elements("Database") where database.Attribute("DatabaseName").Value == oPluginState.ClientFusionDatabase.Name select database; Location.FirstOrDefault().Add(NewPlugin); xDoc.Save(XmlLogFilePath); } oResponse = oPluginState.Plugin.Run(oPluginState.ClientFusionDatabase); if (oResponse == null) { API.Log.Write("ActivityTrace.ShowNullReturnLog", oPluginState.ClientFusionDatabase.Name + "- " + oPluginState.Plugin.Name + " returned null."); } lock (xDoc) { NewPlugin.Attribute("Running").Value = "false"; NewPlugin.Attribute("LastExitStatus").Value = oResponse.ResponseType.ToString(); if (oResponse.ResponseType == PluginResponseTypes.Success || oResponse.ResponseType == PluginResponseTypes.Warning) NewPlugin.Attribute("LastSuccess").Value = DateTime.Now.ToString(); xDoc.Save(XmlLogFilePath); } API.Log.Write("ActivityTrace.ShowXMLLog","Completed " + oPluginState.ClientFusionDatabase.Name + "- " + oPluginState.Plugin.Name + " with XML " + NewPlugin.ToString()); API.Log.Write(oPluginState.Plugin.Name, "(" + oPluginState.ClientFusionDatabase.Connection.Database + " = " + (oResponse.ResponseType + ") ").PadRight(9) + "EXIT MESSAGE: " + (string.IsNullOrEmpty(oResponse.Message) ? "None" : oResponse.Message)); } else { DateTime dLastRun = (DateTime)xPlugin.Attribute("LastStart"); bool bRunning = (bool)xPlugin.Attribute("Running"); if ((DateTime.Now - dLastRun) &gt; oPluginState.Plugin.Interval &amp;&amp; !bRunning) { lock (xDoc) { xPlugin.Attribute("LastStart").Value = DateTime.Now.ToString(); xPlugin.Attribute("Running").Value = "true"; xDoc.Save(XmlLogFilePath); } oResponse = oPluginState.Plugin.Run(oPluginState.ClientFusionDatabase); lock (xDoc) { xPlugin.Attribute("Running").Value = "false"; xPlugin.Attribute("LastExitStatus").Value = oResponse.ResponseType.ToString(); if (oResponse.ResponseType == PluginResponseTypes.Success || oResponse.ResponseType == PluginResponseTypes.Warning) xPlugin.Attribute("LastSuccess").Value = DateTime.Now.ToString(); xDoc.Save(XmlLogFilePath); } API.Log.Write(oPluginState.Plugin.Name, "(" + oPluginState.ClientFusionDatabase.Connection.Database + " = " + (oResponse.ResponseType + ") ").PadRight(9) + "EXIT MESSAGE: " + (string.IsNullOrEmpty(oResponse.Message) ? "None" : oResponse.Message)); } else if (bRunning) API.Log.Write(oPluginState.Plugin.Name, "(" + oPluginState.ClientFusionDatabase.Connection.Database + " = " + ("SKIPPED) ").PadRight(9) + "REASON: Plugin already running"); } oPluginState.Complete = true; } </code></pre> <p>The problem is that one or more of the plugins is not handling an error correctly, which causes it to not return any response and crash the program.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload