Пример #1
0
            public override async Task InvokeAsync(IPipeContext context,
                                                   CancellationToken token = new CancellationToken())
            {
                using (var scope = _serviceProvider.CreateScope())
                {
                    var messageProcessor = scope.ServiceProvider.GetRequiredService <IMessageProcessor>();
                    var messageId        = context.GetDeliveryEventArgs().BasicProperties.MessageId;
                    _logger.LogTrace($"Received a unique message with id: {messageId} to be processed.");
                    if (!await messageProcessor.TryProcessAsync(messageId))
                    {
                        _logger.LogTrace($"A unique message with id: {messageId} was already processed.");
                        return;
                    }

                    try
                    {
                        _logger.LogTrace($"Processing a unique message with id: {messageId}...");
                        await Next.InvokeAsync(context, token);

                        _logger.LogTrace($"Processed a unique message with id: {messageId}.");
                    }
                    catch
                    {
                        _logger.LogTrace($"There was an error when processing a unique message with id: {messageId}.");
                        await messageProcessor.RemoveAsync(messageId);

                        throw;
                    }
                }
            }
        protected virtual Task AckMessageIfApplicable(IPipeContext context)
        {
            var autoAck = context.GetConsumeConfiguration()?.AutoAck;

            if (!autoAck.HasValue)
            {
                _logger.Debug("Unable to ack original message. Can not determine if AutoAck is configured.");
                return(Task.FromResult(0));
            }
            if (autoAck.Value)
            {
                _logger.Debug("Consuming in AutoAck mode. No ack'ing will be performed");
                return(Task.FromResult(0));
            }
            var deliveryTag = context.GetDeliveryEventArgs()?.DeliveryTag;

            if (deliveryTag == null)
            {
                _logger.Info("Unable to ack original message. Delivery tag not found.");
                return(Task.FromResult(0));
            }
            var consumerChannel = context.GetConsumer()?.Model;

            if (consumerChannel != null && consumerChannel.IsOpen && deliveryTag.HasValue)
            {
                _logger.Debug("Acking message with {deliveryTag} on channel {channelNumber}", deliveryTag, consumerChannel.ChannelNumber);
                consumerChannel.BasicAck(deliveryTag.Value, false);
            }
            return(Task.FromResult(0));
        }
        protected virtual Task PublishToErrorExchangeAsync(IPipeContext context, IModel channel, Exception exception, ExchangeDeclaration exchange)
        {
            var args = context.GetDeliveryEventArgs();

            args.BasicProperties.Headers?.TryAdd(PropertyHeaders.Host, Environment.MachineName);
            args.BasicProperties.Headers?.TryAdd(PropertyHeaders.ExceptionType, exception.GetType().Name);
            args.BasicProperties.Headers?.TryAdd(PropertyHeaders.ExceptionStackTrace, exception.StackTrace);
            channel.BasicPublish(exchange.Name, args.RoutingKey, false, args.BasicProperties, args.Body);
            return(Task.FromResult(0));
        }