Example #1
0
        public async Task Send(ConsumeContext <T> context, IPipe <ConsumeContext <T> > next)
        {
            try
            {
                await next.Send(context);
            }
            catch (Exception ex)
            {
                if (!_retryPolicy.CanRetry(ex))
                {
                    throw;
                }

                try
                {
                    MessageRedeliveryContext redeliveryContext;
                    if (!context.TryGetPayload(out redeliveryContext))
                    {
                        throw new ContextException("The message redelivery context was not available to delay the message", ex);
                    }

                    TimeSpan delay;
                    using (IRetryContext retryContext = _retryPolicy.GetRetryContext())
                    {
                        retryContext.CanRetry(ex, out delay);
                    }

                    await redeliveryContext.ScheduleRedelivery(delay);
                }
                catch (Exception redeliveryException)
                {
                    throw new ContextException("The message delivery could not be rescheduled", new AggregateException(redeliveryException, ex));
                }
            }
        }
Example #2
0
        async Task Attempt(RetryConsumeContext context, IPipe <ConsumeContext> next)
        {
            context.ClearPendingFaults();

            TimeSpan delay;

            try
            {
                await next.Send(context).ConfigureAwait(false);

                return;
            }
            catch (Exception ex)
            {
                if (!_retryPolicy.CanRetry(ex))
                {
                    context.NotifyPendingFaults();
                    throw;
                }

                // by not adding the retry payload until the exception occurs, the deepest retry filter
                // is the one to set the actual retry context with the deepest configured policy
                IRetryContext retryContext = context.GetOrAddPayload(() => _retryPolicy.GetRetryContext());
                if (!retryContext.CanRetry(ex, out delay))
                {
                    context.NotifyPendingFaults();
                    throw;
                }
            }

            await Task.Delay(delay).ConfigureAwait(false);

            await Attempt(context, next).ConfigureAwait(false);
        }
Example #3
0
        public bool CanRetry(Exception exception, out TimeSpan delay)
        {
            bool canRetry = _policy.CanRetry(exception);

            if (canRetry && _retryNumber < _delays.Length)
            {
                delay = _delays[_retryNumber++];
                return(true);
            }

            delay = TimeSpan.Zero;
            return(false);
        }
Example #4
0
 private void RunInternal(Func <CancellationToken, Task> runFunction, IRetryPolicy retryPolicy)
 {
     this.processingTask = runFunction(this.cancellationTokenSource.Token);
     this.processingTask.ContinueWith(
         t =>
     {
         t.Exception.Handle(_ => true);
         if (retryPolicy.CanRetry(t.Exception))
         {
             // Run again if we were allowed to
             this.RunInternal(runFunction, retryPolicy);
         }
     },
         this.cancellationTokenSource.Token,
         TaskContinuationOptions.OnlyOnFaulted,
         TaskScheduler.Current);
 }
        public bool CanRetry(Exception exception, out TimeSpan delay)
        {
            bool canRetry = _policy.CanRetry(exception);

            if (_enumerator == null)
            {
                _enumerator = _delays.GetEnumerator();
            }

            if (canRetry && _enumerator.MoveNext())
            {
                delay = _enumerator.Current;
                return(true);
            }

            delay = TimeSpan.Zero;
            return(false);
        }
Example #6
0
        public void Execute(MessageContext context, Action next, MiddlewareParameter parameter)
        {
            IRetryPolicy policy = null;

            try
            {
                if (HasRetry(parameter.Route))
                {
                    policy = GetRetryPolicy(parameter.Route);

                    if (policy != null)
                    {
                        var interval = policy.NextRetryInterval(context.RetryCount + 1);

                        context.LastRetry = !policy.CanRetry(context.RetryCount + 1, interval);
                    }
                    else
                    {
                        Console.WriteLine("The retry policy cannot be null");

                        throw new ApplicationException("The retry policy cannot be null");
                    }
                }

                next();
            }
            catch (Exception ex)
            {
                if (policy != null)
                {
                    if (parameter.Route.RetryExceptionType == ex.GetType())
                    {
                        if (!context.LastRetry)
                        {
                            if (!string.IsNullOrWhiteSpace(parameter.Route.OnRetryEndPoint))
                            {
                                SendRetry(parameter.Route, context, policy);
                            }
                            else
                            {
                                if (!string.IsNullOrWhiteSpace(parameter.Route.OnErrorEndPoint))
                                {
                                    SendError(parameter.Route, context, ex);
                                }
                                else
                                {
                                    throw;
                                }
                            }
                        }
                        else
                        {
                            if (!string.IsNullOrWhiteSpace(parameter.Route.OnErrorEndPoint))
                            {
                                SendError(parameter.Route, context, ex);
                            }
                            else
                            {
                                throw;
                            }
                        }
                    }
                    else
                    {
                        if (!string.IsNullOrWhiteSpace(parameter.Route.OnErrorEndPoint))
                        {
                            SendError(parameter.Route, context, ex);
                        }
                        else
                        {
                            throw;
                        }
                    }
                }
                else
                {
                    if (!string.IsNullOrWhiteSpace(parameter.Route.OnErrorEndPoint))
                    {
                        SendError(parameter.Route, context, ex);
                    }
                    else
                    {
                        throw;
                    }
                }
            }
        }
