38 lines
945 B
C#
38 lines
945 B
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Threading;
|
|
|
|
namespace JD.Core
|
|
{
|
|
public static class Retry
|
|
{
|
|
public static void Do(TimeSpan retryInterval, int retryCount, Action action)
|
|
{
|
|
Do<object>(retryInterval, retryCount, () =>
|
|
{
|
|
action();
|
|
return null;
|
|
});
|
|
}
|
|
|
|
public static T Do<T>(TimeSpan retryInterval, int retryCount, Func<T> action)
|
|
{
|
|
var exceptions = new List<Exception>();
|
|
|
|
for (var retry = 0; retry < retryCount; retry++)
|
|
{
|
|
try
|
|
{
|
|
return action();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
exceptions.Add(ex);
|
|
Thread.Sleep(retryInterval);
|
|
}
|
|
}
|
|
|
|
throw new AggregateException(exceptions);
|
|
}
|
|
}
|
|
}
|