示例#1
0
        public override async Task <TResult> ExecuteAsync(IAsyncBotOperation <TResult> operation,
                                                          ExecutionContext context, CancellationToken token)
        {
            var currentAttempt = 1;
            var tryResult      = TryResult <TResult> .Default;

            while (RetryBotUtils.ShouldExecute(tryResult, base.Configuration, currentAttempt, token))
            {
                tryResult = await this.TryAsync(operation, context, token)
                            .ConfigureAwait(context.BotPolicyConfiguration.ContinueOnCapturedContext);

                if (tryResult.IsSucceeded)
                {
                    if (currentAttempt > 1)
                    {
                        await base.Configuration.RaiseRetrySucceededEventAsync(tryResult.OperationResult, AttemptContext.New(currentAttempt, TimeSpan.Zero, context), token)
                        .ConfigureAwait(context.BotPolicyConfiguration.ContinueOnCapturedContext);
                    }

                    return(tryResult.OperationResult);
                }

                if (RetryBotUtils.HasMaxAttemptsReached(base.Configuration, currentAttempt))
                {
                    break;
                }

                var nextDelay = base.Configuration.CalculateNextDelay(currentAttempt, tryResult.Exception, tryResult.OperationResult);
                await base.Configuration.RaiseRetryEventAsync(tryResult.OperationResult, tryResult.Exception,
                                                              AttemptContext.New(currentAttempt, nextDelay, context), token)
                .ConfigureAwait(context.BotPolicyConfiguration.ContinueOnCapturedContext);

                await RetryBotUtils.WaitAsync(nextDelay, context, token)
                .ConfigureAwait(context.BotPolicyConfiguration.ContinueOnCapturedContext);

                currentAttempt++;
            }

            token.ThrowIfCancellationRequested();

            await base.Configuration.RaiseAsyncRetryLimitReachedEvent(tryResult.Exception, context, token)
            .ConfigureAwait(context.BotPolicyConfiguration.ContinueOnCapturedContext);

            throw new MaxRetryAttemptsReachedException(Constants.MaxRetryExceptionMessage, tryResult.Exception, tryResult.OperationResult);
        }
示例#2
0
        public override void Execute(IBotOperation operation,
                                     ExecutionContext context, CancellationToken token)
        {
            var currentAttempt = 1;
            var tryResult      = TryResult.Default;

            while (RetryBotUtils.ShouldExecute(tryResult, base.Configuration, currentAttempt, token))
            {
                tryResult = this.Try(operation, context, token);

                if (tryResult.IsSucceeded)
                {
                    if (currentAttempt > 1)
                    {
                        base.Configuration.RaiseRetrySucceededEvent(AttemptContext.New(currentAttempt, TimeSpan.Zero, context));
                    }

                    return;
                }

                if (RetryBotUtils.HasMaxAttemptsReached(base.Configuration, currentAttempt))
                {
                    break;
                }

                var nextDelay = base.Configuration.CalculateNextDelay(currentAttempt, tryResult.Exception);
                base.Configuration.RaiseRetryEvent(tryResult.Exception, AttemptContext.New(currentAttempt, nextDelay, context));

                RetryBotUtils.Wait(nextDelay, token);
                currentAttempt++;
            }

            token.ThrowIfCancellationRequested();

            base.Configuration.RaiseRetryLimitReachedEvent(tryResult.Exception, context);
            throw new MaxRetryAttemptsReachedException(Constants.MaxRetryExceptionMessage, tryResult.Exception, null);
        }