Beispiel #1
0
        /// <summary>
        /// Builds a <see cref="Policy"/> that will wait and retry <paramref name="retryCount"/> times
        /// calling <paramref name="onRetry"/> on each retry with the raised exception and the current sleep duration.
        /// On each retry, the duration to wait is calculated by calling <paramref name="sleepDurationProvider"/> with
        /// the current retry attempt allowing an exponentially increasing wait time (exponential backoff).
        /// </summary>
        /// <param name="policyBuilder">The policy builder.</param>
        /// <param name="retryCount">The retry count.</param>
        /// <param name="sleepDurationProvider">The function that provides the duration to wait for for a particular retry attempt.</param>
        /// <param name="onRetry">The action to call on each retry.</param>
        /// <returns>The policy instance.</returns>
        /// <exception cref="System.ArgumentOutOfRangeException">retryCount;Value must be greater than or equal to zero.</exception>
        /// <exception cref="System.ArgumentNullException">
        /// timeSpanProvider
        /// or
        /// onRetry
        /// </exception>
        public static RetryPolicy  WaitAndRetry(this PolicyBuilder policyBuilder, int retryCount, Func <int, TimeSpan> sleepDurationProvider, Action <Exception, TimeSpan> onRetry)
        {
            if (retryCount < 0)
            {
                throw new ArgumentOutOfRangeException("retryCount", "Value must be greater than or equal to zero.");
            }
            if (sleepDurationProvider == null)
            {
                throw new ArgumentNullException("sleepDurationProvider");
            }
            if (onRetry == null)
            {
                throw new ArgumentNullException("onRetry");
            }

            var sleepDurations = Enumerable.Range(1, retryCount)
                                 .Select(sleepDurationProvider);

            return(new RetryPolicy(
                       (action, context) => RetryEngine.Implementation(
                           action,
                           policyBuilder.ExceptionPredicates,
                           () => new RetryPolicyStateWithSleep(sleepDurations, onRetry)
                           ),
                       policyBuilder.ExceptionPredicates
                       ));
        }
        /// <summary>
        /// Builds a <see cref="Policy"/> that will wait and retry <paramref name="retryCount"/> times
        /// calling <paramref name="onRetry"/> on each retry with the handled exception or result, current sleep duration, retry count, and context data.
        /// On each retry, the duration to wait is calculated by calling <paramref name="sleepDurationProvider"/> with
        /// the current retry attempt allowing an exponentially increasing wait time (exponential backoff).
        /// </summary>
        /// <param name="policyBuilder">The policy builder.</param>
        /// <param name="retryCount">The retry count.</param>
        /// <param name="sleepDurationProvider">The function that provides the duration to wait for for a particular retry attempt.</param>
        /// <param name="onRetry">The action to call on each retry.</param>
        /// <returns>The policy instance.</returns>
        /// <exception cref="System.ArgumentOutOfRangeException">retryCount;Value must be greater than or equal to zero.</exception>
        /// <exception cref="System.ArgumentNullException">
        /// timeSpanProvider
        /// or
        /// onRetry
        /// </exception>
        public static RetryPolicy <TResult> WaitAndRetry <TResult>(this PolicyBuilder <TResult> policyBuilder, int retryCount, Func <int, DelegateResult <TResult>, Context, TimeSpan> sleepDurationProvider, Action <DelegateResult <TResult>, TimeSpan, int, Context> onRetry)
        {
            if (retryCount < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(retryCount), "Value must be greater than or equal to zero.");
            }
            if (sleepDurationProvider == null)
            {
                throw new ArgumentNullException(nameof(sleepDurationProvider));
            }
            if (onRetry == null)
            {
                throw new ArgumentNullException(nameof(onRetry));
            }

            return(new RetryPolicy <TResult>(
                       (action, context, cancellationToken) => RetryEngine.Implementation(
                           action,
                           context,
                           cancellationToken,
                           policyBuilder.ExceptionPredicates,
                           policyBuilder.ResultPredicates,
                           () => new RetryStateWaitAndRetryWithProvider <TResult>(retryCount, sleepDurationProvider, onRetry, context)
                           ),
                       policyBuilder.ExceptionPredicates,
                       policyBuilder.ResultPredicates));
        }
Beispiel #3
0
        /// <summary>
        /// Builds a <see cref="Policy"/> that will wait and retry <paramref name="retryCount"/> times
        /// calling <paramref name="onRetry"/> on each retry with the raised exception, current sleep duration and context data.
        /// On each retry, the duration to wait is calculated by calling <paramref name="sleepDurationProvider"/> with
        /// the current retry attempt allowing an exponentially increasing wait time (exponential backoff).
        /// </summary>
        /// <param name="policyBuilder">The policy builder.</param>
        /// <param name="retryCount">The retry count.</param>
        /// <param name="sleepDurationProvider">The function that provides the duration to wait for for a particular retry attempt.</param>
        /// <param name="onRetry">The action to call on each retry.</param>
        /// <returns>The policy instance.</returns>
        /// <exception cref="System.ArgumentOutOfRangeException">retryCount;Value must be greater than or equal to zero.</exception>
        /// <exception cref="System.ArgumentNullException">
        /// sleepDurationProvider
        /// or
        /// onRetry
        /// </exception>
        public static RetryPolicy WaitAndRetry(this PolicyBuilder policyBuilder, int retryCount, Func <int, TimeSpan> sleepDurationProvider, Action <Exception, TimeSpan, Context> onRetry)
        {
            if (retryCount < 0)
            {
                throw new ArgumentOutOfRangeException("retryCount", "Value must be greater than or equal to zero.");
            }
            if (sleepDurationProvider == null)
            {
                throw new ArgumentNullException("sleepDurationProvider");
            }
            if (onRetry == null)
            {
                throw new ArgumentNullException("onRetry");
            }

            var sleepDurations = Enumerable.Range(1, retryCount)
                                 .Select(sleepDurationProvider);

            return(new RetryPolicy((action, context, cancellationToken) => RetryEngine.Implementation(
                                       ct => { action(ct); return EmptyStruct.Instance; },
                                       cancellationToken,
                                       policyBuilder.ExceptionPredicates,
                                       PredicateHelper <EmptyStruct> .EmptyResultPredicates,
                                       () => new RetryPolicyStateWithSleep <EmptyStruct>(sleepDurations, (outcome, timespan, ctx) => onRetry(outcome.Exception, timespan, ctx), context)
                                       ), policyBuilder.ExceptionPredicates));
        }
