Example #1
0
        public async Task Persist(ProcessAuditMessageContext context)
        {
            if (!context.Message.Headers.TryGetValue(Headers.MessageId, out var messageId))
            {
                messageId = DeterministicGuid.MakeId(context.Message.MessageId).ToString();
            }

            var metadata = new ConcurrentDictionary <string, object>
            {
                ["MessageId"]     = messageId,
                ["MessageIntent"] = context.Message.Headers.MessageIntent()
            };

            var commandsToEmit  = new List <ICommand>();
            var enricherContext = new AuditEnricherContext(context.Message.Headers, commandsToEmit, metadata);

            // ReSharper disable once LoopCanBeConvertedToQuery
            foreach (var enricher in enrichers)
            {
                enricher.Enrich(enricherContext);
            }

            await bodyStorageEnricher.StoreAuditMessageBody(context.Message.Body, context.Message.Headers, metadata)
            .ConfigureAwait(false);

            var auditMessage = new ProcessedMessage(context.Message.Headers, new Dictionary <string, object>(metadata))
            {
                // We do this so Raven does not spend time assigning a hilo key
                Id = $"ProcessedMessages/{Guid.NewGuid()}"
            };

            using (var session = store.OpenAsyncSession())
            {
                await session.StoreAsync(auditMessage)
                .ConfigureAwait(false);

                await session.SaveChangesAsync()
                .ConfigureAwait(false);
            }

            foreach (var commandToEmit in commandsToEmit)
            {
                await context.MessageSession.Send(commandToEmit)
                .ConfigureAwait(false);
            }
        }
        async Task ProcessAuditMessage(MessageContext context)
        {
            if (!context.Headers.TryGetValue(Headers.MessageId, out var messageId))
            {
                messageId = DeterministicGuid.MakeId(context.MessageId).ToString();
            }

            try
            {
                var metadata = new ConcurrentDictionary <string, object>
                {
                    ["MessageId"]     = messageId,
                    ["MessageIntent"] = context.Headers.MessageIntent()
                };

                var commandsToEmit  = new List <ICommand>();
                var enricherContext = new AuditEnricherContext(context.Headers, commandsToEmit, metadata);

                // ReSharper disable once LoopCanBeConvertedToQuery
                foreach (var enricher in enrichers)
                {
                    enricher.Enrich(enricherContext);
                }

                await bodyStorageEnricher.StoreAuditMessageBody(context.Body, context.Headers, metadata)
                .ConfigureAwait(false);

                var auditMessage = new ProcessedMessage(context.Headers, new Dictionary <string, object>(metadata))
                {
                    Id = $"ProcessedMessages/{context.Headers.ProcessingId()}"
                };

                foreach (var commandToEmit in commandsToEmit)
                {
                    await messageSession.Send(commandToEmit)
                    .ConfigureAwait(false);
                }

                context.Extensions.Set(auditMessage);
                if (metadata.TryGetValue("SendingEndpoint", out var sendingEndpoint))
                {
                    context.Extensions.Set("SendingEndpoint", (EndpointDetails)sendingEndpoint);
                }

                if (metadata.TryGetValue("ReceivingEndpoint", out var receivingEndpoint))
                {
                    context.Extensions.Set("ReceivingEndpoint", (EndpointDetails)receivingEndpoint);
                }

                context.Extensions.Set("AuditType", "ProcessedMessage");
            }
            catch (Exception e)
            {
                if (Logger.IsDebugEnabled)
                {
                    Logger.Debug($"Processing of message '{messageId}' failed.", e);
                }

                context.GetTaskCompletionSource().TrySetException(e);
            }
        }
        static void RecordKnownEndpoints(EndpointDetails observedEndpoint, Dictionary <string, KnownEndpoint> observedEndpoints, ProcessedMessage processedMessage)
        {
            var uniqueEndpointId = $"{observedEndpoint.Name}{observedEndpoint.HostId}";

            if (!observedEndpoints.TryGetValue(uniqueEndpointId, out var knownEndpoint))
            {
                knownEndpoint = new KnownEndpoint
                {
                    Host     = observedEndpoint.Host,
                    HostId   = observedEndpoint.HostId,
                    LastSeen = processedMessage.ProcessedAt,
                    Name     = observedEndpoint.Name,
                    Id       = KnownEndpoint.MakeDocumentId(observedEndpoint.Name, observedEndpoint.HostId),
                };
                observedEndpoints.Add(uniqueEndpointId, knownEndpoint);
            }

            knownEndpoint.LastSeen = processedMessage.ProcessedAt > knownEndpoint.LastSeen ? processedMessage.ProcessedAt : knownEndpoint.LastSeen;
        }