Consuming subscription while yielding results

29 views
Skip to first unread message

Ron Brouwer

unread,
Dec 24, 2021, 10:53:52 AM12/24/21
to RavenDB - an awesome database
Hi Guys,

How would I solve the following simplified problem given the fact that I cannot change the Produce functions signature because of the abstract EventPublisher implementation? The issue is highlighted below. I cannot yield from the run function. Perhaps I could use a BlockingCollection of 1, but hat seams like a bad solution to me. Not sure how I could make the batch fail if this producer was not readed til the end of the batch (or subscription stops producing).

public class SellerEventPublisher : EventPublisher<SellerProjection>
    {
        private readonly IDocumentStore _documentStore;

        public SellerEventPublisher(IDocumentStore documentStore)
        {
            _documentStore = documentStore;
        }
        public override IEnumerable<SellerProjection> Produce(CancellationToken cancellationToken)
        {
            var subscription = _documentStore.Subscriptions.GetSubscriptionWorker<SellerProjection>(new SubscriptionWorkerOptions(Subscription.Seller)
            {
                Strategy = SubscriptionOpeningStrategy.TakeOver,
                MaxDocsPerBatch = 100,
                CloseWhenNoDocsLeft = true
            });

            subscription.Run(batch =>
            {
                foreach (var item in batch.Items)
                {
                    yield return item.Result;
                }
            }).GetAwaiter().GetResult();
        }
    }

Looking forward to your insights.

Thanks, Ron Brouwer

Egor Shamanaev

unread,
Dec 26, 2021, 6:54:56 AM12/26/21
to rav...@googlegroups.com
Hi 

You cannot yield return items from the Worker.Run() method directly so populating a collection seems like  the way togo.

In case if the batch / fail or subscription stops producing the task will fail. You can see here an example of exception handling in subscription:
https://ravendb.net/docs/article-page/5.3/csharp/client-api/data-subscriptions/consumption/examples#client-with-full-exception-handling-and-processing-retries

--
You received this message because you are subscribed to the Google Groups "RavenDB - an awesome database" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ravendb+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ravendb/58569198-9ea1-46ee-8afb-36008f1f54d5n%40googlegroups.com.


--
Egor
Developer   /   Hibernating Rhinos LTD
Support:  sup...@ravendb.net
  
Reply all
Reply to author
Forward
0 new messages