Beispiel #4
0
        /// <summary>
        /// Builds a <see cref="Policy"/> that will wait and retry <paramref name="retryCount"/> times
        /// calling <paramref name="onRetry"/> on each retry with the raised exception, current sleep duration, retry count, and context data.
        /// On each retry, the duration to wait is calculated by calling <paramref name="sleepDurationProvider"/> with
        /// the current retry attempt allowing an exponentially increasing wait time (exponential backoff).
        /// </summary>
        /// <param name="policyBuilder">The policy builder.</param>
        /// <param name="retryCount">The retry count.</param>
        /// <param name="sleepDurationProvider">The function that provides the duration to wait for for a particular retry attempt.</param>
        /// <param name="onRetry">The action to call on each retry.</param>
        /// <returns>The policy instance.</returns>
        /// <exception cref="System.ArgumentOutOfRangeException">retryCount;Value must be greater than or equal to zero.</exception>
        /// <exception cref="System.ArgumentNullException">
        /// timeSpanProvider
        /// or
        /// onRetry
        /// </exception>
        public static RetryPolicy WaitAndRetry(this PolicyBuilder policyBuilder, int retryCount, Func <int, Exception, Context, TimeSpan> sleepDurationProvider, Action <Exception, TimeSpan, int, Context> onRetry)
        {
            if (retryCount < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(retryCount), "Value must be greater than or equal to zero.");
            }
            if (sleepDurationProvider == null)
            {
                throw new ArgumentNullException(nameof(sleepDurationProvider));
            }
            if (onRetry == null)
            {
                throw new ArgumentNullException(nameof(onRetry));
            }

            return(new RetryPolicy(
                       (action, context, cancellationToken) => RetryEngine.Implementation(
                           (ctx, ct) => { action(ctx, ct); return EmptyStruct.Instance; },
                           context,
                           cancellationToken,
                           policyBuilder.ExceptionPredicates,
                           PredicateHelper <EmptyStruct> .EmptyResultPredicates,
                           () => new RetryStateWaitAndRetryWithProvider <EmptyStruct>(
                               retryCount,
                               (i, outcome, ctx) => sleepDurationProvider(i, outcome.Exception, ctx),
                               (outcome, timespan, i, ctx) => onRetry(outcome.Exception, timespan, i, ctx),
                               context)
                           ), policyBuilder.ExceptionPredicates));
        }
Beispiel #5
0
        /// <summary>
        /// Builds a <see cref="Policy"/> that will wait and retry <paramref name="retryCount"/> times
        /// calling <paramref name="onRetry"/> on each retry with the handled exception or result and the current sleep duration.
        /// On each retry, the duration to wait is calculated by calling <paramref name="sleepDurationProvider"/> with
        /// the current retry attempt allowing an exponentially increasing wait time (exponential backoff).
        /// </summary>
        /// <param name="policyBuilder">The policy builder.</param>
        /// <param name="retryCount">The retry count.</param>
        /// <param name="sleepDurationProvider">The function that provides the duration to wait for for a particular retry attempt.</param>
        /// <param name="onRetry">The action to call on each retry.</param>
        /// <returns>The policy instance.</returns>
        /// <exception cref="System.ArgumentOutOfRangeException">retryCount;Value must be greater than or equal to zero.</exception>
        /// <exception cref="System.ArgumentNullException">
        /// sleepDurationProvider
        /// or
        /// onRetry
        /// </exception>
        public static RetryPolicy <TResult> WaitAndRetry <TResult>(this PolicyBuilder <TResult> policyBuilder, int retryCount, Func <int, TimeSpan> sleepDurationProvider, Action <DelegateResult <TResult>, TimeSpan> onRetry)
        {
            if (retryCount < 0)
            {
                throw new ArgumentOutOfRangeException("retryCount", "Value must be greater than or equal to zero.");
            }
            if (sleepDurationProvider == null)
            {
                throw new ArgumentNullException("sleepDurationProvider");
            }
            if (onRetry == null)
            {
                throw new ArgumentNullException("onRetry");
            }

            var sleepDurations = Enumerable.Range(1, retryCount)
                                 .Select(sleepDurationProvider);

            return(new RetryPolicy <TResult>(
                       (action, context, cancellationToken) => RetryEngine.Implementation(
                           action,
                           cancellationToken,
                           policyBuilder.ExceptionPredicates,
                           policyBuilder.ResultPredicates,
                           () => new RetryPolicyStateWithSleep <TResult>(sleepDurations, (outcome, span, ctx) => onRetry(outcome, span), context)
                           ),
                       policyBuilder.ExceptionPredicates,
                       policyBuilder.ResultPredicates
                       ));
        }
