Skip to content

Commit

Permalink
(GH-622) Allow silent retries
Browse files Browse the repository at this point in the history
Certain operations you want to be performed without logging warnings.
Ensure that those operations can be performed without logging at a
level that may alarm the user. Log only to the file instead.
  • Loading branch information
ferventcoder committed May 27, 2016
1 parent 347753b commit 0a8baca
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 13 deletions.
26 changes: 19 additions & 7 deletions src/chocolatey/infrastructure/filesystem/DotNetFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,14 @@ public sealed class DotNetFileSystem : IFileSystem
private readonly int TIMES_TO_TRY_OPERATION = 3;
private static Lazy<IEnvironment> environment_initializer = new Lazy<IEnvironment>(() => new Environment());

private void allow_retries(Action action)
private void allow_retries(Action action, bool isSilent = false)
{
FaultTolerance.retry(
TIMES_TO_TRY_OPERATION,
action,
waitDurationMilliseconds: 200,
increaseRetryByMilliseconds: 100);
increaseRetryByMilliseconds: 100,
isSilent:isSilent);
}

[EditorBrowsable(EditorBrowsableState.Never)]
Expand Down Expand Up @@ -511,10 +512,15 @@ public void create_directory_if_not_exists(string directoryPath, bool ignoreErro

public void delete_directory(string directoryPath, bool recursive)
{
delete_directory(directoryPath, recursive, overrideAttributes: false);
delete_directory(directoryPath, recursive, overrideAttributes: false, isSilent: false);
}

public void delete_directory(string directoryPath, bool recursive, bool overrideAttributes)
{
delete_directory(directoryPath, recursive, overrideAttributes: overrideAttributes, isSilent: false);
}

public void delete_directory(string directoryPath, bool recursive, bool overrideAttributes, bool isSilent)
{
if (string.IsNullOrWhiteSpace(directoryPath)) throw new ApplicationException("You must provide a directory to delete.");
if (combine_paths(directoryPath, "").is_equal_to(combine_paths(Environment.GetEnvironmentVariable("SystemDrive"), ""))) throw new ApplicationException("Cannot move or delete the root of the system drive");
Expand All @@ -532,20 +538,26 @@ public void delete_directory(string directoryPath, bool recursive, bool override
}
}

this.Log().Debug(ChocolateyLoggers.Verbose, () => "Attempting to delete directory \"{0}\".".format_with(get_full_path(directoryPath)));
allow_retries(() => Directory.Delete(directoryPath, recursive));
if (!isSilent) this.Log().Debug(ChocolateyLoggers.Verbose, () => "Attempting to delete directory \"{0}\".".format_with(get_full_path(directoryPath)));
allow_retries(() => Directory.Delete(directoryPath, recursive),isSilent: isSilent);
}


public void delete_directory_if_exists(string directoryPath, bool recursive)
{
delete_directory_if_exists(directoryPath, recursive, overrideAttributes: false);
delete_directory_if_exists(directoryPath, recursive, overrideAttributes: false, isSilent: false);
}

public void delete_directory_if_exists(string directoryPath, bool recursive, bool overrideAttributes)
{
delete_directory_if_exists(directoryPath, recursive, overrideAttributes: overrideAttributes, isSilent: false);
}

public void delete_directory_if_exists(string directoryPath, bool recursive, bool overrideAttributes, bool isSilent)
{
if (directory_exists(directoryPath))
{
delete_directory(directoryPath, recursive, overrideAttributes);
delete_directory(directoryPath, recursive, overrideAttributes, isSilent);
}
}

Expand Down
20 changes: 19 additions & 1 deletion src/chocolatey/infrastructure/filesystem/IFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -356,12 +356,21 @@ public interface IFileSystem
/// <param name="overrideAttributes">Override the attributes, e.g. delete readonly and/or system files.</param>
void delete_directory(string directoryPath, bool recursive, bool overrideAttributes);

/// <summary>
/// Deletes a directory
/// </summary>
/// <param name="directoryPath">The directory path.</param>
/// <param name="recursive">Would you like to delete the directories inside of this directory? Almost always true.</param>
/// <param name="overrideAttributes">Override the attributes, e.g. delete readonly and/or system files.</param>
/// <param name="isSilent">Should this method be silent? false by default</param>
void delete_directory(string directoryPath, bool recursive, bool overrideAttributes, bool isSilent);

