Example #1
0
 private async Task <AckStrategy> InvokeUserMessageHandlerInternalAsync(
     ConsumerExecutionContext context, CancellationToken cancellationToken
     )
 {
     try
     {
         try
         {
             return(await context.Handler(
                        context.Body, context.Properties, context.ReceivedInfo, cancellationToken
                        ).ConfigureAwait(false));
         }
         catch (OperationCanceledException)
         {
             return(consumerErrorStrategy.HandleConsumerCancelled(context));
         }
         catch (Exception exception)
         {
             return(consumerErrorStrategy.HandleConsumerError(context, exception));
         }
     }
     catch (Exception exception)
     {
         logger.Error(exception, "Consumer error strategy has failed");
         return(AckStrategies.NackWithRequeue);
     }
 }
Example #2
0
        protected virtual AckStrategy GetAckStrategy(ConsumerExecutionContext context, Task task)
        {
            try
            {
                if (task.IsFaulted)
                {
                    logger.Error(
                        task.Exception,
                        "Exception thrown by subscription callback, receivedInfo={receivedInfo}, properties={properties}, message={message}",
                        context.Info,
                        context.Properties,
                        Convert.ToBase64String(context.Body)
                        );
                    return(consumerErrorStrategy.HandleConsumerError(context, task.Exception));
                }

                if (task.IsCanceled)
                {
                    return(consumerErrorStrategy.HandleConsumerCancelled(context));
                }

                return(AckStrategies.Ack);
            }
            catch (Exception exception)
            {
                logger.Error(exception, "Consumer error strategy has failed");
                return(AckStrategies.NackWithRequeue);
            }
        }
Example #3
0
        private async Task <AckStrategy> InvokeUserMessageHandlerInternalAsync(ConsumerExecutionContext context)
        {
            try
            {
                try
                {
                    await context.UserHandler(context.Body, context.Properties, context.Info).ConfigureAwait(false);
                }
                catch (OperationCanceledException)
                {
                    return(consumerErrorStrategy.HandleConsumerCancelled(context));
                }
                catch (Exception exception)
                {
                    logger.Error(
                        exception,
                        "Exception thrown by subscription callback, receivedInfo={receivedInfo}, properties={properties}, message={message}",
                        context.Info,
                        context.Properties,
                        Convert.ToBase64String(context.Body)
                        );
                    return(consumerErrorStrategy.HandleConsumerError(context, exception));
                }
            }
            catch (Exception exception)
            {
                logger.Error(exception, "Consumer error strategy has failed");
                return(AckStrategies.NackWithRequeue);
            }

            return(AckStrategies.Ack);
        }
        public When_a_user_handler_is_cancelled()
        {
            consumerErrorStrategy = Substitute.For <IConsumerErrorStrategy>();
            consumerErrorStrategy.HandleConsumerCancelled(null).ReturnsForAnyArgs(AckStrategies.Ack);

            var handlerRunner = new HandlerRunner(consumerErrorStrategy);

            var consumer = Substitute.For <IBasicConsumer>();

            channel = Substitute.For <IModel>();
            consumer.Model.Returns(channel);

            context = new ConsumerExecutionContext(
                async(body, properties, info) => throw new OperationCanceledException(),
                messageInfo,
                messageProperties,
                messageBody
                );

            var handlerTask = handlerRunner.InvokeUserMessageHandlerAsync(context)
                              .ContinueWith(async x =>
            {
                var ackStrategy = await x.ConfigureAwait(false);
                return(ackStrategy(channel, 42));
            }, TaskContinuationOptions.ExecuteSynchronously)
                              .Unwrap();

            if (!handlerTask.Wait(5000))
            {
                throw new TimeoutException();
            }
        }
Example #5
0
        private AckStrategy GetAckStrategy(ConsumerExecutionContext context, Task task)
        {
            var ackStrategy = AckStrategies.Ack;

            try
            {
                if (task.IsFaulted)
                {
                    Logger.Error(BuildErrorMessage(context, task.Exception));
                    ackStrategy = consumerErrorStrategy.HandleConsumerError(context, task.Exception);
                }
                else if (task.IsCanceled)
                {
                    ackStrategy = consumerErrorStrategy.HandleConsumerCancelled(context);
                }
            }
            catch (Exception consumerErrorStrategyError)
            {
                Logger.Error("Exception in ConsumerErrorStrategy:\n{0}", consumerErrorStrategyError);
                return(AckStrategies.Nothing);
            }
            return(ackStrategy);
        }
Example #6
0
        protected virtual AckStrategy GetAckStrategy(ConsumerExecutionContext context, Task task)
        {
            var ackStrategy = AckStrategies.Ack;

            try
            {
                if (task.IsFaulted)
                {
                    logger.ErrorWrite(BuildErrorMessage(context, task.Exception).EscapeBraces());
                    ackStrategy = consumerErrorStrategy.HandleConsumerError(context, task.Exception);
                }
                else if (task.IsCanceled)
                {
                    ackStrategy = consumerErrorStrategy.HandleConsumerCancelled(context);
                }
            }
            catch (Exception consumerErrorStrategyError)
            {
                logger.ErrorWrite("Exception in ConsumerErrorStrategy:\n{0}",
                                  consumerErrorStrategyError);
                ackStrategy = AckStrategies.Nothing;
            }
            return(ackStrategy);
        }