Beispiel #6
0
        public void TestInitialize()
        {
            var serviceProvider = new ServiceCollection()
                                  .AddLogging()
                                  .AddLibraryDependencies()
                                  .BuildServiceProvider();

            this.retryEngine = serviceProvider.GetRequiredService <RetryEngine>();
        }
Beispiel #7
0
        /// <summary>
        /// Builds a <see cref="Policy"/> that will retry indefinitely
        /// calling <paramref name="onRetry"/> on each retry with the raised exception and context data.
        /// </summary>
        /// <param name="policyBuilder">The policy builder.</param>
        /// <param name="onRetry">The action to call on each retry.</param>
        /// <returns>The policy instance.</returns>
        /// <exception cref="System.ArgumentNullException">onRetry</exception>
        public static RetryPolicy RetryForever(this PolicyBuilder policyBuilder, Action <Exception, Context> onRetry)
        {
            if (onRetry == null)
            {
                throw new ArgumentNullException("onRetry");
            }

            return(new RetryPolicy((action, context) => RetryEngine.Implementation(
                                       action,
                                       policyBuilder.ExceptionPredicates,
                                       () => new RetryPolicyState(onRetry, context)
                                       ), policyBuilder.ExceptionPredicates));
        }
 public FileDeleter(
     ProbeDbContext probeDbContext,
     RetryEngine retryEngine,
     IStorageProvider storageProvider,
     EventService eventService,
     AppsContainer appsContainer)
 {
     _probeDbContext  = probeDbContext;
     _retryEngine     = retryEngine;
     _storageProvider = storageProvider;
     _eventService    = eventService;
     _appsContainer   = appsContainer;
 }
Beispiel #9
0
        /// <summary>
        /// Builds a <see cref="Policy"/> that will retry indefinitely
        /// calling <paramref name="onRetry"/> on each retry with the raised exception and context data.
        /// </summary>
        /// <param name="policyBuilder">The policy builder.</param>
        /// <param name="onRetry">The action to call on each retry.</param>
        /// <returns>The policy instance.</returns>
        /// <exception cref="System.ArgumentNullException">onRetry</exception>
        public static RetryPolicy RetryForever(this PolicyBuilder policyBuilder, Action <Exception, Context> onRetry)
        {
            if (onRetry == null)
            {
                throw new ArgumentNullException("onRetry");
            }

            return(new RetryPolicy((action, context) => RetryEngine.Implementation(
                                       () => { action(); return EmptyStruct.Instance; },
                                       policyBuilder.ExceptionPredicates,
                                       Enumerable.Empty <ResultPredicate <EmptyStruct> >(),
                                       () => new RetryPolicyState <EmptyStruct>((outcome, ctx) => onRetry(outcome.Exception, ctx), context)
                                       ), policyBuilder.ExceptionPredicates));
        }
Beispiel #10
0
        /// <summary>
        /// Builds a <see cref="Policy"/> that will retry indefinitely
        /// calling <paramref name="onRetry"/> on each retry with the raised exception and context data.
        /// </summary>
        /// <param name="policyBuilder">The policy builder.</param>
        /// <param name="onRetry">The action to call on each retry.</param>
        /// <returns>The policy instance.</returns>
        /// <exception cref="System.ArgumentNullException">onRetry</exception>
        public static RetryPolicy RetryForever(this PolicyBuilder policyBuilder, Action <Exception, Context> onRetry)
        {
            if (onRetry == null)
            {
                throw new ArgumentNullException("onRetry");
            }

            return(new RetryPolicy((action, context, cancellationToken) => RetryEngine.Implementation(
                                       ct => { action(ct); return EmptyStruct.Instance; },
                                       cancellationToken,
                                       policyBuilder.ExceptionPredicates,
                                       PredicateHelper <EmptyStruct> .EmptyResultPredicates,
                                       () => new RetryPolicyState <EmptyStruct>((outcome, ctx) => onRetry(outcome.Exception, ctx), context)
                                       ), policyBuilder.ExceptionPredicates));
        }
Beispiel #11
0
        /// <summary>
        /// Builds a <see cref="Policy"/> that will wait and retry indefinitely
        /// calling <paramref name="onRetry"/> on each retry with the raised exception and
        /// execution context.
        /// </summary>
        /// <param name="policyBuilder">The policy builder.</param>
        /// <param name="sleepDurationProvider"></param>
        /// <param name="onRetry">The action to call on each retry.</param>
        /// <returns>The policy instance.</returns>
        /// <exception cref="System.ArgumentNullException">sleepDurationProvider</exception>
        /// <exception cref="System.ArgumentNullException">onRetry</exception>
        public static RetryPolicy WaitAndRetryForever(this PolicyBuilder policyBuilder, Func <int, TimeSpan> sleepDurationProvider, Action <Exception, TimeSpan, Context> onRetry)
        {
            if (sleepDurationProvider == null)
            {
                throw new ArgumentNullException("sleepDurationProvider");
            }
            if (onRetry == null)
            {
                throw new ArgumentNullException("onRetry");
            }

            return(new RetryPolicy((action, context) => RetryEngine.Implementation(
                                       action,
                                       policyBuilder.ExceptionPredicates,
                                       () => new RetryPolicyStateWithSleepDurationProvider(sleepDurationProvider, onRetry, context)
                                       ), policyBuilder.ExceptionPredicates));
        }
