protected override async ValueTask Subscribe(CancellationToken cancellationToken) { var settings = Options.SubscriptionSettings ?? new PersistentSubscriptionSettings(Options.ResolveLinkTos); try { _subscription = await LocalSubscribe(HandleEvent, HandleDrop, cancellationToken).NoContext(); } catch (PersistentSubscriptionNotFoundException) { await CreatePersistentSubscription(settings, cancellationToken); _subscription = await LocalSubscribe(HandleEvent, HandleDrop, cancellationToken).NoContext(); } void HandleDrop( PersistentSubscription __, SubscriptionDroppedReason reason, Exception?exception ) => Dropped(EsdbMappings.AsDropReason(reason), exception); async Task HandleEvent( PersistentSubscription subscription, ResolvedEvent re, int?retryCount, CancellationToken ct ) { var context = CreateContext(re, ct) .WithItem(ResolvedEventKey, re) .WithItem(SubscriptionKey, subscription); try { await Handler(context).NoContext(); LastProcessed = EventPosition.FromContext(context); await Ack(context).NoContext(); } catch (Exception e) { await Nack(context, e).NoContext(); } } }
protected override async ValueTask Subscribe(CancellationToken cancellationToken) { var filterOptions = new SubscriptionFilterOptions( Options.EventFilter ?? EventTypeFilter.ExcludeSystemEvents(), Options.CheckpointInterval, async(_, p, ct) => { // This doesn't allow to report tie time gap LastProcessed = new EventPosition(p.CommitPosition, DateTime.Now); await StoreCheckpoint(LastProcessed, ct).NoContext(); } ); var(_, position) = await GetCheckpoint(cancellationToken).NoContext(); var fromAll = position == null ? FromAll.Start : FromAll.After(new Position(position.Value, position.Value)); Subscription = await EventStoreClient.SubscribeToAllAsync( fromAll, HandleEvent, Options.ResolveLinkTos, HandleDrop, filterOptions, Options.Credentials, cancellationToken ).NoContext(); async Task HandleEvent( global::EventStore.Client.StreamSubscription _, ResolvedEvent re, CancellationToken ct ) => await HandleInternal(CreateContext(re, ct)).NoContext(); void HandleDrop( global::EventStore.Client.StreamSubscription _, SubscriptionDroppedReason reason, Exception?ex ) => Dropped(EsdbMappings.AsDropReason(reason), ex); }