Exemplo n.º 1
0
        public override void Execute(Action <IRetryContext> retryCallback, IRecoveryCallback recoveryCallback)
        {
            var policy       = BuildPolicy <object>();
            var retryContext = new RetryContext();
            var context      = new Context();

            context.Add(RETRYCONTEXT_KEY, retryContext);
            if (recoveryCallback != null)
            {
                retryContext.SetAttribute(RECOVERY_CALLBACK_KEY, recoveryCallback);
            }

            if (!CallListenerOpen(retryContext))
            {
                throw new TerminatedRetryException("Retry terminated abnormally by interceptor before first attempt");
            }

            policy.Execute(
                (ctx) =>
            {
                retryCallback(retryContext);
                return(null);
            }, context);

            CallListenerClose(retryContext, retryContext.LastException);
        }
Exemplo n.º 2
0
        public override T Execute <T>(Func <IRetryContext, T> retryCallback, IRecoveryCallback <T> recoveryCallback)
        {
            var policy       = BuildPolicy <T>();
            var retryContext = new RetryContext();
            var context      = new Context();

            context.Add(RETRYCONTEXT_KEY, retryContext);
            if (recoveryCallback != null)
            {
                retryContext.SetAttribute(RECOVERY_CALLBACK_KEY, recoveryCallback);
            }

            CallListenerOpen(retryContext);
            var result = policy.Execute(
                (ctx) =>
            {
                var callbackResult = retryCallback(retryContext);
                if (recoveryCallback != null)
                {
                    var recovered = (bool)retryContext.GetAttribute(RECOVERED);
                    if (recovered)
                    {
                        callbackResult = (T)retryContext.GetAttribute(RECOVERED_RESULT);
                    }
                }

                return(callbackResult);
            }, context);

            CallListenerClose(retryContext, retryContext.LastException);
            return(result);
        }
Exemplo n.º 3
0
 private void CallListenerOnError(RetryContext context, Exception ex)
 {
     foreach (var listener in listeners)
     {
         listener.OnError(context, ex);
     }
 }
Exemplo n.º 4
0
 private void CallListenerClose(RetryContext context, Exception ex)
 {
     context.SetAttribute(CLOSED, true);
     foreach (var listener in listeners)
     {
         listener.Close(context, ex);
     }
 }
Exemplo n.º 5
0
        private bool CallListenerOpen(RetryContext context)
        {
            var running = true;

            foreach (var listener in listeners)
            {
                running &= listener.Open(context);
            }

            return(running);
        }