private static void ProcessQueue()
{
// Prevent 2 Import Tasks from processing at once
lock (ThisLock)
{
// Make sure we arent already processing
if (ImportTask != null && ImportTask.Status == TaskStatus.Running)
return;
// Do this in another thread
ImportTask = Task.Run(() =>
{
// Fire event
if (SnapshotReceived != null)
SnapshotReceived();
// Loop through all of the snapshots and process em
while (SnapshotQueue.Count > 0)
{
// Fetch our snapshot
Snapshot Snap;
if (!SnapshotQueue.TryDequeue(out Snap))
throw new Exception("Unable to Dequeue Snapshot Item");
// Attempt to process the snapshot
bool WasSuccess = false;
try
{
Snap.ProcessData();
WasSuccess = true;
SnapshotsCompleted++;
// Fire event
if (SnapshotProcessed != null)
SnapshotProcessed();
// Alert user
Notify.Show("Snapshot Processed Successfully!", "From Server IP: " + Snap.ServerIp, AlertType.Success);
}
catch (Exception E)
{
HttpServer.AspStatsLog.Write("ERROR: [SnapshotProcess] " + E.Message + " @ " + E.TargetSite);
ExceptionHandler.GenerateExceptionLog(E);
}
// Create backup of snapshot
try
{
// Backup the snapshot
string FileName = Snap.ServerPrefix + "-" + Snap.MapName + "_" + Snap.RoundEndDate.ToLocalTime().ToString("yyyyMMdd_HHmm") + ".txt";
File.AppendAllText(
(WasSuccess) ? Path.Combine(Paths.SnapshotProcPath, FileName) : Path.Combine(Paths.SnapshotTempPath, FileName),
Snap.DataString
);
}
catch (Exception E)
{
HttpServer.AspStatsLog.Write("WARNING: [SnapshotFileOperations] Unable to create Snapshot Backup File: " + E.Message);
}
}
})
.ContinueWith((Action<Task>)delegate
{
// Create an exception log if we failed to import correctly
if (ImportTask.IsFaulted)
{
Program.ErrorLog.Write("ERROR: [SnapshotQueue] Failed to process all snapshots in Queue: " + ImportTask.Exception.Message);
ExceptionHandler.GenerateExceptionLog(ImportTask.Exception);
}
});
}
}