Skip to content

Commit

Permalink
(chocolateyGH-189) Shorten retry messages and include description
Browse files Browse the repository at this point in the history
  • Loading branch information
christianrondeau committed Jan 27, 2016
1 parent 7294952 commit 39c0d07
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,41 @@ public void should_log_warning_each_time()
// don't care
}

MockLogger.MessagesFor(LogLevel.Warn).Count.ShouldEqual(2);
MockLogger.MessagesFor(LogLevel.Warn).Count.ShouldEqual(3);
}

[Fact]
public void should_log_error_once()
{
reset();

try
{
FaultTolerance.retry(3, () => { throw new Exception("YIKES"); }, waitDurationMilliseconds: 0);
}
catch
{
// don't care
}

MockLogger.MessagesFor(LogLevel.Error).ShouldContain("Chocolatey could not execute the operation.");
}

[Fact]
public void should_log_error_once_with_description()
{
reset();

try
{
FaultTolerance.retry(3, () => { throw new Exception("YIKES"); }, description: "Throwing YYKES", waitDurationMilliseconds: 0);
}
catch
{
// don't care
}

MockLogger.MessagesFor(LogLevel.Error).ShouldContain("Chocolatey could not execute: Throwing YYKES");
}

[Fact]
Expand Down
28 changes: 16 additions & 12 deletions src/chocolatey/infrastructure/tolerance/FaultTolerance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,10 @@ private static bool log_is_in_debug_mode()
/// </summary>
/// <param name="numberOfTries">The number of tries.</param>
/// <param name="action">The action.</param>
/// <param name="description">A message describing what the <paramref name="action"/> is. Shown when an error occurs.</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)
public static void retry(int numberOfTries, Action action, string description = null, int waitDurationMilliseconds = 100, int increaseRetryByMilliseconds = 0)
{
if (action == null) return;

Expand All @@ -57,6 +58,7 @@ public static void retry(int numberOfTries, Action action, int waitDurationMilli
action.Invoke();
return true;
},
description,
waitDurationMilliseconds,
increaseRetryByMilliseconds);
}
Expand All @@ -67,11 +69,12 @@ public static void retry(int numberOfTries, Action action, int waitDurationMilli
/// <typeparam name="T">The type of the return value from the function.</typeparam>
/// <param name="numberOfTries">The number of tries.</param>
/// <param name="function">The function.</param>
/// <param name="description">A message describing what the <paramref name="function"/> is. Shown when an error occurs.</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>
/// <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)
public static T retry<T>(int numberOfTries, Func<T> function, string description = null, int waitDurationMilliseconds = 100, int increaseRetryByMilliseconds = 0)
{
if (function == null) return default(T);
if (numberOfTries == 0) throw new ApplicationException("You must specify a number of retries greater than zero.");
Expand All @@ -88,22 +91,23 @@ public static T retry<T>(int numberOfTries, Func<T> function, int waitDurationMi
}
catch (Exception ex)
{
var exceptionMessage = debugging ? ex.ToString() : ex.Message;

var failedMessage = "Operation failed ({0}/{1}): {2}".format_with(i, numberOfTries, exceptionMessage);
"chocolatey".Log().Warn(failedMessage);

if (i == numberOfTries)
{
"chocolatey".Log().Error("Maximum tries of {0} reached. Throwing error.".format_with(numberOfTries));
"chocolatey".Log().Error(description != null
? "Chocolatey could not execute: {0}".format_with(description)
: "Chocolatey could not execute the operation.");

throw;
}

int retryWait = waitDurationMilliseconds + (i*increaseRetryByMilliseconds);

var exceptionMessage = debugging ? ex.ToString() : ex.Message;
int retryWait = waitDurationMilliseconds + (i * increaseRetryByMilliseconds);

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

0 comments on commit 39c0d07

Please sign in to comment.