Skip to content

Commit

Permalink
Better à la Magnus
Browse files Browse the repository at this point in the history
  • Loading branch information
oskogstad committed Oct 1, 2024
1 parent de54c71 commit 845c565
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -17,8 +15,6 @@ internal sealed class ConvertDomainEventsToOutboxMessagesInterceptor : SaveChang
private readonly ITransactionTime _transactionTime;
private readonly ITopicEventSender _topicEventSender;
private readonly ILogger<ConvertDomainEventsToOutboxMessagesInterceptor> _logger;
private static readonly MemoryCache UpdateEventThrottleCache = new(new MemoryCacheOptions());


private List<IDomainEvent> _domainEvents = [];

Expand Down Expand Up @@ -68,56 +64,37 @@ x.Entity is IEventPublisher publisher
public override async ValueTask<int> 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<DialogEventPayload>()
.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);
}
}

0 comments on commit 845c565

Please sign in to comment.