Exemplo n.º 1
0
        public Task Send(ConsumeContext <TData> context)
        {
            using (RetryPolicyContext <ConsumeContext <TData> > policyContext = _retryPolicy.CreatePolicyContext(context))
            {
                var exception = new SagaException("An existing saga instance was not found", typeof(TInstance), typeof(TData), context.CorrelationId ?? Guid
                                                  .Empty);

                if (!policyContext.CanRetry(exception, out RetryContext <ConsumeContext <TData> > retryContext))
                {
                    return(_finalPipe.Send(context));
                }

                int previousDeliveryCount = context.GetRedeliveryCount();
                for (int retryIndex = 0; retryIndex < previousDeliveryCount; retryIndex++)
                {
                    if (!retryContext.CanRetry(exception, out retryContext))
                    {
                        return(_finalPipe.Send(context));
                    }
                }

                var schedulerContext = context.GetPayload <MessageSchedulerContext>();

                MessageRedeliveryContext redeliveryContext = new ScheduleMessageRedeliveryContext <TData>(context, schedulerContext);

                var delay = retryContext.Delay ?? TimeSpan.Zero;

                return(redeliveryContext.ScheduleRedelivery(delay));
            }
        }
Exemplo n.º 2
0
        public async Task Consume(ConsumeContext <ISimpleMessage> context)
        {
            var retryAttempt    = context.GetRetryAttempt();
            var retryCount      = context.GetRetryCount();
            var redeliveryCount = context.GetRedeliveryCount();

            logger.LogInformation("Message: {0} CreationDateTime: {1}", context.Message.Message, context.Message.CreationDateTime);
            logger.LogInformation("RetryAttempt: {0} RetryCount: {1} RedeliveryCount: {2}", retryAttempt, retryCount, redeliveryCount);

            if (context.Message.Message.Contains("error", StringComparison.InvariantCultureIgnoreCase))
            {
                throw new ApplicationException("Invalid message content!");
            }
        }
Exemplo n.º 3
0
        async Task MessageRedeliveryContext.ScheduleRedelivery(TimeSpan delay, Action <ConsumeContext, SendContext> callback)
        {
            IPipe <SendContext <TMessage> > pipe = Pipe.Execute <SendContext <TMessage> >(sendContext =>
            {
                sendContext.Headers.Set(MessageHeaders.RedeliveryCount, _context.GetRedeliveryCount() + 1);

                callback?.Invoke(_context, sendContext);
            });

            IPipe <SendContext <TMessage> > delaySendPipe = new DelaySendPipe <TMessage>(pipe, delay);

            var schedulerEndpoint = await _context.GetSendEndpoint(_context.ReceiveContext.InputAddress).ConfigureAwait(false);

            await _context.Forward(schedulerEndpoint, delaySendPipe).ConfigureAwait(false);
        }
        public Task PreConsume <T>(ConsumeContext <T> context) where T : class
        {
            _logger.LogInformation(
                "Message is being consumed {@context}",
                new
            {
                MessageId       = context.MessageId,
                ConversationId  = context.ConversationId,
                CorrelationId   = context.CorrelationId,
                RedeliveryCount = context.GetRedeliveryCount(),
                SentTime        = context.SentTime,
                Message         = context.Message,
                MessageType     = context.Message.GetType()
            });

            return(Task.CompletedTask);
        }
        public Task ConsumeFault <T>(ConsumeContext <T> context, Exception exception) where T : class
        {
            _logger.LogError(exception,
                             "Message consumption has been failed {@context}",
                             new
            {
                MessageId       = context.MessageId,
                ConversationId  = context.ConversationId,
                CorrelationId   = context.CorrelationId,
                RedeliveryCount = context.GetRedeliveryCount(),
                SentTime        = context.SentTime,
                Message         = context.Message,
                MessageType     = context.Message.GetType(),
                ElapsedTime     = context.ReceiveContext.ElapsedTime
            });

            return(Task.CompletedTask);
        }