Beispiel #12
0
        /// <summary>
        /// Builds a <see cref="Policy"/> that will retry <paramref name="retryCount"/> times
        /// calling <paramref name="onRetry"/> on each retry with the raised exception, retry count and context data.
        /// </summary>
        /// <param name="policyBuilder">The policy builder.</param>
        /// <param name="retryCount">The retry count.</param>
        /// <param name="onRetry">The action to call on each retry.</param>
        /// <returns>The policy instance.</returns>
        /// <exception cref="System.ArgumentOutOfRangeException">retryCount;Value must be greater than or equal to zero.</exception>
        /// <exception cref="System.ArgumentNullException">onRetry</exception>
        public static RetryPolicy Retry(this PolicyBuilder policyBuilder, int retryCount, Action <Exception, int, Context> onRetry)
        {
            if (retryCount < 0)
            {
                throw new ArgumentOutOfRangeException("retryCount", "Value must be greater than or equal to zero.");
            }
            if (onRetry == null)
            {
                throw new ArgumentNullException("onRetry");
            }

            return(new RetryPolicy((action, context) => RetryEngine.Implementation(
                                       action,
                                       policyBuilder.ExceptionPredicates,
                                       () => new RetryPolicyStateWithCount(retryCount, onRetry, context)
                                       ), policyBuilder.ExceptionPredicates));
        }
        /// <summary>
        /// Builds a <see cref="Policy"/> that will retry indefinitely
        /// calling <paramref name="onRetry"/> on each retry with the handled exception or result and context data.
        /// </summary>
        /// <param name="policyBuilder">The policy builder.</param>
        /// <param name="onRetry">The action to call on each retry.</param>
        /// <returns>The policy instance.</returns>
        /// <exception cref="System.ArgumentNullException">onRetry</exception>
        public static RetryPolicy <TResult> RetryForever <TResult>(this PolicyBuilder <TResult> policyBuilder, Action <DelegateResult <TResult>, Context> onRetry)
        {
            if (onRetry == null)
            {
                throw new ArgumentNullException("onRetry");
            }

            return(new RetryPolicy <TResult>(
                       (action, context) => RetryEngine.Implementation(
                           action,
                           policyBuilder.ExceptionPredicates,
                           policyBuilder.ResultPredicates,
                           () => new RetryPolicyState <TResult>(onRetry, context)
                           ),
                       policyBuilder.ExceptionPredicates,
                       policyBuilder.ResultPredicates));
        }
Beispiel #14
0
        /// <summary>
        /// Builds a <see cref="Policy"/> that will wait and retry as many times as there are provided <paramref name="sleepDurations"/>
        /// calling <paramref name="onRetry"/> on each retry with the raised exception, current sleep duration and context data.
        /// On each retry, the duration to wait is the current <paramref name="sleepDurations"/> item.
        /// </summary>
        /// <param name="policyBuilder">The policy builder.</param>
        /// <param name="sleepDurations">The sleep durations to wait for on each retry.</param>
        /// <param name="onRetry">The action to call on each retry.</param>
        /// <returns>The policy instance.</returns>
        /// <exception cref="System.ArgumentNullException">
        /// sleepDurations
        /// or
        /// onRetry
        /// </exception>
        public static RetryPolicy WaitAndRetry(this PolicyBuilder policyBuilder, IEnumerable <TimeSpan> sleepDurations, Action <Exception, TimeSpan, Context> onRetry)
        {
            if (sleepDurations == null)
            {
                throw new ArgumentNullException("sleepDurations");
            }
            if (onRetry == null)
            {
                throw new ArgumentNullException("onRetry");
            }

            return(new RetryPolicy((action, context) => RetryEngine.Implementation(
                                       action,
                                       policyBuilder.ExceptionPredicates,
                                       () => new RetryPolicyStateWithSleep(sleepDurations, onRetry, context)
                                       ), policyBuilder.ExceptionPredicates));
        }
Beispiel #15
0
        /// <summary>
        /// Builds a <see cref="Policy"/> that will wait and retry indefinitely
        /// calling <paramref name="onRetry"/> on each retry with the raised exception and
        /// execution context.
        /// </summary>
        /// <param name="policyBuilder">The policy builder.</param>
        /// <param name="sleepDurationProvider"></param>
        /// <param name="onRetry">The action to call on each retry.</param>
        /// <returns>The policy instance.</returns>
        /// <exception cref="System.ArgumentNullException">sleepDurationProvider</exception>
        /// <exception cref="System.ArgumentNullException">onRetry</exception>
        public static RetryPolicy WaitAndRetryForever(this PolicyBuilder policyBuilder, Func <int, TimeSpan> sleepDurationProvider, Action <Exception, TimeSpan, Context> onRetry)
        {
            if (sleepDurationProvider == null)
            {
                throw new ArgumentNullException("sleepDurationProvider");
            }
            if (onRetry == null)
            {
                throw new ArgumentNullException("onRetry");
            }

            return(new RetryPolicy((action, context) => RetryEngine.Implementation(
                                       () => { action(); return EmptyStruct.Instance; },
                                       policyBuilder.ExceptionPredicates,
                                       Enumerable.Empty <ResultPredicate <EmptyStruct> >(),
                                       () => new RetryPolicyStateWithSleepDurationProvider <EmptyStruct>(sleepDurationProvider, (outcome, timespan, ctx) => onRetry(outcome.Exception, timespan, ctx), context)
                                       ), policyBuilder.ExceptionPredicates));
        }
