From 3d8e78752741f9a1aee44007a7806a4963694cfa Mon Sep 17 00:00:00 2001
From: Niko Ehrenfeuchter <nikolaus.ehrenfeuchter@unibas.ch>
Date: Tue, 9 Jan 2018 13:35:43 +0100
Subject: [PATCH] Send an email on expired folders in the grace location.

An email will be sent only if the time since the last one exceeds the
new configuration option "GraceNotificationDelta", defaulting to 12
hours.

Fixes #1
---
 AutoTx/AutoTx.cs                           | 14 +++++++++++---
 AutoTx/Resources/configuration-example.xml |  4 ++++
 AutoTx/XmlWrapper/ServiceConfig.cs         |  7 +++++--
 AutoTx/XmlWrapper/ServiceStatus.cs         | 14 +++++++++++++-
 4 files changed, 33 insertions(+), 6 deletions(-)

diff --git a/AutoTx/AutoTx.cs b/AutoTx/AutoTx.cs
index 4608042..c069730 100644
--- a/AutoTx/AutoTx.cs
+++ b/AutoTx/AutoTx.cs
@@ -931,9 +931,10 @@ namespace AutoTx
         /// <summary>
         /// Generate a report on expired folders in the grace location.
         /// 
-        /// Check all user-directories in the grace location for subdirectories whose
-        /// name-timestamp exceeds the configured grace period and generate a summary
-        /// containing the age and size of those directories.
+        /// Check all user-directories in the grace location for subdirectories whose timestamp
+        /// (the directory name) exceeds the configured grace period and generate a summary
+        /// containing the age and size of those directories. The summary will be sent to the admin
+        /// if the configured GraceNotificationDelta has passed since the last email.
         /// </summary>
         /// <param name="threshold">The number of days used as expiration threshold.</param>
         public string GraceLocationSummary(int threshold) {
@@ -949,6 +950,13 @@ namespace AutoTx
             if (string.IsNullOrEmpty(report))
                 return "";
             report = "Expired folders in grace location:\n" + report;
+            var delta = (int)(DateTime.Now - _status.LastGraceNotification).TotalMinutes;
+            report += "\nTime since last grace notification: " + delta + "\n";
+            if (delta >= _config.GraceNotificationDelta) {
+                SendAdminEmail(report, "Grace location cleanup required.");
+                _status.LastGraceNotification = DateTime.Now;
+                report += "\nNotification sent to AdminEmailAdress.\n";
+            }
             return report;
         }
 
diff --git a/AutoTx/Resources/configuration-example.xml b/AutoTx/Resources/configuration-example.xml
index 8917bec..ec6bc14 100644
--- a/AutoTx/Resources/configuration-example.xml
+++ b/AutoTx/Resources/configuration-example.xml
@@ -87,6 +87,10 @@
          messages to, e.g. on completed transfers. Can be empty. -->
     <AdminDebugEmailAdress>admin@mydomain.xy</AdminDebugEmailAdress>
 
+    <!-- GraceNotificationDelta: minimum time (in minutes) between two emails
+         about expired folders in the grace location (default: 720 (12h)) -->
+    <GraceNotificationDelta>720</GraceNotificationDelta>
+
     <!-- InterPacketGap: RoboCopy parameter to limit the bandwidth -->
     <InterPacketGap>0</InterPacketGap>
 
diff --git a/AutoTx/XmlWrapper/ServiceConfig.cs b/AutoTx/XmlWrapper/ServiceConfig.cs
index dd70309..0006b1f 100644
--- a/AutoTx/XmlWrapper/ServiceConfig.cs
+++ b/AutoTx/XmlWrapper/ServiceConfig.cs
@@ -24,6 +24,7 @@ namespace AutoTx.XmlWrapper
             EmailPrefix = "";
             AdminEmailAdress = "";
             AdminDebugEmailAdress = "";
+            GraceNotificationDelta = 720;
 
             InterPacketGap = 0;
 
@@ -116,6 +117,8 @@ namespace AutoTx.XmlWrapper
         public string AdminEmailAdress { get; set; }
         public string AdminDebugEmailAdress { get; set; }
 
+        public int GraceNotificationDelta { get; set; }
+
         public int InterPacketGap { get; set; }
 
         /// <summary>
@@ -212,8 +215,8 @@ namespace AutoTx.XmlWrapper
                     "AdminEmailAdress: " + AdminEmailAdress + "\n" +
                     "AdminDebugEmailAdress: " + AdminDebugEmailAdress + "\n" +
                     "StorageNotificationDelta: " + StorageNotificationDelta + "\n" +
-                    "AdminNotificationDelta: " + AdminNotificationDelta + "\n";
-
+                    "AdminNotificationDelta: " + AdminNotificationDelta + "\n" +
+                    "GraceNotificationDelta: " + GraceNotificationDelta + "\n";
             }
             return msg;
         }
diff --git a/AutoTx/XmlWrapper/ServiceStatus.cs b/AutoTx/XmlWrapper/ServiceStatus.cs
index 25f4ed8..50c2543 100644
--- a/AutoTx/XmlWrapper/ServiceStatus.cs
+++ b/AutoTx/XmlWrapper/ServiceStatus.cs
@@ -14,6 +14,7 @@ namespace AutoTx.XmlWrapper
         private DateTime _lastStatusUpdate;
         private DateTime _lastStorageNotification;
         private DateTime _lastAdminNotification;
+        private DateTime _lastGraceNotification;
 
         private string _limitReason;
         string _currentTransferSrc;
@@ -49,6 +50,15 @@ namespace AutoTx.XmlWrapper
             }
         }
 
+        [XmlElement("LastGraceNotification", DataType = "dateTime")]
+        public DateTime LastGraceNotification {
+            get { return _lastGraceNotification; }
+            set {
+                _lastGraceNotification = value;
+                Serialize();
+            }
+        }
+
         public string LimitReason {
             get { return _limitReason; }
             set {
@@ -209,7 +219,9 @@ namespace AutoTx.XmlWrapper
                 "LastStorageNotification: " +
                 LastStorageNotification.ToString("yyyy-MM-dd HH:mm:ss") + "\n" +
                 "LastAdminNotification: " +
-                LastAdminNotification.ToString("yyyy-MM-dd HH:mm:ss") + "\n";
+                LastAdminNotification.ToString("yyyy-MM-dd HH:mm:ss") + "\n" +
+                "LastGraceNotification: " +
+                LastGraceNotification.ToString("yyyy-MM-dd HH:mm:ss") + "\n";
         }
     }
 }
-- 
GitLab