From 845c5656b29da541626bab98ce908a3ed9bf1260 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ole=20J=C3=B8rgen=20Skogstad?= Date: Tue, 1 Oct 2024 13:10:50 +0200 Subject: [PATCH] =?UTF-8?q?Better=20=C3=A0=20la=20Magnus?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Dialogs/Entities/DialogEntity.cs | 15 +--- ...DomainEventsToOutboxMessagesInterceptor.cs | 75 +++++++------------ 2 files changed, 28 insertions(+), 62 deletions(-) diff --git a/src/Digdir.Domain.Dialogporten.Domain/Dialogs/Entities/DialogEntity.cs b/src/Digdir.Domain.Dialogporten.Domain/Dialogs/Entities/DialogEntity.cs index 794046b3b..634bf121a 100644 --- a/src/Digdir.Domain.Dialogporten.Domain/Dialogs/Entities/DialogEntity.cs +++ b/src/Digdir.Domain.Dialogporten.Domain/Dialogs/Entities/DialogEntity.cs @@ -74,19 +74,8 @@ public sealed class DialogEntity : public void OnCreate(AggregateNode self, DateTimeOffset utcNow) => _domainEvents.Add(new DialogCreatedDomainEvent(Id, ServiceResource, Party, Process, PrecedingProcess)); - public void OnUpdate(AggregateNode self, DateTimeOffset utcNow) - { - var changedChildren = self.Children.Where(x => - x.State != AggregateNodeState.Unchanged && - x.Entity is not DialogSearchTag && - x.Entity is not DialogActivity); - - var shouldProduceEvent = self.IsDirectlyModified() || changedChildren.Any(); - if (shouldProduceEvent) - { - _domainEvents.Add(new DialogUpdatedDomainEvent(Id, ServiceResource, Party, Process, PrecedingProcess)); - } - } + public void OnUpdate(AggregateNode self, DateTimeOffset utcNow) => + _domainEvents.Add(new DialogUpdatedDomainEvent(Id, ServiceResource, Party, Process, PrecedingProcess)); public void OnDelete(AggregateNode self, DateTimeOffset utcNow) => _domainEvents.Add(new DialogDeletedDomainEvent(Id, ServiceResource, Party, Process, PrecedingProcess)); diff --git a/src/Digdir.Domain.Dialogporten.Infrastructure/DomainEvents/Outbox/ConvertDomainEventsToOutboxMessagesInterceptor.cs b/src/Digdir.Domain.Dialogporten.Infrastructure/DomainEvents/Outbox/ConvertDomainEventsToOutboxMessagesInterceptor.cs index 94108a864..3d2575718 100644 --- a/src/Digdir.Domain.Dialogporten.Infrastructure/DomainEvents/Outbox/ConvertDomainEventsToOutboxMessagesInterceptor.cs +++ b/src/Digdir.Domain.Dialogporten.Infrastructure/DomainEvents/Outbox/ConvertDomainEventsToOutboxMessagesInterceptor.cs @@ -1,12 +1,10 @@ using Digdir.Domain.Dialogporten.Application.Common; using Digdir.Domain.Dialogporten.Domain.Dialogs.Events; -using Digdir.Domain.Dialogporten.Domain.Dialogs.Events.Activities; using Digdir.Domain.Dialogporten.Domain.Outboxes; using Digdir.Domain.Dialogporten.Infrastructure.GraphQl; using Digdir.Library.Entity.Abstractions.Features.EventPublisher; using HotChocolate.Subscriptions; using Microsoft.EntityFrameworkCore.Diagnostics; -using Microsoft.Extensions.Caching.Memory; using Microsoft.Extensions.Logging; using Constants = Digdir.Domain.Dialogporten.Infrastructure.GraphQl.GraphQlSubscriptionConstants; @@ -17,8 +15,6 @@ internal sealed class ConvertDomainEventsToOutboxMessagesInterceptor : SaveChang private readonly ITransactionTime _transactionTime; private readonly ITopicEventSender _topicEventSender; private readonly ILogger _logger; - private static readonly MemoryCache UpdateEventThrottleCache = new(new MemoryCacheOptions()); - private List _domainEvents = []; @@ -68,56 +64,37 @@ x.Entity is IEventPublisher publisher public override async ValueTask SavedChangesAsync(SaveChangesCompletedEventData eventData, int result, CancellationToken cancellationToken = default) { - foreach (var domainEvent in _domainEvents) + try { - try - { - var task = domainEvent switch + var tasks = _domainEvents + .Select(x => x switch { - DialogUpdatedDomainEvent dialogUpdatedDomainEvent => - SendDialogUpdated(dialogUpdatedDomainEvent.DialogId, cancellationToken), - - DialogActivityCreatedDomainEvent dialogActivityCreatedDomainEvent => - SendDialogUpdated(dialogActivityCreatedDomainEvent.DialogId, cancellationToken), - - DialogDeletedDomainEvent dialogDeletedDomainEvent => _topicEventSender.SendAsync( - $"{Constants.DialogEventsTopic}{dialogDeletedDomainEvent.DialogId}", - new DialogEventPayload - { - Id = dialogDeletedDomainEvent.DialogId, - Type = DialogEventType.DialogDeleted - }, - cancellationToken), - _ => ValueTask.CompletedTask - }; - - await task; - } - catch (Exception e) - { - _logger.LogError(e, "Failed to send domain event to graphQL subscription"); - } + DialogUpdatedDomainEvent dialogUpdatedDomainEvent => new DialogEventPayload + { + Id = dialogUpdatedDomainEvent.DialogId, + Type = DialogEventType.DialogUpdated + }, + DialogDeletedDomainEvent dialogDeletedDomainEvent => new DialogEventPayload + { + Id = dialogDeletedDomainEvent.DialogId, + Type = DialogEventType.DialogDeleted + }, + _ => (DialogEventPayload?)null + }) + .Where(x => x is not null) + .Cast() + .Select(x => _topicEventSender.SendAsync( + $"{Constants.DialogEventsTopic}{x.Id}", + x, + cancellationToken) + .AsTask()); + await Task.WhenAll(tasks); } - - return await base.SavedChangesAsync(eventData, result, cancellationToken); - } - - private async ValueTask SendDialogUpdated(Guid dialogId, CancellationToken cancellationToken) - { - if (UpdateEventThrottleCache.TryGetValue(dialogId, out _)) + catch (Exception e) { - return; + _logger.LogError(e, "Failed to send domain events to graphQL subscription"); } - UpdateEventThrottleCache.Set(dialogId, true, TimeSpan.FromMilliseconds(50)); - - await _topicEventSender.SendAsync( - $"{Constants.DialogEventsTopic}{dialogId}", - new DialogEventPayload - { - Id = dialogId, - Type = DialogEventType.DialogUpdated - }, - cancellationToken); + return await base.SavedChangesAsync(eventData, result, cancellationToken); } }