Files
git-tfs/GitTfs.VsCommon/Retry.cs
T
Philippe Miossec 4de1afebf9 Retry after getting a file from TFS if the status is not satisfying
Should improve reliability.
Hope it will fix #444
2014-10-27 21:36:02 +01:00

77 lines
2.2 KiB
C#

using System;
using System.Collections.Generic;
using System.Threading;
using Sep.Git.Tfs.Core;
namespace Sep.Git.Tfs.VsCommon
{
public static class Retry
{
public static void Do(Action action)
{
Do(action, TimeSpan.FromSeconds(1));
}
public static void Do(Action action, TimeSpan retryInterval, int retryCount = 10)
{
Do<object>(() =>
{
action();
return null;
}, retryInterval, retryCount);
}
public static T Do<T>(Func<T> action)
{
return Do(action, TimeSpan.FromSeconds(1));
}
public static T Do<T>(Func<T> action, TimeSpan retryInterval, int retryCount = 10)
{
var exceptions = new List<Exception>();
for (int retry = 0; retry < retryCount; retry++)
{
try
{
return action();
}
catch (Microsoft.TeamFoundation.TeamFoundationServiceUnavailableException ex)
{
exceptions.Add(ex);
Thread.Sleep(retryInterval);
}
catch (Microsoft.TeamFoundation.Framework.Client.DatabaseOperationTimeoutException ex)
{
exceptions.Add(ex);
Thread.Sleep(retryInterval);
}
catch (System.Net.WebException ex)
{
exceptions.Add(ex);
Thread.Sleep(retryInterval);
}
}
throw new AggregateException(exceptions);
}
public static void DoWhile(Func<bool> action, int retryCount = 10)
{
DoWhile(action, TimeSpan.FromSeconds(0), retryCount);
}
public static void DoWhile(Func<bool> action, TimeSpan retryInterval, int retryCount = 10)
{
int count = 0;
while (action())
{
count++;
if (count > retryCount)
throw new GitTfsException("error: Action failed after " + retryCount + " retries!");
Thread.Sleep(retryInterval);
}
}
}
}