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); RetrySynchronizationManager.Register(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 != null && recovered.Value) { callbackResult = (T)retryContext.GetAttribute(RECOVERED_RESULT); } } return(callbackResult); }, context); CallListenerClose(retryContext, retryContext.LastException); RetrySynchronizationManager.Clear(); return(result); }
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); RetrySynchronizationManager.Register(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); RetrySynchronizationManager.Clear(); }
/// <summary>Actions to take after final attempt has failed. If there is state clean /// up the cache. If there is a recovery callback, execute that and return /// its result. Otherwise throw an exception.</summary> /// <param name="recoveryCallback">The callback for recovery (might be null).</param> /// <param name="context">The current retry context.</param> /// <param name="state">The state.</param> /// <returns>The T.</returns> /// <typeparam name="T">Type T.</typeparam> protected T HandleRetryExhausted <T>(IRecoveryCallback <T> recoveryCallback, IRetryContext context, IRetryState state) { if (state != null) { this.retryContextCache.Remove(state.GetKey()); } if (recoveryCallback != null) { return(recoveryCallback.Recover(context)); } if (state != null) { Logger.Debug(m => m("Retry exhausted after last attempt with no recovery path.")); throw new ExhaustedRetryException("Retry exhausted after last attempt with no recovery path", context.LastException); } throw WrapIfNecessary(context.LastException); }
public abstract void Execute(Action <IRetryContext> retryCallback, IRecoveryCallback recoveryCallback);
public abstract T Execute <T>(Func <IRetryContext, T> retryCallback, IRecoveryCallback <T> recoveryCallback);
public RecoveryCallbackWrapper(IRecoveryCallback recoveryCallback) { _recoveryCallback = recoveryCallback; }
/// <summary>The execute.</summary> /// <param name="retryCallback">The retry callback.</param> /// <param name="recoveryCallback">The recovery callback.</param> /// <param name="retryState">The retry state.</param> /// <returns>The T.</returns> /// <typeparam name="T">Type T.</typeparam> public T Execute <T>(Func <IRetryContext, T> retryCallback, IRecoveryCallback <T> recoveryCallback, IRetryState retryState) { return(this.DoExecute <T>(retryCallback, recoveryCallback, retryState)); }
/// <summary>The execute.</summary> /// <param name="retryCallback">The retry callback.</param> /// <param name="recoveryCallback">The recovery callback.</param> /// <returns>The T.</returns> /// <typeparam name="T">Type T.</typeparam> public T Execute <T>(IRetryCallback <T> retryCallback, IRecoveryCallback <T> recoveryCallback) { return(this.DoExecute <T>(retryCallback, recoveryCallback, null)); }