Content out of sync with the database

Topics: Customizing Orchard, Writing modules
May 21, 2014 at 12:19 AM

I see my content items are little behind the database. I would like to get experts advice here on this issue and my approach.

I have a background process in orchard (created a singleton class timed to run for every 3 seconds) which creates new entries in the database.

I have a controller created with long polling feature, here the request is waiting for such new entries.
If any such new entries found, the browser would fetch these newly created entries in a separate call to the api controller.

Its pretty much like a chat application. But entries are created by the background task.

Now the problem is, these entries are not available instantly sometimes but after few seconds. Sometime its instantly available.

Any guidance is appreciated.
May 21, 2014 at 4:58 PM
The issue can be that the transaction your background task opened wasn't committed and thus the controller sees old data. You could try explicitly committing the transaction from the bg task by Calling ITransactionManager.RequireNew() after you've done the work.

By singleton I hope you mean ISingletonDependency BTW, but then again using singletons is rarely a good idea, be careful.
May 21, 2014 at 5:40 PM
You are right about singleton class. It implements ISingletonDependency.
Here is the code I used to perform the background task.
 public void DoWork()
            using (var workContext = this.m_workContextAccessor.CreateWorkContextScope())
                    var activityService = workContext.Resolve<IActivityService>();
                    var allActivities = activityService.GetAllActvities();////Retrieves the data from the database.
                    ITransactionManager transactionManager = workContext.Resolve<ITransactionManager>();
                    foreach (var activityId in allActivities)
                           //Do some db related work.

                            //// TransactionManager RequireNew commits all the changes made so far and creates a new transaction.
                        catch (BusinessException businessException)
                            Logger.Error(businessException, "Error while processing assessment with id {0} task", activityId);
                catch (Exception generalException)
                    Logger.Error(generalException, "Error occured in the ActivityMonitor.");
May 22, 2014 at 9:53 AM
Why don't you use the existing features for background tasks, in Orchard.WorkQueues ?
There is already and engine writen, totally integrated and maintained inside.
May 22, 2014 at 2:38 PM
Which module are referring by "Orchard.WorkQueues"?
If you mean by IBackgroundTask interface, It might not suit my requirement. I would need almost live activity notifications to the user. Orchard background tasks are triggered at a minute interval.
May 22, 2014 at 2:45 PM
Yes you are right this parameter should be adaptable.
May 22, 2014 at 3:03 PM
I would like to keep the IBackgroundTask the way it is since it runs lot of other tasks and they are good being executed at a minute interval.

I would like to keep this activity monitor an exception.
May 22, 2014 at 3:30 PM
Edited May 22, 2014 at 3:31 PM
Ok. It makes sens.
By curiosity how do you check that each loop doesn't stay longer than the 3 seconds ?
May 22, 2014 at 3:36 PM
Its pretty much like SweepGenerator.cs.

If the previous cycle is not done yet, just skip the tick event until the previous task is done. Worst case the background task will be running constantly.