protected override async Task Terminate(IInvokeHandlerContext context) { IDelayedChannel channel = null; try { channel = context.Builder.Build <IDelayedChannel>(); } // Catch in case IDelayedChannel isn't registered which shouldn't happen unless a user registered Consumer without GetEventStore catch (Exception) { } var msgType = context.MessageBeingHandled.GetType(); if (!msgType.IsInterface) { msgType = _mapper.GetMappedTypeFor(msgType) ?? msgType; } context.Extensions.Set(new State { ScopeWasPresent = Transaction.Current != null }); var messageHandler = context.MessageHandler; var channelKey = $"{messageHandler.HandlerType.FullName}:{msgType.FullName}"; bool contains = false; lock (Lock) contains = IsNotDelayed.Contains(channelKey); var contextChannelKey = ""; if (context.Headers.ContainsKey(Defaults.ChannelKey)) { contextChannelKey = context.Headers[Defaults.ChannelKey]; } // Special case for when we are bulk processing messages from DelayedSubscriber, simply process it and return dont check for more bulk if (channel == null || contains || contextChannelKey == channelKey) { Logger.Write(LogLevel.Debug, () => $"Invoking handle for message {msgType.FullName} on handler {messageHandler.HandlerType.FullName}"); await messageHandler.Invoke(context.MessageBeingHandled, context).ConfigureAwait(false); return; } // If we are bulk processing and the above check fails it means the current message handler shouldn't be called if (!string.IsNullOrEmpty(contextChannelKey)) { return; } if (IsDelayed.ContainsKey(channelKey)) { DelayedAttribute delayed; IsDelayed.TryGetValue(channelKey, out delayed); var msgPkg = new DelayedMessage { MessageId = context.MessageId, Headers = context.Headers, Message = context.MessageBeingHandled, Received = DateTime.UtcNow, ChannelKey = channelKey }; InvokesDelayed.Mark(); var specificKey = ""; if (delayed.KeyPropertyFunc != null) { try { specificKey = delayed.KeyPropertyFunc(context.MessageBeingHandled); } catch { Logger.Warn($"Failed to get key properties from message {msgType.FullName}"); } } await channel.AddToQueue(channelKey, msgPkg, key : specificKey).ConfigureAwait(false); bool bulkInvoked; if (context.Extensions.TryGet <bool>("BulkInvoked", out bulkInvoked) && bulkInvoked) { // Prevents a single message from triggering a dozen different bulk invokes Logger.Write(LogLevel.Debug, () => $"Limiting bulk processing for a single message to a single invoke"); return; } int? size = null; TimeSpan?age = null; if (delayed.Delay.HasValue) { age = await channel.Age(channelKey, key : specificKey).ConfigureAwait(false); } if (delayed.Count.HasValue) { size = await channel.Size(channelKey, key : specificKey).ConfigureAwait(false); } if (!ShouldExecute(delayed, size, age)) { Logger.Write(LogLevel.Debug, () => $"Threshold Count [{delayed.Count}] DelayMs [{delayed.Delay}] Size [{size}] Age [{age?.TotalMilliseconds}] - delaying processing channel [{channelKey}] specific [{specificKey}]"); return; } lock (RecentLock) { if (RecentlyInvoked.ContainsKey($"{channelKey}.{specificKey}")) { Logger.Write(LogLevel.Debug, () => $"Channel [{channelKey}] specific [{specificKey}] was checked by this instance recently - leaving it alone"); return; } RecentlyInvoked.Add($"{channelKey}.{specificKey}", DateTime.UtcNow.AddSeconds(10)); } context.Extensions.Set <bool>("BulkInvoked", true); Logger.Write(LogLevel.Info, () => $"Threshold Count [{delayed.Count}] DelayMs [{delayed.Delay}] Size [{size}] Age [{age?.TotalMilliseconds}] - bulk processing channel [{channelKey}] specific [{specificKey}]"); await InvokeDelayedChannel(channel, channelKey, specificKey, delayed, messageHandler, context).ConfigureAwait(false); return; } var attrs = Attribute.GetCustomAttributes(messageHandler.HandlerType, typeof(DelayedAttribute)) .Cast <DelayedAttribute>(); var single = attrs.SingleOrDefault(x => x.Type == msgType); if (single == null) { lock (Lock) IsNotDelayed.Add(channelKey); } else { Logger.Write(LogLevel.Debug, () => $"Found delayed handler {messageHandler.HandlerType.FullName} for message {msgType.FullName}"); IsDelayed.TryAdd(channelKey, single); } await Terminate(context).ConfigureAwait(false); }
protected override async Task Terminate(IInvokeHandlerContext context) { if (context.Extensions.TryGet(out ActiveSagaInstance saga) && saga.NotFound && ((SagaMetadata)saga.GetType().GetProperty("Metadata", BindingFlags.NonPublic).GetValue(saga)).SagaType == context.MessageHandler.Instance.GetType()) { return; } IDelayedChannel channel = null; try { IContainer container; if (context.Extensions.TryGet <IContainer>(out container)) { channel = container.Resolve <IDelayedChannel>(); } } catch (Exception) { // Catch in case IDelayedChannel isn't registered which shouldn't happen unless a user registered Consumer without GetEventStore } var msgType = context.MessageBeingHandled.GetType(); if (!msgType.IsInterface) { msgType = _mapper.GetMappedTypeFor(msgType) ?? msgType; } context.Extensions.Set(new State { ScopeWasPresent = Transaction.Current != null }); var messageHandler = context.MessageHandler; var channelKey = $"{messageHandler.HandlerType.FullName}:{msgType.FullName}"; bool contains = false; lock (Lock) contains = IsNotDelayed.Contains(channelKey); context.Extensions.TryGet(Defaults.ChannelKey, out string contextChannelKey); // Special case for when we are bulk processing messages from DelayedSubscriber or BulkMessage, simply process it and return dont check for more bulk if (channel == null || contains || contextChannelKey == channelKey || context.Headers.ContainsKey(Defaults.BulkHeader)) { await messageHandler.Invoke(context.MessageBeingHandled, context).ConfigureAwait(false); return; } // If we are bulk processing and the above check fails it means the current message handler shouldn't be called // because if contextChannelKey is set we are using delayed bulk SendLocal // which should only execute the message on the handlers that were marked delayed, the other handlers were already invoked when the message // originally came in if (!string.IsNullOrEmpty(contextChannelKey)) { return; } if (IsDelayed.ContainsKey(channelKey)) { DelayedAttribute delayed; IsDelayed.TryGetValue(channelKey, out delayed); var specificKey = ""; if (delayed.KeyPropertyFunc != null) { try { specificKey = delayed.KeyPropertyFunc(context.MessageBeingHandled); } catch { Logger.Warn($"Failed to get key properties from message {msgType.FullName}"); } } var msgPkg = new DelayedMessage { MessageId = context.MessageId, Headers = context.Headers, Message = context.MessageBeingHandled as IMessage, Received = DateTime.UtcNow, ChannelKey = channelKey, SpecificKey = specificKey }; await channel.AddToQueue(channelKey, msgPkg, key : specificKey).ConfigureAwait(false); Logger.DebugEvent("QueueAdd", "Message added to delayed queue [{Channel:l}] key [{Key:l}]", channelKey, specificKey); bool bulkInvoked; if (context.Extensions.TryGet <bool>("BulkInvoked", out bulkInvoked) && bulkInvoked) { // Prevents a single message from triggering a dozen different bulk invokes return; } int? size = null; TimeSpan?age = null; if (delayed.Delay.HasValue) { age = await channel.Age(channelKey, key : specificKey).ConfigureAwait(false); } if (delayed.Count.HasValue) { size = await channel.Size(channelKey, key : specificKey).ConfigureAwait(false); } if (!ShouldExecute(delayed, size, age)) { return; } context.Extensions.Set <bool>("BulkInvoked", true); Logger.DebugEvent("Threshold", "Count [{Count}] DelayMs [{DelayMs}] bulk procesing Size [{Size}] Age [{Age}] - [{Channel:l}] key [{Key:l}]", delayed.Count, delayed.Delay, size, age?.TotalMilliseconds, channelKey, specificKey); await InvokeDelayedChannel(channel, channelKey, specificKey, delayed, messageHandler, context).ConfigureAwait(false); return; } var attrs = Attribute.GetCustomAttributes(messageHandler.HandlerType, typeof(DelayedAttribute)) .Cast <DelayedAttribute>(); var single = attrs.SingleOrDefault(x => x.Type == msgType); if (single == null) { lock (Lock) IsNotDelayed.Add(channelKey); } else { IsDelayed.TryAdd(channelKey, single); } await Terminate(context).ConfigureAwait(false); }