protected override async Task <EventSubscription> Subscribe( Checkpoint _, CancellationToken cancellationToken ) { var settings = new PersistentSubscriptionSettings(true); PersistentSubscription sub; try { sub = await LocalSubscribe(); } catch (PersistentSubscriptionNotFoundException) { await _persistentSubscriptionsClient.CreateAsync( _stream, SubscriptionId, settings, cancellationToken : cancellationToken ); sub = await LocalSubscribe(); } return(new EventSubscription(SubscriptionId, new Stoppable(() => sub.Dispose()))); void HandleDrop(PersistentSubscription __, SubscriptionDroppedReason reason, Exception?exception) => Dropped(EsdbMappings.AsDropReason(reason), exception); async Task HandleEvent( PersistentSubscription subscription, ResolvedEvent re, int?retryCount, CancellationToken ct ) { try { await Handler(AsReceivedEvent(re), ct); await subscription.Ack(re); } catch (Exception e) { await subscription.Nack(PersistentSubscriptionNakEventAction.Retry, e.Message, re); } } Task <PersistentSubscription> LocalSubscribe() => _persistentSubscriptionsClient.SubscribeAsync( _stream, SubscriptionId, HandleEvent, HandleDrop, cancellationToken: cancellationToken );
protected override async Task <EventSubscription> Subscribe( Checkpoint checkpoint, CancellationToken cancellationToken ) { var subTask = checkpoint.Position == null ? EventStoreClient.SubscribeToStreamAsync( _options.StreamName, HandleEvent, _options.ResolveLinkTos, HandleDrop, _options.ConfigureOperation, _options.Credentials, cancellationToken ) : EventStoreClient.SubscribeToStreamAsync( _options.StreamName, StreamPosition.FromInt64((long)checkpoint.Position), HandleEvent, _options.ResolveLinkTos, HandleDrop, _options.ConfigureOperation, _options.Credentials, cancellationToken ); var sub = await subTask.Ignore(); return(new EventSubscription(SubscriptionId, new Stoppable(() => sub.Dispose()))); async Task HandleEvent(EventStore.Client.StreamSubscription _, ResolvedEvent re, CancellationToken ct) { await Handler(AsReceivedEvent(re), ct).Ignore(); } void HandleDrop(EventStore.Client.StreamSubscription _, SubscriptionDroppedReason reason, Exception?ex) => Dropped(EsdbMappings.AsDropReason(reason), ex); ReceivedEvent AsReceivedEvent(ResolvedEvent re) { var evt = DeserializeData( re.Event.ContentType, re.Event.EventType, re.Event.Data, re.Event.EventStreamId, re.Event.EventNumber ); return(new ReceivedEvent( re.Event.EventId.ToString(), re.Event.EventType, re.Event.ContentType, re.Event.Position.CommitPosition, re.Event.EventNumber, re.Event.EventStreamId, re.Event.EventNumber, re.Event.Created, evt // re.Event.Metadata )); } }
protected override async Task <EventSubscription> Subscribe( Checkpoint _, CancellationToken cancellationToken ) { var settings = _options.SubscriptionSettings ?? new PersistentSubscriptionSettings(_options.ResolveLinkTos); var autoAck = _options.AutoAck; PersistentSubscription sub; try { sub = await LocalSubscribe().Ignore(); } catch (PersistentSubscriptionNotFoundException) { await _subscriptionClient.CreateAsync( _options.Stream, SubscriptionId, settings, _options.Credentials, cancellationToken ).Ignore(); sub = await LocalSubscribe().Ignore(); } return(new EventSubscription(SubscriptionId, new Stoppable(() => sub.Dispose()))); void HandleDrop(PersistentSubscription __, SubscriptionDroppedReason reason, Exception?exception) => Dropped(EsdbMappings.AsDropReason(reason), exception); async Task HandleEvent( PersistentSubscription subscription, ResolvedEvent re, int?retryCount, CancellationToken ct ) { var receivedEvent = AsReceivedEvent(re); try { await Handler(receivedEvent, ct).Ignore(); if (!autoAck) { await subscription.Ack(re).Ignore(); } } catch (Exception e) { await _handleEventProcessingFailure(EventStoreClient, subscription, re, e).Ignore(); } } Task <PersistentSubscription> LocalSubscribe() => _subscriptionClient.SubscribeAsync( _options.Stream, SubscriptionId, HandleEvent, HandleDrop, _options.Credentials, _options.BufferSize, _options.AutoAck, cancellationToken ); ReceivedEvent AsReceivedEvent(ResolvedEvent re) { var evt = DeserializeData( re.Event.ContentType, re.Event.EventType, re.Event.Data, re.OriginalStreamId, re.Event.Position.CommitPosition ); return(new ReceivedEvent( re.Event.EventId.ToString(), re.Event.EventType, re.Event.ContentType, re.Event.Position.CommitPosition, re.Event.Position.CommitPosition, re.OriginalStreamId, re.Event.EventNumber, re.Event.Created, evt // re.Event.Metadata )); } }
protected override async Task <EventSubscription> Subscribe( Checkpoint checkpoint, CancellationToken cancellationToken ) { var filterOptions = new SubscriptionFilterOptions( _eventFilter, 10, (_, p, ct) => StoreCheckpoint(new EventPosition(p.CommitPosition, DateTime.Now), ct) ); var subTask = checkpoint.Position != null ? EventStoreClient.SubscribeToAllAsync( new Position(checkpoint.Position.Value, checkpoint.Position.Value), HandleEvent, false, HandleDrop, filterOptions, _options.ConfigureOperation, _options.Credentials, cancellationToken ) : EventStoreClient.SubscribeToAllAsync( HandleEvent, false, HandleDrop, filterOptions, _options.ConfigureOperation, _options.Credentials, cancellationToken ); var sub = await subTask.Ignore(); return(new EventSubscription(SubscriptionId, new Stoppable(() => sub.Dispose()))); Task HandleEvent(EventStore.Client.StreamSubscription _, ResolvedEvent re, CancellationToken ct) => Handler(AsReceivedEvent(re), ct); void HandleDrop(EventStore.Client.StreamSubscription _, SubscriptionDroppedReason reason, Exception?ex) => Dropped(EsdbMappings.AsDropReason(reason), ex); ReceivedEvent AsReceivedEvent(ResolvedEvent re) { var evt = DeserializeData( re.Event.ContentType, re.Event.EventType, re.Event.Data, re.Event.EventStreamId, re.Event.EventNumber ); return(new ReceivedEvent( re.Event.EventId.ToString(), re.Event.EventType, re.Event.ContentType, re.Event.Position.CommitPosition, re.Event.Position.CommitPosition, re.OriginalStreamId, re.Event.EventNumber, re.Event.Created, evt // re.Event.Metadata )); } }