Beispiel #16
0
        /// <summary>
        /// Builds a <see cref="Policy"/> that will retry <paramref name="retryCount"/> times
        /// calling <paramref name="onRetry"/> on each retry with the raised exception, retry count and context data.
        /// </summary>
        /// <param name="policyBuilder">The policy builder.</param>
        /// <param name="retryCount">The retry count.</param>
        /// <param name="onRetry">The action to call on each retry.</param>
        /// <returns>The policy instance.</returns>
        /// <exception cref="System.ArgumentOutOfRangeException">retryCount;Value must be greater than or equal to zero.</exception>
        /// <exception cref="System.ArgumentNullException">onRetry</exception>
        public static RetryPolicy Retry(this PolicyBuilder policyBuilder, int retryCount, Action <Exception, int, Context> onRetry)
        {
            if (retryCount < 0)
            {
                throw new ArgumentOutOfRangeException("retryCount", "Value must be greater than or equal to zero.");
            }
            if (onRetry == null)
            {
                throw new ArgumentNullException("onRetry");
            }

            return(new RetryPolicy((action, context) => RetryEngine.Implementation(
                                       () => { action(); return EmptyStruct.Instance; },
                                       policyBuilder.ExceptionPredicates,
                                       Enumerable.Empty <ResultPredicate <EmptyStruct> >(),
                                       () => new RetryPolicyStateWithCount <EmptyStruct>(retryCount, (outcome, i, ctx) => onRetry(outcome.Exception, i, ctx), context)
                                       ), policyBuilder.ExceptionPredicates));
        }
Beispiel #17
0
        /// <summary>
        /// Builds a <see cref="Policy"/> that will retry indefinitely
        /// calling <paramref name="onRetry"/> on each retry with the handled exception or result.
        /// </summary>
        /// <param name="policyBuilder">The policy builder.</param>
        /// <param name="onRetry">The action to call on each retry.</param>
        /// <returns>The policy instance.</returns>
        /// <exception cref="System.ArgumentNullException">onRetry</exception>
        public static RetryPolicy <TResult> RetryForever <TResult>(this PolicyBuilder <TResult> policyBuilder, Action <Exception> onRetry)
        {
            if (onRetry == null)
            {
                throw new ArgumentNullException("onRetry");
            }

            return(new RetryPolicy <TResult>(
                       (action, context, cancellationToken) => RetryEngine.Implementation(
                           action,
                           cancellationToken,
                           policyBuilder.ExceptionPredicates,
                           policyBuilder.ResultPredicates,
                           () => new RetryPolicyState <TResult>((outcome, ctx) => onRetry(outcome.Exception), context)
                           ),
                       policyBuilder.ExceptionPredicates,
                       policyBuilder.ResultPredicates
                       ));
        }
Beispiel #18
0
        /// <summary>
        /// Builds a <see cref="Policy"/> that will retry indefinitely
        /// calling <paramref name="onRetry"/> on each retry with the handled exception or result, retry count and context data.
        /// </summary>
        /// <param name="policyBuilder">The policy builder.</param>
        /// <param name="onRetry">The action to call on each retry.</param>
        /// <returns>The policy instance.</returns>
        /// <exception cref="System.ArgumentNullException">onRetry</exception>
        public static RetryPolicy <TResult> RetryForever <TResult>(this PolicyBuilder <TResult> policyBuilder, Action <DelegateResult <TResult>, int, Context> onRetry)
        {
            if (onRetry == null)
            {
                throw new ArgumentNullException(nameof(onRetry));
            }

            return(new RetryPolicy <TResult>(
                       (action, context, cancellationToken) => RetryEngine.Implementation(
                           action,
                           context,
                           cancellationToken,
                           policyBuilder.ExceptionPredicates,
                           policyBuilder.ResultPredicates,
                           () => new RetryStateRetryForeverWithCount <TResult>(onRetry, context)
                           ),
                       policyBuilder.ExceptionPredicates,
                       policyBuilder.ResultPredicates));
        }
Beispiel #19
0
        /// <summary>
        /// Builds a <see cref="Policy"/> that will wait and retry as many times as there are provided <paramref name="sleepDurations"/>
        /// calling <paramref name="onRetry"/> on each retry with the raised exception, current sleep duration, retry count and context data.
        /// On each retry, the duration to wait is the current <paramref name="sleepDurations"/> item.
        /// </summary>
        /// <param name="policyBuilder">The policy builder.</param>
        /// <param name="sleepDurations">The sleep durations to wait for on each retry.</param>
        /// <param name="onRetry">The action to call on each retry.</param>
        /// <returns>The policy instance.</returns>
        /// <exception cref="System.ArgumentNullException">
        /// sleepDurations
        /// or
        /// onRetry
        /// </exception>
        public static RetryPolicy WaitAndRetry(this PolicyBuilder policyBuilder, IEnumerable <TimeSpan> sleepDurations, Action <Exception, TimeSpan, int, Context> onRetry)
        {
            if (sleepDurations == null)
            {
                throw new ArgumentNullException("sleepDurations");
            }
            if (onRetry == null)
            {
                throw new ArgumentNullException("onRetry");
            }

            return(new RetryPolicy(
                       (action, context, cancellationToken) => RetryEngine.Implementation(
                           ct => { action(ct); return EmptyStruct.Instance; },
                           cancellationToken,
                           policyBuilder.ExceptionPredicates,
                           PredicateHelper <EmptyStruct> .EmptyResultPredicates,
                           () => new RetryPolicyStateWithSleep <EmptyStruct>(sleepDurations, (outcome, timespan, i, ctx) => onRetry(outcome.Exception, timespan, i, ctx), context)
                           ), policyBuilder.ExceptionPredicates));
        }
