public override Task OnMessageAsync(T message, RabbitMQConsumerContext consumerContext, IFeedbackSender feedbackSender,
                                            CancellationToken cancellationToken)
        {
            try
            {
                _callbackAction(message, consumerContext);
                feedbackSender.Ack();
            }
            catch (Exception e)
            {
                feedbackSender.Nack(true);
            }

            return(Task.FromResult(0));
        }
        protected override Task <bool> TryInvokeAsync(T message, RabbitMQConsumerContext consumerContext, List <Exception> exceptions,
                                                      CancellationToken cancellationToken)
        {
            try
            {
                _callbackAction(message, consumerContext);

                return(Task.FromResult(true));
            }
            catch (Exception exception)
            {
                exceptions.Add(exception);

                return(Task.FromResult(false));
            }
        }
        public override async Task OnMessageAsync(T message, RabbitMQConsumerContext consumerContext, IFeedbackSender feedbackSender, CancellationToken cancellationToken)
        {
            try
            {
                using (var tokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken))
                {
                    tokenSource.CancelAfter(_processingTimeout);

                    await _callbackFunc(message, consumerContext, tokenSource.Token).ConfigureAwait(false);
                }

                feedbackSender.Ack();
            }
            catch (Exception e)
            {
                feedbackSender.Nack(true);
            }
        }
Example #4
0
        protected override async Task <bool> TryInvokeAsync(T message, RabbitMQConsumerContext consumerContext, List <Exception> exceptions,
                                                            CancellationToken cancellationToken)
        {
            try
            {
                using (var tokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken))
                {
                    tokenSource.CancelAfter(_processingTimeout);

                    await _callbackFunc(message, consumerContext, tokenSource.Token).ConfigureAwait(false);
                }

                return(true);
            }
            catch (Exception exception)
            {
                exceptions.Add(exception);

                return(false);
            }
        }
Example #5
0
        public override async Task OnMessageAsync(T message, RabbitMQConsumerContext consumerContext, IFeedbackSender feedbackSender,
                                                  CancellationToken cancellationToken)
        {
            var invocationSuccess = false;
            var exceptions        = new List <Exception>();

            var tryCount = 0;

            while (tryCount == 0 || (!invocationSuccess && ShouldRetry(tryCount, exceptions)))
            {
                if (tryCount > 0 && InvokeRetryWaitMilliseconds > 0)
                {
                    await Task.Delay(InvokeRetryWaitMilliseconds, cancellationToken).ConfigureAwait(false);
                }

                tryCount++;

                invocationSuccess = await TryInvokeAsync(message, consumerContext, exceptions, cancellationToken).ConfigureAwait(false);
            }

            if (invocationSuccess)
            {
                feedbackSender.Ack();
            }
            else if (ShouldRequeue(exceptions))
            {
                feedbackSender.Nack(true);

                Logger.LogWarning(new AggregateException(exceptions), $"Message successfully processed, but exceptions were thrown after {tryCount} tries");
            }
            else
            {
                feedbackSender.Nack(false);

                Logger.LogError(new AggregateException(exceptions), $"Unable to successfully process message after {tryCount} tries");
            }
        }
 public abstract Task OnMessageAsync(T message, RabbitMQConsumerContext consumerContext, IFeedbackSender feedbackSender, CancellationToken cancellationToken);
Example #7
0
 protected abstract Task <bool> TryInvokeAsync(T message, RabbitMQConsumerContext consumerContext, List <Exception> exceptions,
                                               CancellationToken cancellationToken);