Monday, April 21, 2008

Sitecore Scheduling Explained


Sitecore provides a framework for the process scheduling out of the box. This functionality is often used in the scenarios when it is important to automate a background process of any sort such as content sync or import, cleanup, etc. So it is critical to understand the underlying logic of this process which is really straightforward, but may be unknown for the newbies.

The Sitecore Scheduler is basically a background process that is started with the sleep interval defined in the <frequency> section.
The skeleton of the <scheduling> section looks like this:

   1: <scheduling>
   2:  <frequency>00:10:00</frequency>
   3:  <agent ...>
   4:  </agent>
   5: </scheduling>

Within the execution of the Scheduler, it queries the inner contents for the agents defined there and adds to the queue only those agents which marked as "due". An agent is considered "due"  if the value of the "interval" parameter is less than the timeframe of the last execution. In other words, if the agent was run 4 minutes ago last time (the frequency is set to 1 minute for example) and the interval parameter of the agent is 5 minutes, it is obvious that it will not run this time. So you may find it convenient to think about the global frequency parameters as an external loop.

An agent is considered disabled if the "interval" parameter is set to zero seconds ("00:00:00").

After the logic is able to retrieve a list of agents marked as "due", each agent is executed as a job process and the last run date is updated.

Here are some examples of what is expected in two different configurations below:

1. The case when "MyAgent" will not be executed each 10 seconds due to the bigger value in the "frequency" parameter. The agent will still be executed each 10 minutes. This is a common configuration issue for the Staging module.

   1: <scheduling>
   2:   <frequency>00:10:00</frequency>
   3:   <agent type="MyProj.MyAgent, MyDLL" method="Run" interval="00:00:10"/>
   4: </scheduling>

2. In this case the MyAgent code will be executed roughly each 10 minutes as specified in "interval" but will be triggered for the "IsDue" check each 10 seconds:

   1: <scheduling>
   2:   <frequency>00:00:10</frequency>
   3:   <agent type="MyProj.MyAgent, MyDLL" method="Run" interval="00:10:00"/>
   4: </scheduling>

Hope this makes sense.

5 comments:

Anonymous said...

Hi Alex,

I wonder if you recommend people using the database-based task scheduling, or web.config agents are always better?

Unknown said...

Hi Alexey,

Thanks for bringing this up!
The database-based tasks are running inside of the 'DatabaseAgent' which adds another layer of complexity when figuring out the proper timing parameter for your schedule process.
Another thing is that having a separate agent defined in web.config is clearer - you see what is going on straight away without necessity of going to the database and check, plus the tasks will need to be published as items to be working on the content delivery servers.
So it looks like web.config-based agents are the safest bet...

Anonymous said...

I think one advantage of the database agent is that you can create tasks automatically without updating web.config, for instance single-run tasks and things like that.

Anonymous said...

I need a scheduler to run at a fixed hour once a day, how can i do something like that ?

Unknown said...

You can use something like this:
http://sitecoresupport.blogspot.com/2007/02/publish-at-specific-time.html