Beispiel #20
0
        /// <summary>
        /// Builds a <see cref="Policy"/> that will wait and retry indefinitely until the action succeeds,
        /// calling <paramref name="onRetry"/> on each retry with the raised exception and
        /// execution context.
        /// </summary>
        /// <param name="policyBuilder">The policy builder.</param>
        /// <param name="sleepDurationProvider">A function providing the duration to wait before retrying.</param>
        /// <param name="onRetry">The action to call on each retry.</param>
        /// <returns>The policy instance.</returns>
        /// <exception cref="System.ArgumentNullException">sleepDurationProvider</exception>
        /// <exception cref="System.ArgumentNullException">onRetry</exception>
        public static RetryPolicy WaitAndRetryForever(this PolicyBuilder policyBuilder, Func <int, Context, TimeSpan> sleepDurationProvider, Action <Exception, TimeSpan, Context> onRetry)
        {
            if (sleepDurationProvider == null)
            {
                throw new ArgumentNullException(nameof(sleepDurationProvider));
            }
            if (onRetry == null)
            {
                throw new ArgumentNullException(nameof(onRetry));
            }

            return(new RetryPolicy((action, context, cancellationToken) => RetryEngine.Implementation(
                                       (ctx, ct) => { action(ctx, ct); return EmptyStruct.Instance; },
                                       context,
                                       cancellationToken,
                                       policyBuilder.ExceptionPredicates,
                                       PredicateHelper <EmptyStruct> .EmptyResultPredicates,
                                       () => new RetryStateWaitAndRetryForever <EmptyStruct>(sleepDurationProvider, (outcome, timespan, ctx) => onRetry(outcome.Exception, timespan, ctx), context)
                                       ), policyBuilder.ExceptionPredicates));
        }
Beispiel #21
0
        /// <summary>
        /// Builds a <see cref="Policy"/> that will retry <paramref name="retryCount"/> times
        /// calling <paramref name="onRetry"/> on each retry with the handled exception or result, retry count and context data.
        /// </summary>
        /// <param name="policyBuilder">The policy builder.</param>
        /// <param name="retryCount">The retry count.</param>
        /// <param name="onRetry">The action to call on each retry.</param>
        /// <returns>The policy instance.</returns>
        /// <exception cref="System.ArgumentOutOfRangeException">retryCount;Value must be greater than or equal to zero.</exception>
        /// <exception cref="System.ArgumentNullException">onRetry</exception>
        public static RetryPolicy <TResult> Retry <TResult>(this PolicyBuilder <TResult> policyBuilder, int retryCount, Action <DelegateResult <TResult>, int, Context> onRetry)
        {
            if (retryCount < 0)
            {
                throw new ArgumentOutOfRangeException("retryCount", "Value must be greater than or equal to zero.");
            }
            if (onRetry == null)
            {
                throw new ArgumentNullException("onRetry");
            }

            return(new RetryPolicy <TResult>(
                       (action, context, cancellationToken) => RetryEngine.Implementation(
                           action,
                           cancellationToken,
                           policyBuilder.ExceptionPredicates,
                           policyBuilder.ResultPredicates,
                           () => new RetryPolicyStateWithCount <TResult>(retryCount, onRetry, context)
                           ),
                       policyBuilder.ExceptionPredicates,
                       policyBuilder.ResultPredicates));
        }
        /// <summary>
        /// Builds a <see cref="Policy"/> that will wait and retry indefinitely
        /// calling <paramref name="onRetry"/> on each retry with the handled exception or result.
        /// </summary>
        /// <param name="policyBuilder">The policy builder.</param>
        /// <param name="sleepDurationProvider"></param>
        /// <param name="onRetry">The action to call on each retry.</param>
        /// <returns>The policy instance.</returns>
        /// <exception cref="System.ArgumentNullException">sleepDurationProvider</exception>
        /// <exception cref="System.ArgumentNullException">onRetry</exception>
        public static RetryPolicy <TResult> WaitAndRetryForever <TResult>(this PolicyBuilder <TResult> policyBuilder, Func <int, TimeSpan> sleepDurationProvider, Action <DelegateResult <TResult>, TimeSpan> onRetry)
        {
            if (sleepDurationProvider == null)
            {
                throw new ArgumentNullException("sleepDurationProvider");
            }
            if (onRetry == null)
            {
                throw new ArgumentNullException("onRetry");
            }

            return(new RetryPolicy <TResult>(
                       (action, context) => RetryEngine.Implementation(
                           action,
                           policyBuilder.ExceptionPredicates,
                           policyBuilder.ResultPredicates,
                           () => new RetryPolicyStateWithSleepDurationProvider <TResult>(sleepDurationProvider, onRetry)
                           ),
                       policyBuilder.ExceptionPredicates,
                       policyBuilder.ResultPredicates
                       ));
        }
        /// <summary>
        /// Builds a <see cref="Policy"/> that will wait and retry as many times as there are provided <paramref name="sleepDurations"/>
        /// calling <paramref name="onRetry"/> on each retry with the handled exception or result, current sleep duration, retry count and context data.
        /// On each retry, the duration to wait is the current <paramref name="sleepDurations"/> item.
        /// </summary>
        /// <param name="policyBuilder">The policy builder.</param>
        /// <param name="sleepDurations">The sleep durations to wait for on each retry.</param>
        /// <param name="onRetry">The action to call on each retry.</param>
        /// <returns>The policy instance.</returns>
        /// <exception cref="System.ArgumentNullException">
        /// sleepDurations
        /// or
        /// onRetry
        /// </exception>
        public static RetryPolicy <TResult> WaitAndRetry <TResult>(this PolicyBuilder <TResult> policyBuilder, IEnumerable <TimeSpan> sleepDurations, Action <DelegateResult <TResult>, TimeSpan, int, Context> onRetry)
        {
            if (sleepDurations == null)
            {
                throw new ArgumentNullException("sleepDurations");
            }
            if (onRetry == null)
            {
                throw new ArgumentNullException("onRetry");
            }

            return(new RetryPolicy <TResult>(
                       (action, context) => RetryEngine.Implementation(
                           action,
                           policyBuilder.ExceptionPredicates,
                           policyBuilder.ResultPredicates,
                           () => new RetryPolicyStateWithSleep <TResult>(sleepDurations, onRetry, context)
                           ),
                       policyBuilder.ExceptionPredicates,
                       policyBuilder.ResultPredicates
                       ));
        }
