Scheduled Tasks in Sitecore: Everything's Relative
Creating Tasks in Sitecore
In a big, complex CMS like Sitecore, the ability to execute common tasks in a routine, automated way can greatly help system administrators manage the CMS.
Sitecore does provide a way to schedule routine tasks, but the devil is in the details:
- Task schedules are based on interval execution, not absolute moments in time. Scheduling tasks to execute at a particular hour or minute of the day is impossible.
- Sitecore periodically polls for any tasks that are due to run. This polling interval is defined in Sitecore's configuration: /sitecore/scheduling/frequency (Sitecore.config) and defaults to 5 minutes.
- The polling requires an active AppPool, so if Sitecore isn't running, scheduled tasks won't execute.
- Polling begins when the Sitecore App Pool starts, not at a specific time.
Scheduling tasks involves two types of items in the Sitecore tree: Command and Schedule. Both items are located under /sitecore/System/Tasks.
If you only need to execute a task occasionally, Sitecore's built-in task scheduler works pretty well. But what if you need to fire of a common task - say, publish content once a day - at a specific time? While you can schedule a task to run every 24 hours, you cannot be sure it will execute at the same time every day because of the interval-based nature of the schedule execution.
For more information about using the built-in task scheduling tools, refer to these blog posts:
- John West: All About Sitecore Scheduling: Agents and Tasks
- DEG Digital: How to Create a Sitecore Scheduled Task
You may eventually stumble upon this excellent blog post about scheduling tasks in Sitecore to run at the same time every day. I've had modest success with this technique, but this hasn't given me the consistency that I demand for production processes.
Using an External Task Scheduler
The best way to schedule tasks at a specific time of day is to use an external scheduling tool. Windows has a great one built in: the Task Scheduler.
To scheduled a task externally, you can use a URL-addressable webpage that executes the given task internally (for example, http://spark.local/utilities/RunMyTask.aspx). A small script (ie, VBScript or PowerShell) on the OS can call this webpage, and that script file can be scheduled to execute via the Windows Task Scheduler. As a bonus, you now have one extra layer of logging against your scheduled task (plus it is a lot easier to debug scheduling issues in Windows than Sitecore).
Whenever possible, I prefer to use the Sitecore Client to manage Sitecore-related processes. To that end, there is a great module available on the Sitecore Marketplace that do enable absolute scheduling for Sitecore tasks: Sitecron. As of June 2017, this is still an actively-maintained module and works great, especially if you're familiar with cron-based scheduling.
Do you have questions or comments about this post? Find me on Twitter: @SitecoreSpark