mirror of
https://github.com/Ombi-app/Ombi.git
synced 2025-07-11 07:46:05 -07:00
71 lines
1.5 KiB
C#
71 lines
1.5 KiB
C#
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);
|
|
}
|
|
}
|
|
}
|
|
|