Beispiel #24
0
        /// <summary>
        ///     Builds a <see cref="Policy" /> that will wait and retry <paramref name="retryCount" /> times
        ///     calling <paramref name="onRetryAsync" /> on each retry with the raised exception, the current sleep duration and context data.
        ///     On each retry, the duration to wait is calculated by calling <paramref name="sleepDurationProvider" /> with
        ///     the current retry attempt allowing an exponentially increasing wait time (exponential backoff).
        /// </summary>
        /// <param name="policyBuilder">The policy builder.</param>
        /// <param name="retryCount">The retry count.</param>
        /// <param name="sleepDurationProvider">The function that provides the duration to wait for for a particular retry attempt.</param>
        /// <param name="onRetryAsync">The action to call asynchronously on each retry.</param>
        /// <returns>The policy instance.</returns>
        /// <exception cref="System.ArgumentOutOfRangeException">retryCount;Value must be greater than or equal to zero.</exception>
        /// <exception cref="System.ArgumentNullException">
        ///     sleepDurationProvider
        ///     or
        ///     onRetryAsync
        /// </exception>
        public static RetryPolicy WaitAndRetryAsync(this PolicyBuilder policyBuilder, int retryCount,
                                                    Func <int, TimeSpan> sleepDurationProvider, Func <Exception, TimeSpan, Context, Task> onRetryAsync)
        {
            if (retryCount < 0)
            {
                throw new ArgumentOutOfRangeException("retryCount", "Value must be greater than or equal to zero.");
            }
            if (sleepDurationProvider == null)
            {
                throw new ArgumentNullException("sleepDurationProvider");
            }
            if (onRetryAsync == null)
            {
                throw new ArgumentNullException("onRetryAsync");
            }

            IEnumerable <TimeSpan> sleepDurations = Enumerable.Range(1, retryCount)
                                                    .Select(sleepDurationProvider);

            return(new RetryPolicy(
                       (action, context, cancellationToken, continueOnCapturedContext) => RetryEngine.ImplementationAsync(
                           action,
                           cancellationToken,
                           policyBuilder.ExceptionPredicates,
                           () => new RetryPolicyStateWithSleep(sleepDurations, onRetryAsync, context),
                           continueOnCapturedContext),
                       policyBuilder.ExceptionPredicates
                       ));
        }
Beispiel #25
0
        /// <summary>
        ///     Builds a <see cref="Policy" /> that will retry indefinitely
        ///     calling <paramref name="onRetry" /> on each retry with the raised exception.
        /// </summary>
        /// <param name="policyBuilder">The policy builder.</param>
        /// <param name="onRetry">The action to call on each retry.</param>
        /// <returns>The policy instance.</returns>
        /// <exception cref="System.ArgumentNullException">onRetry</exception>
        public static RetryPolicy RetryForeverAsync(this PolicyBuilder policyBuilder, Action <Exception> onRetry)
        {
            if (onRetry == null)
            {
                throw new ArgumentNullException("onRetry");
            }

            return(new RetryPolicy(
                       (action, context, cancellationToken, continueOnCapturedContext) => RetryEngine.ImplementationAsync(
                           action,
                           cancellationToken,
                           policyBuilder.ExceptionPredicates,
#pragma warning disable 1998
                           () => new RetryPolicyState(async e => onRetry(e)),
#pragma warning restore 1998
                           continueOnCapturedContext),
                       policyBuilder.ExceptionPredicates
                       ));
        }
