public async Task Invoke(IIncomingPhysicalMessageContext context, Func <IIncomingPhysicalMessageContext, Task> next)
        {
            var state = new State();

            var headers = context.Message.Headers;

            if (headers.TryGetValue(Headers.TimeSent, out var timeSentString))
            {
                state.TimeSent = DateTimeOffsetHelper.ToDateTimeOffset(timeSentString);
            }

            state.ProcessingStarted = DateTimeOffset.UtcNow;
            context.Extensions.Set(state);
            var stopwatch = Stopwatch.StartNew();

            try
            {
                await next(context).ConfigureAwait(false);
            }
            finally
            {
                stopwatch.Stop();
                state.ProcessingEnded = state.ProcessingStarted + stopwatch.Elapsed;
            }
        }
        static bool HasReachedMaxTime(IncomingMessage message)
        {
            if (!message.Headers.TryGetValue(Headers.DelayedRetriesTimestamp, out var timestampHeader))
            {
                return(false);
            }

            if (string.IsNullOrEmpty(timestampHeader))
            {
                return(false);
            }

            try
            {
                var handledAt = DateTimeOffsetHelper.ToDateTimeOffset(timestampHeader);

                var now = DateTimeOffset.UtcNow;
                if (now > handledAt.AddDays(1))
                {
                    return(true);
                }
            }
            // this code won't usually throw but in case a user has decided to hack a message/headers and for some bizarre reason
            // they changed the date and that parse fails, we want to make sure that doesn't prevent the message from being
            // forwarded to the error queue.
            catch (Exception)
            {
            }

            return(false);
        }
Ejemplo n.º 3
0
        static List <DeliveryConstraint> DeserializeConstraints(Dictionary <string, string> options)
        {
            var constraints = new List <DeliveryConstraint>(4);

            if (options.TryGetValue("DeliverAt", out var deliverAt))
            {
                constraints.Add(new DoNotDeliverBefore(DateTimeOffsetHelper.ToDateTimeOffset(deliverAt)));
            }

            if (options.TryGetValue("DelayDeliveryFor", out var delay))
            {
                constraints.Add(new DelayDeliveryWith(TimeSpan.Parse(delay)));
            }

            if (options.TryGetValue("TimeToBeReceived", out var ttbr))
            {
                constraints.Add(new DiscardIfNotReceivedBefore(TimeSpan.Parse(ttbr)));
            }
            return(constraints);
        }