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); }
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); }
private void CallListenerOnError(RetryContext context, Exception ex) { foreach (var listener in listeners) { listener.OnError(context, ex); } }
private void CallListenerClose(RetryContext context, Exception ex) { context.SetAttribute(CLOSED, true); foreach (var listener in listeners) { listener.Close(context, ex); } }
private bool CallListenerOpen(RetryContext context) { var running = true; foreach (var listener in listeners) { running &= listener.Open(context); } return(running); }