Sitecore includes several means of running automated jobs at regular intervals, e.g. using agents or scheduled tasks. Some developers have the impression that these don’t run consistently, which is usually caused by improper IIS or Sitecore configuration. The example below is based on IIS 7 and Sitecore 6.5.
Sitecore (i.e. the Sitecore website) is run in a IIS worker process. “Sitecore starts up” (the worker process is created) when the server receives a request which is mapped to a URL handled by the site. Below is an explanation of why Sitecore may sometimes seem to “shut down without reason” thus not running agents and scheduled tasks.
Each website hosted in IIS is associated with a specific application pool, which is configured in the “Basic Settings” for the site in question, using the IIS Manager.
Every application pool has a timeout which can be changed by selecting the appropriate application pool and editing its advanced settings.
The timeout is reached once no requests have been received for n minutes. When the application pool timeout is reached, the associated worker process shuts down (i.e. “Sitecore shuts down”).
Some benefits in preventing the application pool from timing out (“preventing Sitecore from shutting down”) include:
- Scheduled tasks and agents run as expected, even during times of low traffic.
- Users don’t experience the initial delay when the worker process starts up, which is mainly caused by Sitecore loading configuration files, initializing caches and so forth.
Sitecore comes with two built-in features which in tandem prevent application pool timeouts: the KeepAlive-page and the UrlAgent. The UrlAgent (Sitecore.Tasks.UrlAgent) is a simple Sitecore agent which accesses a predefined URL at regular intervals. The keep alive page is basically an empty ASP.NET form which is located in “[webroot]/sitecore/service/keepalive.aspx”. An example can be seen at http://www.sitecore.net/sitecore/service/keepalive.aspx.
Out of the box Sitecores Web.config only contains a “naive” UrlAgent configuration: it assumes an application pool timeout of more than one hour and a website which is mapped to localhost, as shown below.
<agent type="Sitecore.Tasks.UrlAgent" method="Run" interval="01:00:00"> <param desc="url">/sitecore/service/keepalive.aspx</param> <LogActivity>true</LogActivity> </agent>
To configure the UrlAgent for actual use, copy the XML shown below into a configuration file (e.g. “KeepAliveAgent.config”), change the interval attribute and URL parameter as required, and place it in “[webroot]/App_Config/Include”. Sitecore will merge these settings into the main Web.config file when your site starts up.
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/"> <sitecore> <scheduling> <!-- The time format is "HH:MM:SS" --> <agent type="Sitecore.Tasks.UrlAgent" method="Run" interval="00:15:00"> <!-- Replace [WEBSITE] with the appropriate domain --> <param desc="url">http://[WEBSITE]/sitecore/service/keepalive.aspx</param> <LogActivity>true</LogActivity> </agent> </scheduling> </sitecore> </configuration>
When configuring a UrlAgent to prevent application pool timeouts keep the following in mind:
- The “interval” attribute value must be less than the application pool timeout.
- Practically the lower limit of the interval is equal to the interval set in the node “/configuration/sitecore/scheduling/frequency” in Web.config (expressed as XPath), which is 5 minutes by default.
- When not set to an absolute URL, the UrlAgent assumes that the specified partial URL is located on localhost. In many scenarios this is not the case (e.g. multiple sites per server), so it’s usually better to provide a publicly reachable absolute URL.
- The “keep alive page” can be any page on the website, but the default page is well suited since it doesn’t contain any functionality or visitor tracking scripts.
Once set up correctly, entries like the following should start showing up in the Sitecore log, unless the “LogActivity” parameter is set to “false” for the UrlAgent:
ManagedPoolThread #n HH:MM:SS INFO Job started: Sitecore.Tasks.UrlAgent ManagedPoolThread #n HH:MM:SS INFO Scheduling.UrlAgent started. Url: http://[WEBSITE]/sitecore/service/keepalive.aspx