Exemplo n.º 1
0
        private async Task ProcessPublishedAsync(IStorageConnection connection, ProcessingContext context)
        {
            var messages = await connection.GetPublishedMessagesOfNeedRetry();

            var hasException = false;

            foreach (var message in messages)
            {
                if (message.Retries > _options.FailedRetryCount)
                {
                    continue;
                }

                if (!hasException)
                {
                    try
                    {
                        _options.FailedCallback?.Invoke(MessageType.Publish, message.Name, message.Content);
                    }
                    catch (Exception ex)
                    {
                        hasException = true;
                        _logger.LogWarning("Failed call-back method raised an exception:" + ex.Message);
                    }
                }

                using (var transaction = connection.CreateTransaction())
                {
                    try
                    {
                        await _publishExecutor.PublishAsync(message.Name, message.Content);

                        _stateChanger.ChangeState(message, new SucceededState(), transaction);
                    }
                    catch (Exception e)
                    {
                        message.Content = Helper.AddExceptionProperty(message.Content, e);
                        transaction.UpdateMessage(message);
                    }

                    await transaction.CommitAsync();
                }

                context.ThrowIfStopping();

                await context.WaitAsync(_delay);
            }
        }
Exemplo n.º 2
0
        private async Task ProcessPublishedAsync(IStorageConnection connection, ProcessingContext context)
        {
            var messages = await connection.GetPublishedMessagesOfNeedRetry();

            var hasException = false;

            foreach (var message in messages)
            {
                if (message.Retries > _options.FailedRetryCount)
                {
                    continue;
                }

                using (var transaction = connection.CreateTransaction())
                {
                    var result = await _publishExecutor.PublishAsync(message.Name, message.Content);

                    if (result.Succeeded)
                    {
                        _stateChanger.ChangeState(message, new SucceededState(), transaction);
                        _logger.LogInformation("The message was sent successfully during the retry. MessageId:" + message.Id);
                    }
                    else
                    {
                        message.Content = Helper.AddExceptionProperty(message.Content, result.Exception);
                        message.Retries++;
                        if (message.StatusName == StatusName.Scheduled)
                        {
                            message.ExpiresAt  = GetDueTime(message.Added, message.Retries);
                            message.StatusName = StatusName.Failed;
                        }
                        transaction.UpdateMessage(message);

                        if (message.Retries >= _options.FailedRetryCount)
                        {
                            _logger.LogError($"The message still sent failed after {_options.FailedRetryCount} retries. We will stop retrying the message. " +
                                             "MessageId:" + message.Id);
                            if (message.Retries == _options.FailedRetryCount)
                            {
                                if (!hasException)
                                {
                                    try
                                    {
                                        _options.FailedThresholdCallback?.Invoke(MessageType.Publish, message.Name, message.Content);
                                    }
                                    catch (Exception ex)
                                    {
                                        hasException = true;
                                        _logger.LogWarning("Failed call-back method raised an exception:" + ex.Message);
                                    }
                                }
                            }
                        }
                    }
                    await transaction.CommitAsync();
                }

                context.ThrowIfStopping();

                await context.WaitAsync(_delay);
            }
        }