Querying inside handler of event fired by changes API retrieve outdated data.

24 views
Skip to first unread message

Ivan Montilla

unread,
Jun 6, 2020, 5:57:30 PM6/6/20
to RavenDB - 2nd generation document database
I have a hosted service with this Start method:

        public async Task StartAsync(CancellationToken cancellationToken)
        {
            _logger.LogInformation("Cron scheduler started.");

            await ScheduleAsync(cancellationToken);
            _store.Changes()
                .ForDocumentsInCollection<CronEntity>()
                .Subscribe(async _ => await ScheduleAsync(cancellationToken));
        }

And this is my Schedule method:

        private async Task ScheduleAsync(CancellationToken cancellationToken)
        {
            _logger.LogInformation("Scheduling cron jobs...");

            // [Irrevelant code]

            using var session = _store.OpenAsyncSession(new SessionOptions
            {
                NoCaching = true,
                NoTracking = true
            });
            var jobs = await session.Query<CronEntity>()
                .Where(x => x.IsActive)
                .Select(x => new
                {
                    x.Expression,
                    x.Parameters,
                    x.Type
                })
                .ToArrayAsync();

            // [Irrevelant code]
        }

As you can see, when a change is done in CronEntity collection, the Schedule method is called, in this method, a readonly (no tracking) session is created and perform a query over CronEntity.

My problem is when I perform the query, I get the data how was before the last change that fire the changes API. I tried oppening the session with tracking and caching disabled, but my data does't reflect the last changed.

Any idea?

Ivan Montilla

unread,
Jun 6, 2020, 6:11:18 PM6/6/20
to RavenDB - 2nd generation document database
I tried with await Task.Delay(1000) before creating the session and now data is syncronized. 


            await Task.Delay(1000); // https://groups.google.com/forum/#!topic/ravendb/sY4sTSOahHs
            using var session = _store.OpenAsyncSession(new SessionOptions
            {
                NoCaching = true,
                NoTracking = true
            });
            var jobs = await session.Query<CronEntity>()
                .Where(x => x.IsActive)
                .Select(x => new
                {
                    x.Expression,
                    x.Parameters,
                    x.Type
                })
                .ToArrayAsync();

I think this is because the changes API is fired when started to save and not after saving. Is this correct? How can I handle it?

Egor Shamanaev

unread,
Jun 7, 2020, 3:25:50 AM6/7/20
to rav...@googlegroups.com
Hi,

Yes, Subscribe hit each time you do a change to the tracked entity in session, for example 

session.Store(new Employee(), "employees/1");
session.SaveChanges();

the Subscribe will be executed, after session.Store(), in your ScheduleAsync() method you make a query to the server but the actual change is not yet sent/processed by the server.
You can take a look on OnAfterSaveChanges event: https://ravendb.net/docs/article-page/4.2/Csharp/client-api/session/how-to/subscribe-to-events#onaftersavechanges this will make sure that the server already processed the changes.

--
You received this message because you are subscribed to the Google Groups "RavenDB - 2nd generation document 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/fd1b61fc-2b8c-4ca5-9535-b2ab6b01cc70o%40googlegroups.com.


--
Egor
Developer   /   Hibernating Rhinos LTD
Support:  sup...@ravendb.net
  

Oren Eini (Ayende Rahien)

unread,
Jun 7, 2020, 6:58:35 AM6/7/20
to ravendb
I think that you'll need to use WaitForNonStaleResults() (and pass the relevant change vector there) in the Suscribe call.
You are getting notified before the indexes had a chance to run



--
Oren Eini
CEO   /   Hibernating Rhinos LTD
Skype:  ayenderahien
Support:  sup...@ravendb.net
  

Ivan Montilla

unread,
Jun 7, 2020, 8:29:28 AM6/7/20
to RavenDB - 2nd generation document database
Hi Egor, that no was my problem because I made the changes in the studio to fire the subscription.
To unsubscribe from this group and stop receiving emails from it, send an email to rav...@googlegroups.com.

Ivan Montilla

unread,
Jun 7, 2020, 8:30:38 AM6/7/20
to RavenDB - 2nd generation document database
Hi Oren. Thank you! I added WaitForNonStale in the query and now is working as I expected.
To unsubscribe from this group and stop receiving emails from it, send an email to rav...@googlegroups.com.


--
Egor
Developer   /   Hibernating Rhinos LTD
Support:  sup...@ravendb.net
  

--
You received this message because you are subscribed to the Google Groups "RavenDB - 2nd generation document database" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rav...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages