Exemplo n.º 1
0
        public int ResendSmsMessage()
        {
            var dateTime = _dateTimeProvider.OffsetNow.AddMinutes(-1);

            var messages = _repository.GetAll()
                           .Where(x => x.SentDateTime == null && x.RetriedCount < 3)
                           .Where(x => (x.RetriedCount == 0 && x.CreatedDateTime < dateTime) || (x.RetriedCount != 0 && x.UpdatedDateTime < dateTime))
                           .ToList();

            if (messages.Any())
            {
                foreach (var sms in messages)
                {
                    _smsMessageCreatedEventSender.Send(new SmsMessageCreatedEvent {
                        Id = sms.Id
                    });

                    sms.RetriedCount++;

                    _repository.AddOrUpdate(sms);
                    _repository.UnitOfWork.SaveChanges();
                }
            }
            else
            {
                _logger.LogInformation("No SMS to resend.");
            }

            return(messages.Count);
        }
        public async Task <int> ResendSmsMessageAsync()
        {
            var dateTime = _dateTimeProvider.OffsetNow.AddMinutes(-1);

            var messages = _repository.GetAll()
                           .Where(x => x.SentDateTime == null && x.RetriedCount < 3)
                           .Where(x => (x.RetriedCount == 0 && x.CreatedDateTime < dateTime) || (x.RetriedCount != 0 && x.UpdatedDateTime < dateTime))
                           .ToList();

            if (messages.Any())
            {
                foreach (var sms in messages)
                {
                    await _smsMessageCreatedEventSender.SendAsync(new SmsMessageCreatedEvent { Id = sms.Id });

                    _repository.IncreaseRetry(sms.Id);
                }
            }
            else
            {
                _logger.LogInformation("No SMS to resend.");
            }

            return(messages.Count);
        }
Exemplo n.º 3
0
        public async Task <int> SendSmsMessagesAsync()
        {
            var circuit = _circuitBreakerManager.GetCircuitBreaker("SmsService", TimeSpan.FromMinutes(1));

            circuit.EnsureOkStatus();

            var deplayedTimes = new[]
            {
                TimeSpan.FromMinutes(1),
                TimeSpan.FromMinutes(2),
                TimeSpan.FromMinutes(3),
                TimeSpan.FromMinutes(5),
                TimeSpan.FromMinutes(8),
                TimeSpan.FromMinutes(13),
                TimeSpan.FromMinutes(21),
                TimeSpan.FromMinutes(34),
                TimeSpan.FromMinutes(55),
                TimeSpan.FromMinutes(89),
            };

            var dateTime            = _dateTimeProvider.OffsetNow;
            var defaultAttemptCount = 5;

            var messages = _repository.GetAll()
                           .Where(x => x.SentDateTime == null)
                           .Where(x => x.ExpiredDateTime == null || x.ExpiredDateTime > dateTime)
                           .Where(x => (x.MaxAttemptCount == 0 && x.AttemptCount < defaultAttemptCount) || x.AttemptCount < x.MaxAttemptCount)
                           .Where(x => x.NextAttemptDateTime == null || x.NextAttemptDateTime <= dateTime)
                           .ToList();

            if (messages.Any())
            {
                foreach (var sms in messages)
                {
                    string log = Environment.NewLine + Environment.NewLine
                                 + $"[{_dateTimeProvider.OffsetNow.ToString(CultureInfo.InvariantCulture)}] ";
                    try
                    {
                        await _smsNotification.SendAsync(sms);

                        sms.SentDateTime = _dateTimeProvider.OffsetNow;
                        sms.Log         += log + "Succeed.";

                        _circuitBreakerManager.LogSuccess(circuit);
                    }
                    catch (Exception ex)
                    {
                        sms.Log += log + ex.ToString();
                        sms.NextAttemptDateTime = _dateTimeProvider.OffsetNow + deplayedTimes[sms.AttemptCount];

                        _circuitBreakerManager.LogFailure(circuit, 5, TimeSpan.FromMinutes(5));
                    }

                    sms.AttemptCount   += 1;
                    sms.Log             = sms.Log.Trim();
                    sms.UpdatedDateTime = _dateTimeProvider.OffsetNow;

                    if (sms.MaxAttemptCount == 0)
                    {
                        sms.MaxAttemptCount = defaultAttemptCount;
                    }

                    await _repository.UnitOfWork.SaveChangesAsync();

                    circuit.EnsureOkStatus();
                }
            }
            else
            {
                _logger.LogInformation("No SMS to send.");
            }

            return(messages.Count);
        }