Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(graphQL): GraphQL subscription not notified on DialogActivityCreated #1187

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
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 @@ -16,6 +17,8 @@ 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 @@ -69,18 +72,14 @@ public override async ValueTask<int> SavedChangesAsync(SaveChangesCompletedEvent
{
try
{
// If you are adding any additional cases to this switch,
// please consider making the running of the tasks parallel
var task = domainEvent switch
{
DialogUpdatedDomainEvent dialogUpdatedDomainEvent => _topicEventSender.SendAsync(
$"{Constants.DialogEventsTopic}{dialogUpdatedDomainEvent.DialogId}",
new DialogEventPayload
{
Id = dialogUpdatedDomainEvent.DialogId,
Type = DialogEventType.DialogUpdated
},
cancellationToken),
DialogUpdatedDomainEvent dialogUpdatedDomainEvent =>
SendDialogUpdated(dialogUpdatedDomainEvent.DialogId, cancellationToken),

DialogActivityCreatedDomainEvent dialogActivityCreatedDomainEvent =>
SendDialogUpdated(dialogActivityCreatedDomainEvent.DialogId, cancellationToken),

oskogstad marked this conversation as resolved.
Show resolved Hide resolved
DialogDeletedDomainEvent dialogDeletedDomainEvent => _topicEventSender.SendAsync(
$"{Constants.DialogEventsTopic}{dialogDeletedDomainEvent.DialogId}",
new DialogEventPayload
Expand All @@ -89,14 +88,6 @@ public override async ValueTask<int> SavedChangesAsync(SaveChangesCompletedEvent
Type = DialogEventType.DialogDeleted
},
cancellationToken),
DialogActivityCreatedDomainEvent dialogActivityCreatedDomainEvent => _topicEventSender.SendAsync(
$"{Constants.DialogEventsTopic}{dialogActivityCreatedDomainEvent.DialogId}",
new DialogEventPayload
{
Id = dialogActivityCreatedDomainEvent.DialogId,
Type = DialogEventType.DialogUpdated
},
cancellationToken),
_ => ValueTask.CompletedTask
};

Expand All @@ -110,4 +101,23 @@ public override async ValueTask<int> SavedChangesAsync(SaveChangesCompletedEvent

return await base.SavedChangesAsync(eventData, result, cancellationToken);
}

private async ValueTask SendDialogUpdated(Guid dialogId, CancellationToken cancellationToken)
{
if (UpdateEventThrottleCache.TryGetValue(dialogId, out _))
{
return;
}

UpdateEventThrottleCache.Set(dialogId, true, TimeSpan.FromMilliseconds(50));

await _topicEventSender.SendAsync(
$"{Constants.DialogEventsTopic}{dialogId}",
new DialogEventPayload
{
Id = dialogId,
Type = DialogEventType.DialogUpdated
},
cancellationToken);
}
oskogstad marked this conversation as resolved.
Show resolved Hide resolved
}
Loading