예제 #1
0
        public EmailRetryPolicy(IEmailService emailService, ILogger logger)
        {
            _emailService = emailService;
            _logger       = logger;
            _policy       = Policy
                            .Handle <Exception>()
                            .OrResult <Task <EmailResponse> >(response => response.Result.IsOk().Equals(false))
                            .WaitAndRetry(retryCount: 3,
                                          sleepDurationProvider: (attempt) => TimeSpan.FromSeconds(Math.Pow(2, attempt)),
                                          onRetry: (outcome, timespan, retryCount, context) =>
            {
                var error = outcome.Result.Result.Message;
                switch (retryCount)
                {
                case 1:
                    _logger.LogInfo($"Falhou 1 vez, 1o retry | Erro: {error}");
                    break;

                case 2:
                    _logger.LogWarn($"Falhou 2 vezes, 2o retry | Erro: {error}");
                    _emailService.ChangeSmtpAsync();
                    break;

                case 3:
                    _logger.LogFatal($"Falhou 3 vezes, 3o e ultimo retry | Erro: {error}");
                    _emailService.CircuitBreaker().EnableAsync();
                    break;
                }
            });
        }