/// <summary>
/// Deletes a directory if it exists
/// </summary>
/// <param name="directoryPath">The directory path.</param>
/// <param name="recursive">Would you like to delete the directories inside of this directory? Almost always true.</param>
void delete_directory_if_exists(string directoryPath, bool recursive);
void delete_directory_if_exists(string directoryPath, bool recursive);

/// <summary>
/// Deletes a directory if it exists
Expand All @@ -371,6 +380,15 @@ public interface IFileSystem
/// <param name="overrideAttributes">Override the attributes, e.g. delete readonly and/or system files.</param>
void delete_directory_if_exists(string directoryPath, bool recursive, bool overrideAttributes);

/// <summary>
/// Deletes a directory if it exists
/// </summary>
/// <param name="directoryPath">The directory path.</param>
/// <param name="recursive">Would you like to delete the directories inside of this directory? Almost always true.</param>
/// <param name="overrideAttributes">Override the attributes, e.g. delete readonly and/or system files.</param>
/// <param name="isSilent">Should this method be silent? false by default</param>
void delete_directory_if_exists(string directoryPath, bool recursive, bool overrideAttributes, bool isSilent);

#endregion

/// <summary>
Expand Down
16 changes: 11 additions & 5 deletions src/chocolatey/infrastructure/tolerance/FaultTolerance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ namespace chocolatey.infrastructure.tolerance
using System;
using System.Threading;
using configuration;
using logging;

/// <summary>
/// Provides methods that are able to tolerate faults and recover
Expand Down Expand Up @@ -46,7 +47,8 @@ private static bool log_is_in_debug_mode()
/// <param name="action">The action.</param>
/// <param name="waitDurationMilliseconds">The wait duration in milliseconds.</param>
/// <param name="increaseRetryByMilliseconds">The time for each try to increase the wait duration by in milliseconds.</param>
public static void retry(int numberOfTries, Action action, int waitDurationMilliseconds = 100, int increaseRetryByMilliseconds = 0)
/// <param name="isSilent">Log messages?</param>
public static void retry(int numberOfTries, Action action, int waitDurationMilliseconds = 100, int increaseRetryByMilliseconds = 0, bool isSilent = false)
{
if (action == null) return;

Expand All @@ -58,7 +60,8 @@ public static void retry(int numberOfTries, Action action, int waitDurationMilli
return true;
},
waitDurationMilliseconds,
increaseRetryByMilliseconds);
increaseRetryByMilliseconds,
isSilent);
}

/// <summary>
Expand All @@ -71,13 +74,16 @@ public static void retry(int numberOfTries, Action action, int waitDurationMilli
/// <param name="increaseRetryByMilliseconds">The time for each try to increase the wait duration by in milliseconds.</param>
/// <returns>The return value from the function</returns>
/// <exception cref="System.ApplicationException">You must specify a number of retries greater than zero.</exception>
public static T retry<T>(int numberOfTries, Func<T> function, int waitDurationMilliseconds = 100, int increaseRetryByMilliseconds = 0)
/// <param name="isSilent">Log messages?</param>
public static T retry<T>(int numberOfTries, Func<T> function, int waitDurationMilliseconds = 100, int increaseRetryByMilliseconds = 0, bool isSilent = false)
{
if (function == null) return default(T);
if (numberOfTries == 0) throw new ApplicationException("You must specify a number of retries greater than zero.");
var returnValue = default(T);

var debugging = log_is_in_debug_mode();
var logLocation = ChocolateyLoggers.Normal;
if (isSilent) logLocation = ChocolateyLoggers.LogFileOnly;

for (int i = 1; i <= numberOfTries; i++)
{
Expand All @@ -90,15 +96,15 @@ public static T retry<T>(int numberOfTries, Func<T> function, int waitDurationMi
{
if (i == numberOfTries)
{
"chocolatey".Log().Error("Maximum tries of {0} reached. Throwing error.".format_with(numberOfTries));
"chocolatey".Log().Error(logLocation, "Maximum tries of {0} reached. Throwing error.".format_with(numberOfTries));
throw;
}

int retryWait = waitDurationMilliseconds + (i*increaseRetryByMilliseconds);

var exceptionMessage = debugging ? ex.ToString() : ex.Message;

"chocolatey".Log().Warn("This is try {3}/{4}. Retrying after {2} milliseconds.{0} Error converted to warning:{0} {1}".format_with(
"chocolatey".Log().Warn(logLocation, "This is try {3}/{4}. Retrying after {2} milliseconds.{0} Error converted to warning:{0} {1}".format_with(
Environment.NewLine,
exceptionMessage,
retryWait,
Expand Down

0 comments on commit 0a8baca

Please sign in to comment.