Skip to content

Commit

Permalink
Extract method for creating a retry strategy.
Browse files Browse the repository at this point in the history
  • Loading branch information
sjp committed May 1, 2018
1 parent 7c8f5d0 commit ab5ea14
Showing 1 changed file with 15 additions and 21 deletions.
36 changes: 15 additions & 21 deletions src/coverlet.core/Helpers/InstrumentationHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,7 @@ public static void RestoreOriginalModule(string module, string identifier)

// Restore the original module - retry up to 10 times, since the destination file could be locked
// See: https://github.com/tonerdo/coverlet/issues/25
var currentSleep = 6;
TimeSpan retryStrategy()
{
var sleep = TimeSpan.FromMilliseconds(currentSleep);
currentSleep *= 2;
return sleep;
}
var retryStrategy = CreateRetryStrategy();

RetryHelper.Retry(() => {
File.Copy(backupPath, module, true);
Expand All @@ -81,13 +75,7 @@ public static IEnumerable<string> ReadHitsFile(string path)
{
// Retry hitting the hits file - retry up to 10 times, since the file could be locked
// See: https://github.com/tonerdo/coverlet/issues/25
var currentSleep = 6;
TimeSpan retryStrategy()
{
var sleep = TimeSpan.FromMilliseconds(currentSleep);
currentSleep *= 2;
return sleep;
}
var retryStrategy = CreateRetryStrategy();

return RetryHelper.Do(() => File.ReadLines(path), retryStrategy, 10);
}
Expand All @@ -96,13 +84,7 @@ public static void DeleteHitsFile(string path)
{
// Retry hitting the hits file - retry up to 10 times, since the file could be locked
// See: https://github.com/tonerdo/coverlet/issues/25
var currentSleep = 6;
TimeSpan retryStrategy()
{
var sleep = TimeSpan.FromMilliseconds(currentSleep);
currentSleep *= 2;
return sleep;
}
var retryStrategy = CreateRetryStrategy();

RetryHelper.Retry(() => File.Delete(path), retryStrategy, 10);
}
Expand Down Expand Up @@ -164,6 +146,18 @@ private static string GetBackupPath(string module, string identifier)
Path.GetFileNameWithoutExtension(module) + "_" + identifier + ".dll"
);
}

private static Func<TimeSpan> CreateRetryStrategy(int initialSleepSeconds = 6)
{
TimeSpan retryStrategy()
{
var sleep = TimeSpan.FromMilliseconds(initialSleepSeconds);
initialSleepSeconds *= 2;
return sleep;
}

return retryStrategy;
}
}
}

0 comments on commit ab5ea14

Please sign in to comment.