Example #7
0
 private static bool WillRetry(Exception exception, CancellationToken cancellationToken, IRetryStrategy strategy, IRetryPolicy policy)
 {
     return(strategy.CanRetry && !cancellationToken.IsCancellationRequested && policy.CanRetry(exception));
 }
Example #8
0
        /**
         * Decide whether to proceed with the ongoing retry attempt. This method is
         * called before the {@link RetryCallback} is executed, but after the
         * backoff and open interceptors.
         *
         * @param retryPolicy the policy to apply
         * @param context the current retry context
         * @return true if we can continue with the attempt
         */

        /// <summary>The can retry.</summary>
        /// <param name="retryPolicy">The retry policy.</param>
        /// <param name="context">The context.</param>
        /// <returns>The System.Boolean.</returns>
        protected bool CanRetry(IRetryPolicy retryPolicy, IRetryContext context)
        {
            return(retryPolicy.CanRetry(context));
        }
Example #9
0
        public void Execute(MessageContext messagecontext, Action <MessageContext, MiddlewareContext> next, MiddlewareContext middlewarecontext)
        {
            IRetryPolicy policy = null;

            try
            {
                if (HasRetry(messagecontext.Route))
                {
                    policy = GetRetryPolicy(messagecontext.Route);

                    if (policy != null)
                    {
                        var interval = policy.NextRetryInterval(messagecontext.RetryCount + 1);

                        messagecontext.LastRetry = !policy.CanRetry(messagecontext.RetryCount + 1, interval);
                    }
                    else
                    {
                        throw new ApplicationException("The retry policy cannot be null");
                    }
                }

                next(messagecontext, middlewarecontext);
            }
            catch (Exception ex)
            {
                if (policy != null)
                {
                    if (messagecontext.Route.RetryExceptionType == ex.GetType())
                    {
                        if (!messagecontext.LastRetry)
                        {
                            if (!string.IsNullOrWhiteSpace(messagecontext.Route.OnRetryEndPoint))
                            {
                                SendRetry(messagecontext.Route, messagecontext, policy);
                            }
                            else
                            {
                                if (!string.IsNullOrWhiteSpace(messagecontext.Route.OnErrorEndPoint))
                                {
                                    SendError(messagecontext.Route, messagecontext, ex);
                                }
                                else
                                {
                                    throw;
                                }
                            }
                        }
                        else
                        {
                            if (!string.IsNullOrWhiteSpace(messagecontext.Route.OnErrorEndPoint))
                            {
                                SendError(messagecontext.Route, messagecontext, ex);
                            }
                            else
                            {
                                throw;
                            }
                        }
                    }
                    else
                    {
                        if (!string.IsNullOrWhiteSpace(messagecontext.Route.OnErrorEndPoint))
                        {
                            SendError(messagecontext.Route, messagecontext, ex);
                        }
                        else
                        {
                            throw;
                        }
                    }
                }
                else
                {
                    if (!string.IsNullOrWhiteSpace(messagecontext.Route.OnErrorEndPoint))
                    {
                        SendError(messagecontext.Route, messagecontext, ex);
                    }
                    else
                    {
                        throw;
                    }
                }
            }
        }
Example #10
0
 public bool CanRetry(Exception exception)
 {
     return(!_cancellationToken.IsCancellationRequested && _retryPolicy.CanRetry(exception));
 }