Exemplo n.º 6
0
        public Task Consume(ConsumeContext <NotifySomethingHappened> context)
        {
            var id   = $"[happeningId:{context.Message.HappeningId}]";
            var hap  = $"[whatHappened:{context.Message.WhatHappened}]";
            var redC = $"[redeliveryCount:{context.GetRedeliveryCount()}]";
            var retA = $"[retryAttempt:{context.GetRetryAttempt()}]";
            var retC = $"[retryCount:{context.GetRetryCount()}]";
            var ten  = $"[tenantId:{context.Headers.Get<string>("tenant-id")}]";

            _logger.LogInformation($"Consuming NotifySomethingHappened {id}{hap}{ten}{redC}{retA}{retC}");

            if (string.IsNullOrWhiteSpace(context.Message.WhatHappened))
            {
                throw new Exception("No happening");
            }

            if (context.Message.WhatHappened.Contains("redeliver"))
            {
                throw new CanNotCurrentlyProcessException(context.Message.WhatHappened);
            }

            return(Task.CompletedTask);
        }
Exemplo n.º 7
0
            public Task Consume(ConsumeContext <PingMessage> context)
            {
                if (_timer == null)
                {
                    _timer = Stopwatch.StartNew();
                }

                if (_count++ < 2)
                {
                    Console.WriteLine("{0} now is not a good time", DateTime.UtcNow);
                    throw new IntentionalTestException("I'm so not ready for this jelly.");
                }

                _timer.Stop();

                Console.WriteLine("{0} okay, now is good (retried {1} times)", DateTime.UtcNow, context.Headers.Get("MT-Redelivery-Count", default(int?)));

                // okay, ready.
                _receivedTimeSpan = _timer.Elapsed;
                RedeliveryCount   = context.GetRedeliveryCount();
                _received.TrySetResult(context);

                return(TaskUtil.Completed);
            }
 static void UpdateDeliveryContext(ConsumeContext context, SendContext sendContext)
 {
     sendContext.Headers.Set(MessageHeaders.RedeliveryCount, context.GetRedeliveryCount() + 1);
 }
        public async Task Send(ConsumeContext <T> context, IPipe <ConsumeContext <T> > next)
        {
            RetryPolicyContext <ConsumeContext <T> > policyContext = _retryPolicy.CreatePolicyContext(context);

            await _observers.PostCreate(policyContext).ConfigureAwait(false);

            try
            {
                await next.Send(policyContext.Context).ConfigureAwait(false);
            }
            catch (OperationCanceledException exception) when(exception.CancellationToken == context.CancellationToken)
            {
                throw;
            }
            catch (Exception exception)
            {
                if (context.CancellationToken.IsCancellationRequested)
                {
                    context.CancellationToken.ThrowIfCancellationRequested();
                }

                if (!policyContext.CanRetry(exception, out RetryContext <ConsumeContext <T> > retryContext))
                {
                    await retryContext.RetryFaulted(exception).ConfigureAwait(false);

                    await _observers.RetryFault(retryContext).ConfigureAwait(false);

                    if (_retryPolicy.IsHandled(exception))
                    {
                        context.GetOrAddPayload(() => retryContext);
                    }

                    throw;
                }

                int previousDeliveryCount = context.GetRedeliveryCount();
                for (int retryIndex = 0; retryIndex < previousDeliveryCount; retryIndex++)
                {
                    if (!retryContext.CanRetry(exception, out retryContext))
                    {
                        await retryContext.RetryFaulted(exception).ConfigureAwait(false);

                        await _observers.RetryFault(retryContext).ConfigureAwait(false);

                        if (_retryPolicy.IsHandled(exception))
                        {
                            context.GetOrAddPayload(() => retryContext);
                        }

                        throw;
                    }
                }

                await _observers.PostFault(retryContext).ConfigureAwait(false);

                try
                {
                    if (!context.TryGetPayload(out MessageRedeliveryContext redeliveryContext))
                    {
                        throw new ContextException("The message redelivery context was not available to delay the message", exception);
                    }

                    var delay = retryContext.Delay ?? TimeSpan.Zero;

                    await redeliveryContext.ScheduleRedelivery(delay).ConfigureAwait(false);

                    await context.NotifyConsumed(context, context.ReceiveContext.ElapsedTime, TypeMetadataCache <RedeliveryRetryFilter <T> > .ShortName)
                    .ConfigureAwait(false);
                }
                catch (Exception redeliveryException)
                {
                    throw new ContextException("The message delivery could not be rescheduled", new AggregateException(redeliveryException, exception));
                }
            }
        }