Beispiel #26
0
        /// <summary>
        /// Builds a <see cref="Policy"/> that will retry indefinitely
        /// calling <paramref name="onRetry"/> on each retry with the raised exception and context data.
        /// </summary>
        /// <param name="policyBuilder">The policy builder.</param>
        /// <param name="onRetry">The action to call on each retry.</param>
        /// <returns>The policy instance.</returns>
        /// <exception cref="System.ArgumentNullException">onRetry</exception>
        public static RetryPolicy RetryForeverAsync(this PolicyBuilder policyBuilder, Action <Exception, Context> onRetry)
        {
            if (onRetry == null)
            {
                throw new ArgumentNullException("onRetry");
            }

            return(new RetryPolicy((action, context, cancellationToken, continueOnCapturedContext) => RetryEngine.ImplementationAsync(
                                       action,
                                       cancellationToken,
                                       policyBuilder.ExceptionPredicates,
                                       () => new RetryPolicyState(onRetry, context),
                                       continueOnCapturedContext
                                       ), policyBuilder.ExceptionPredicates));
        }
Beispiel #27
0
        /// <summary>
        /// Builds a <see cref="Policy"/> that will wait and retry indefinitely
        /// calling <paramref name="onRetryAsync"/> on each retry with the raised exception and
        /// execution context.
        /// </summary>
        /// <param name="policyBuilder">The policy builder.</param>
        /// <param name="sleepDurationProvider"></param>
        /// <param name="onRetryAsync">The action to call asynchronously on each retry.</param>
        /// <returns>The policy instance.</returns>
        /// <exception cref="System.ArgumentNullException">sleepDurationProvider</exception>
        /// <exception cref="System.ArgumentNullException">onRetryAsync</exception>
        public static RetryPolicy WaitAndRetryForeverAsync(this PolicyBuilder policyBuilder, Func <int, TimeSpan> sleepDurationProvider, Func <Exception, TimeSpan, Context, Task> onRetryAsync)
        {
            if (sleepDurationProvider == null)
            {
                throw new ArgumentNullException("sleepDurationProvider");
            }
            if (onRetryAsync == null)
            {
                throw new ArgumentNullException("onRetryAsync");
            }

            return(new RetryPolicy(
                       (action, context, cancellationToken, continueOnCapturedContext) => RetryEngine.ImplementationAsync(
                           action,
                           cancellationToken,
                           policyBuilder.ExceptionPredicates,
                           () => new RetryPolicyStateWithSleepDurationProvider(sleepDurationProvider, onRetryAsync, context),
                           continueOnCapturedContext
                           ), policyBuilder.ExceptionPredicates));
        }
Beispiel #28
0
        /// <summary>
        /// Builds a <see cref="Policy"/> that will wait and retry indefinitely
        /// calling <paramref name="onRetry"/> on each retry with the raised exception and
        /// execution context.
        /// </summary>
        /// <param name="policyBuilder">The policy builder.</param>
        /// <param name="sleepDurationProvider"></param>
        /// <param name="onRetry">The action to call on each retry.</param>
        /// <returns>The policy instance.</returns>
        /// <exception cref="System.ArgumentNullException">sleepDurationProvider</exception>
        /// <exception cref="System.ArgumentNullException">onRetry</exception>
        public static RetryPolicy WaitAndRetryForeverAsync(this PolicyBuilder policyBuilder, Func <int, TimeSpan> sleepDurationProvider, Action <Exception, TimeSpan, Context> onRetry)
        {
            if (sleepDurationProvider == null)
            {
                throw new ArgumentNullException("sleepDurationProvider");
            }
            if (onRetry == null)
            {
                throw new ArgumentNullException("onRetry");
            }

            return(new RetryPolicy(
                       (action, context, cancellationToken, continueOnCapturedContext) => RetryEngine.ImplementationAsync(
                           action,
                           cancellationToken,
                           policyBuilder.ExceptionPredicates,
#pragma warning disable 1998
                           () => new RetryPolicyStateWithSleepDurationProvider(sleepDurationProvider, async(e, t, c) => onRetry(e, t, c), context),
#pragma warning restore 1998
                           continueOnCapturedContext
                           ), policyBuilder.ExceptionPredicates));
        }
Beispiel #29
0
        /// <summary>
        ///     Builds a <see cref="Policy" /> that will retry <paramref name="retryCount" /> times
        ///     calling <paramref name="onRetry" /> on each retry with the raised exception and retry count.
        /// </summary>
        /// <param name="policyBuilder">The policy builder.</param>
        /// <param name="retryCount">The retry count.</param>
        /// <param name="onRetry">The action to call on each retry.</param>
        /// <returns>The policy instance.</returns>
        /// <exception cref="System.ArgumentOutOfRangeException">retryCount;Value must be greater than or equal to zero.</exception>
        /// <exception cref="System.ArgumentNullException">onRetry</exception>
        public static RetryPolicy RetryAsync(this PolicyBuilder policyBuilder, int retryCount, Action <Exception, int> onRetry)
        {
            if (retryCount < 0)
            {
                throw new ArgumentOutOfRangeException("retryCount", "Value must be greater than or equal to zero.");
            }
            if (onRetry == null)
            {
                throw new ArgumentNullException("onRetry");
            }

            return(new RetryPolicy(
                       (action, context, cancellationToken, continueOnCapturedContext) => RetryEngine.ImplementationAsync(
                           action,
                           cancellationToken,
                           policyBuilder.ExceptionPredicates,
#pragma warning disable 1998
                           () => new RetryPolicyStateWithCount(retryCount, async(e, i) => onRetry(e, i)),
#pragma warning restore 1998
                           continueOnCapturedContext),
                       policyBuilder.ExceptionPredicates
                       ));
        }
Beispiel #30
0
 public void Execute(Action action, CancellationToken cancellationToken) =>
 RetryEngine.Execute(action, _maxTryCount, HandleException, cancellationToken);