コード例 #1
0
        public async Task SendSmsMessageAsync(Guid id)
        {
            var smsMessage = _repository.GetAll().FirstOrDefault(x => x.Id == id);

            if (smsMessage != null && !smsMessage.SentDateTime.HasValue)
            {
                try
                {
                    await _smsNotification.SendAsync(smsMessage);

                    _repository.UpdateSent(smsMessage.Id);
                }
                catch (Exception ex)
                {
                    _repository.UpdateFailed(smsMessage.Id, Environment.NewLine + Environment.NewLine + ex.ToString());
                }
            }
        }
コード例 #2
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);
        }