You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Ombi/PlexRequests.Api/RetryHandler.cs

72 lines
1.5 KiB

using System;
using Polly.Retry;
using Polly;
using System.Threading.Tasks;
namespace PlexRequests.Api
{
public static class RetryHandler
{
private static readonly TimeSpan[] DefaultTime = new TimeSpan[] {
TimeSpan.FromSeconds (2),
TimeSpan.FromSeconds(5),
TimeSpan.FromSeconds(10)};
public static RetryPolicy RetryAndWaitPolicy(TimeSpan[] timeSpan, Action action)
{
if(timeSpan == null)
{
timeSpan = DefaultTime;
}
var policy = Policy.Handle<Exception> ()
.WaitAndRetry(timeSpan, (e, ts) => action());
return policy;
}
public static RetryPolicy RetryAndWaitPolicy(TimeSpan[] timeSpan)
{
if(timeSpan == null)
{
timeSpan = DefaultTime;
}
var policy = Policy.Handle<Exception> ()
.WaitAndRetry(timeSpan);
return policy;
}
public static RetryPolicy RetryAndWaitPolicy(TimeSpan[] timeSpan, Action<Exception, TimeSpan> action)
{
if(timeSpan == null)
{
timeSpan = DefaultTime;
}
var policy = Policy.Handle<Exception> ()
.WaitAndRetry(timeSpan, action);
return policy;
}
public static T Execute<T>(Func<T> action, TimeSpan[] timeSpan)
{
var policy = RetryAndWaitPolicy (timeSpan);
return policy.Execute (action);
}
public static T Execute<T>(Func<T> func, TimeSpan[] timeSpan, Action<Exception, TimeSpan> action)
{
if(timeSpan == null)
{
timeSpan = DefaultTime;
}
var policy = RetryAndWaitPolicy (timeSpan, action);
return policy.Execute (func);
}
}
}