From 34e476d5b0ea639d57daabb39a40cbd618792664 Mon Sep 17 00:00:00 2001
From: Niko Ehrenfeuchter <nikolaus.ehrenfeuchter@unibas.ch>
Date: Fri, 2 Mar 2018 15:37:40 +0100
Subject: [PATCH] Clean up processing location if moving a new candidate fails.

If a new directory is found in incoming but the move operation to
processing failed for some reason, the timestamp and username
directories (and potentially some partially moved data?) would remain
in processing. To prevent confusion about those "incomplete" transfer
candidates they are moved to the "ERROR" location.
---
 ATxCommon/FsUtils.cs                     |  1 +
 ATxCommon/Serializables/ServiceConfig.cs |  7 +++++++
 ATxService/AutoTx.cs                     | 14 ++++++++++++++
 3 files changed, 22 insertions(+)

diff --git a/ATxCommon/FsUtils.cs b/ATxCommon/FsUtils.cs
index a5ac9a2..865bfc5 100644
--- a/ATxCommon/FsUtils.cs
+++ b/ATxCommon/FsUtils.cs
@@ -224,6 +224,7 @@ namespace ATxCommon
             retval &= CheckForDirectory(Path.Combine(managed, "PROCESSING"));
             retval &= CheckForDirectory(Path.Combine(managed, "DONE"));
             retval &= CheckForDirectory(Path.Combine(managed, "UNMATCHED"));
+            retval &= CheckForDirectory(Path.Combine(managed, "ERROR"));
             return retval;
         }
 
diff --git a/ATxCommon/Serializables/ServiceConfig.cs b/ATxCommon/Serializables/ServiceConfig.cs
index c7e622a..386ba4a 100644
--- a/ATxCommon/Serializables/ServiceConfig.cs
+++ b/ATxCommon/Serializables/ServiceConfig.cs
@@ -226,6 +226,13 @@ namespace ATxCommon.Serializables
         [XmlIgnore]
         public string UnmatchedPath => Path.Combine(ManagedPath, "UNMATCHED");
 
+        /// <summary>
+        /// The full path to the directory for directories that were moved out of the way due to
+        /// any kind of error in processing them.
+        /// </summary>
+        [XmlIgnore]
+        public string ErrorPath => Path.Combine(ManagedPath, "ERROR");
+
         #endregion
 
 
diff --git a/ATxService/AutoTx.cs b/ATxService/AutoTx.cs
index 58bd70b..b85f3c6 100644
--- a/ATxService/AutoTx.cs
+++ b/ATxService/AutoTx.cs
@@ -752,6 +752,20 @@ namespace ATxService
                     return true;
 
                 errMsg = $"unable to move [{userDir.FullName}]";
+                // just to be safe, don't delete the failed directory from the processing location
+                // but move it to the error location (in case something has in fact been moved
+                // there already we have to make sure not to kill it):
+                var moveFromProcessing = new DirectoryInfo(Path.Combine(processingPath, timeStamp));
+                try {
+                    moveFromProcessing.MoveTo(Path.Combine(_config.ErrorPath, timeStamp));
+                    Log.Debug("Moved failed directory [{0}] out of processing to [{1}].",
+                        moveFromProcessing.FullName, _config.ErrorPath);
+                }
+                catch (Exception ex) {
+                    Log.Error("Moving [{0}] to [{1}] failed: {2}",
+                        moveFromProcessing.FullName, _config.ErrorPath, ex.Message);
+                    throw;
+                }
             }
             catch (Exception ex) {
                 errMsg = ex.Message;
-- 
GitLab