diff --git a/ATXCommon/FsUtils.cs b/ATXCommon/FsUtils.cs index 3f537683accdbc0cbe077b6bf07c2c255725a1e7..a7192a84efaa5451ccdc372837306453edaea98b 100644 --- a/ATXCommon/FsUtils.cs +++ b/ATXCommon/FsUtils.cs @@ -122,6 +122,31 @@ namespace ATXCommon return collection; } + /// <summary> + /// Generate a report on expired folders in the grace location. + /// + /// 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. + /// </summary> + /// <param name="graceLocation">The location to scan for expired folders.</param> + /// <param name="threshold">The number of days used as expiration threshold.</param> + public static string GraceLocationSummary(DirectoryInfo graceLocation, int threshold) { + var expired = ExpiredDirs(graceLocation, threshold); + var report = ""; + foreach (var userdir in expired.Keys) { + report += "\n - user '" + userdir + "'\n"; + foreach (var subdir in expired[userdir]) { + report += string.Format(" - {0} [age: {2} days, size: {1}]\n", + subdir.Item1, Conv.BytesToString(subdir.Item2), subdir.Item3); + } + } + if (string.IsNullOrEmpty(report)) + return ""; + + return "Expired folders in grace location:\n" + report; + } + /// <summary> /// Check if a given directory is empty. If a marker file is set in the config a /// file with this name will be created inside the given directory and will be diff --git a/AutoTx/AutoTx.cs b/AutoTx/AutoTx.cs index f5b4eb873d87e9e757a621cc34348f94d1dfc127..e3132f6d499c296c6a9ebe45ddf65ac98a3af482 100644 --- a/AutoTx/AutoTx.cs +++ b/AutoTx/AutoTx.cs @@ -300,7 +300,7 @@ namespace AutoTx msg += "\n------ Grace location status (threshold: " + _config.GracePeriod + ") ------\n"; try { - var tmp = GraceLocationSummary(_config.GracePeriod); + var tmp = SendGraceLocationSummary(_config.GracePeriod); if (string.IsNullOrEmpty(tmp)) { msg += " -- NO EXPIRED folders in grace location! --\n"; } else { @@ -721,7 +721,7 @@ namespace AutoTx sourceDirectory.Parent.Delete(); // check age and size of existing folders in the grace location after // a transfer has completed, trigger a notification if necessary: - Log.Debug(GraceLocationSummary(_config.GracePeriod)); + Log.Debug(SendGraceLocationSummary(_config.GracePeriod)); return; } errMsg = "unable to move " + sourceDirectory.FullName; @@ -761,39 +761,6 @@ namespace AutoTx _lastUserDirCheck = DateTime.Now; } - /// <summary> - /// Generate a report on expired folders in the grace location. - /// - /// 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) { - var doneDir = new DirectoryInfo(Path.Combine(_managedPath, "DONE")); - var expired = FsUtils.ExpiredDirs(doneDir, threshold); - var report = ""; - foreach (var userdir in expired.Keys) { - report += "\n - user '" + userdir + "'\n"; - foreach (var subdir in expired[userdir]) { - report += string.Format(" - {0} [age: {2} days, size: {1}]\n", - subdir.Item1, Conv.BytesToString(subdir.Item2), subdir.Item3); - } - } - if (string.IsNullOrEmpty(report)) - return ""; - report = "Expired folders in grace location:\n" + report; - var delta = TimeUtils.MinutesSince(_status.LastGraceNotification); - 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; - } - #endregion } diff --git a/AutoTx/Email.cs b/AutoTx/Email.cs index f2b46894bfb03f83e2aad3908066b30c64bc9716..fd22c8408fbac52ef68393697370be1d14f03c66 100644 --- a/AutoTx/Email.cs +++ b/AutoTx/Email.cs @@ -218,5 +218,29 @@ namespace AutoTx SendAdminEmail(msg); } } + + /// <summary> + /// Send a report on expired folders in the grace location if applicable. + /// + /// Create a summary of expired folders and send it to the admin address + /// if the configured GraceNotificationDelta has passed since the last email. + /// </summary> + /// <param name="threshold">The number of days used as expiration threshold.</param> + /// <returns>The summary report, empty if no expired folders exist.</returns> + private string SendGraceLocationSummary(int threshold) { + var report = FsUtils.GraceLocationSummary( + new DirectoryInfo(Path.Combine(_managedPath, "DONE")), threshold); + if (string.IsNullOrEmpty(report)) + return ""; + + var delta = TimeUtils.MinutesSince(_status.LastGraceNotification); + report += "\nTime since last grace notification: " + delta + "\n"; + if (delta < _config.GraceNotificationDelta) + return report; + + _status.LastGraceNotification = DateTime.Now; + SendAdminEmail(report, "Grace location cleanup required."); + return report + "\nNotification sent to AdminEmailAdress.\n"; + } } } \ No newline at end of file