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; /// /// gRPC implementation of IEventNotifier that pushes events to active gRPC streams for Phase 8 persistent subscriptions. /// public sealed class GrpcEventNotifier : IEventNotifier { private readonly IPersistentSubscriptionStore _subscriptionStore; private readonly ILogger _logger; public GrpcEventNotifier( IPersistentSubscriptionStore subscriptionStore, ILogger 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); } } }