dotnet-cqrs/Svrnty.CQRS.Events.Grpc/GrpcEventNotifier.cs

55 lines
1.9 KiB
C#

using System;
using Svrnty.CQRS.Events.Abstractions.Notifications;
using Svrnty.CQRS.Events.Abstractions.EventStore;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Svrnty.CQRS.Events.Abstractions;
using Svrnty.CQRS.Events.Abstractions.Subscriptions;
namespace Svrnty.CQRS.Events.Grpc;
/// <summary>
/// gRPC implementation of IEventNotifier that pushes events to active gRPC streams for Phase 8 persistent subscriptions.
/// </summary>
public sealed class GrpcEventNotifier : IEventNotifier
{
private readonly IPersistentSubscriptionStore _subscriptionStore;
private readonly ILogger<GrpcEventNotifier> _logger;
public GrpcEventNotifier(
IPersistentSubscriptionStore subscriptionStore,
ILogger<GrpcEventNotifier> logger)
{
_subscriptionStore = subscriptionStore ?? throw new ArgumentNullException(nameof(subscriptionStore));
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
}
public async Task NotifyAsync(ICorrelatedEvent @event, long sequence, CancellationToken cancellationToken = default)
{
try
{
// Use the static method from EventServiceImpl to notify active gRPC subscribers
await EventServiceImpl.NotifySubscribersAsync(
@event.CorrelationId,
@event,
sequence,
_subscriptionStore);
_logger.LogDebug(
"Notified gRPC subscribers about event {EventType} {EventId} (sequence {Sequence})",
@event.GetType().Name,
@event.EventId,
sequence);
}
catch (Exception ex)
{
_logger.LogError(
ex,
"Error notifying gRPC subscribers about event {EventType} {EventId}",
@event.GetType().Name,
@event.EventId);
}
}
}