Thursday, March 17, 2016

Add a Pacemaker to your Sitecore

The topic of Sitecore scheduling agents and tasks have been blogged about in the past.  I often refer back to this post by John West:

To ensure your scheduled agents and tasks run at expected times, the Sitecore instance needs to be running.  If you have these configured on a Sitecore server that is used infrequently, there is a possibility your worker process will shutdown from inactivity.  There is an agent that runs in Sitecore already to keep the worker process alive (Sitecore.Tasks.UrlAgent).  But this process and any other Sitecore process will only run if the website is still active and running (i.e. the app. pool wasn't recycled).

An Application Pool can get recycled if you update the web.config, if you set your app. pool to recycle at a specific time, if your system gets rebooted after OS patches are installed, or other similar reasons.

In our specific instance, we have a content management server configured to run custom tasks that find Sitecore items with an expiration date/time field set and will email a report of those.  We also have our app. pool recycled overnight when no one would be on the system.  We found that rebuilding indexes and some other tasks really bloat the app. pool, and so recycling it automatically daily was a nice solution, instead of only doing it once things started running slow.

Our solution to this, was to create a Windows Scheduled Task that would hit the website periodically.  We take advantage of that lightweight page request that Sitecore uses already, /sitecore/service/keepalive.aspx.  Then we take advantage of Powershell's ability to access .NET assemblies and we can one-line a Url request:

powershell -ExecutionPolicy unrestricted -Command "(New-Object Net.WebClient).DownloadString(\"http://sitecoreCM.mydomain.com/sitecore/service/keepalive.aspx\")"

Finally, we setup our scheduled task to run daily.  And repeat the task every 20 minutes (NOTE: 20 minutes isn't a value available in the combobox, but you can just type in that text and the task scheduler will respect it).

You can set this up on any machine, although running it on the same server hosting your Sitecore web instance is recommended, so you can eliminate the need to keep another computer on and/or work around any network connectivity issues that may exist between those machines.

No comments:

Post a Comment