BackgroundTask & Sweep()

Topics: Core, Customizing Orchard, General, Troubleshooting
Oct 21, 2011 at 7:22 PM

Hey guys!

I'm implementing business logic to perform data retrieval programmatically as a BackgroundTask which should run 3 times a day (at 7am, 11am, and 3pm).

So I've customized the SweepGenerator() method to have an interval of 60min rather than 1min

public SweepGenerator(IWorkContextAccessor workContextAccessor) 
{
       _workContextAccessor = workContextAccessor;
       _timer = new Timer();
       _timer.Elapsed += Elapsed;
       Logger = NullLogger.Instance;
       Interval = TimeSpan.FromMinutes(60);
}

and I have a Sweep() method in one of my Controllers that looks like this:

public
void Sweep() { DateTime currentTime = DateTime.Now; if ((currentTime.Hour == _7AM.Hour) || (currentTime.Hour == _11AM.Hour) || (currentTime.Hour == _3PM.Hour)) { ArticleGroupPartRecord articleGrp = _repository_articlegroup .Fetch(x => x.Id > 0).SingleOrDefault(); if (articleGrp != null) { // call PullBraftonArticles() PullBraftonArticles(articleGrp.Id); } } }

When I test this by publishing locally to IIS everything works fine - the background task runs at the appropriate times and pulls the correct data.

My problem occurs when we publish out our code to a live/production server; the background task never runs.

Could this be an issue with who is logged in to the Orchard Admin? Any other thoughts?
public void Sweep()
        {
            DateTime currentTime = DateTime.Now;

            if ((currentTime.Hour == _7AM.Hour) ||
                (currentTime.Hour == _11AM.Hour) ||
                (currentTime.Hour == _3PM.Hour))
            {
                ArticleGroupPartRecord articleGrp = _repository_articlegroup
                    .Fetch(x => x.Id > 0).SingleOrDefault();

                if (articleGrp != null)
                {
                    // call PullBraftonArticles()
                    PullBraftonArticles(articleGrp.Id);
                }
            }
        }
Coordinator
Oct 21, 2011 at 8:26 PM
Edited Oct 21, 2011 at 8:28 PM

mmh, well, if your app domain has a short timeout and you don't get lots of visits, then it may be that the task won't run because there is nothing at all running, but it should next time somebody visits the site. Does it? Oh wait, you implemented that in a controller? Well I'm not sure what your sweep generator is doing here. It doesn't seem to be scheduling anything.

Oct 21, 2011 at 8:56 PM

Bertrand,

SweepGenerator is a native Orchard Framework object located under Orchard.Tasks module.

All I've done with it is change the interval that it sweeps from the default (1min) to 60min.

I'm assuming that every 60 minutes it will call all Sweep() methods that are located in a class that implements IBackgroundTask.

 

You have a good idea with the timeout, I will look into that in the meantime.

Coordinator
Oct 21, 2011 at 11:35 PM

oh, ok, well, you really shouldn't be modifying anything in framework, as this will get overwritten next time you upgrade. I'm not sure why you would actually, seeing the times when you want this to run. Looks like a regular scheduled task should work just fine.