The Sitecore task runner, usually called Scheduled Tasks, is a simple way of executing code with intervals. You configure scheduled tasks in Sitecore, at /sitecore/system/Tasks/Schedules:
The quirkiest configuration setting is the “Schedule” field, which is a pipe separated string determining when the task should run:
{start timestamp}|{end timestamp}|{days to run bit pattern}|{interval}
- Start timestamp and End timestamp: Determines the start and end of the scheduled task.
Format is the Sitecore ISO datetime, YearMonthDayTHoursMinutesSeconds.
Example: 20000101T000000 = January 1st 2000 at 00:00:00.
(the font Sitecore uses does not help reading the timestamp at all, I know).
NOTE: If you do the format wrong, the task will run. - Days to run: A 7 bit pattern determining which days the task must run:
1 = Sunday
2 = Monday
4 = Tuesday
8 = Wednesday
16 = Thursday
32 = Friday
64 = Saturday
So, 127 means to run the task every day. To run the task on Saturday and Sunday, add the 2 values, 1+64 = 65. - Interval: How long time between each run. 00:05:00 means that the task will run with 5 minute intervals.
WHY DOESN’T MY TASK RUN WITH MY SPECIFIED INTERVALS?
Sitecore uses no less than 2 sitecore.config settings to determine when the task runner should run:
<scheduling> <frequency>00:05:00</frequency> <agent type="Sitecore.Tasks.DatabaseAgent" method="Run" interval="00:05:00"> <param desc="database">master</param> <param desc="schedule root">/sitecore/system/Tasks/Schedules</param> <LogActivity>true</LogActivity> </agent> </scheduling>
The frequency setting determine when the global Sitecore task runner should run at all.
The agent determine when the tasks configured in the master database at the root /sitecore/system/Tasks/Schedules should run.
So, in the example above, my task runner runs every 5 minutes, checking the config file for tasks to run. It will then run the agent with 5 minute intervals. If another task is running, it could block the task runner, delaying the agent from running. With the above settings, my best case scenario is that my agent runs every 5 minutes.
The tasks configured in Sitecore could also block. If a task should run every 5 minutes, but the execution time is 11 minutes, the agent would run the task again after 15 minutes, in the best case scenario. To avoid this, you can mark your task as “async” in the configuration, but beware that long running (or never ending) tasks will then run simultaneously, slowing down Sitecore.
CAN I HAVE TASKS RUNNING ON MY CM SERVER ONLY?
Yes, you can add a new folder in Sitecore, and then add a new agent that points to the new folder as root, to the sitecore.config file of the CM server.
See more here: Sitecore Scheduled Tasks – Run on certain server instance.
CAN I RUN TASKS AT THE SAME TIME EVERY DAY?
Kind of. You can have your task running once a day within the same interval, using a little code.
See more here: Run Sitecore scheduled task at the same time every day.
IN WHAT CONTEXT DOES MY TASK RUN?
Sitecore have created a site called “scheduler” where the context is defined:
<sites> <site name="scheduler" database="master" language="da" enableTracking="false" domain="sitecore" /> </sites>
To run the task in a different context, use a context switcher.
DO I HAVE A HTTP CONTEXT WHEN RUNNING SCHEDULED TASKS?
No.
DO I HAVE A USER WHEN RUNNING SCHEDULED TASKS?
Do not expect to have a user. Expect the Sitecore Scheduled Task – Schedule time format and other quirks to be NULL, unless you use a UserSwitcher.
CAN I RUN THE SAME CODE FROM DIFFERENT TASKS?
Yes. Sitecore have split the definition of the code to run from the definition of the schedule. The code is defined as a “command” where you define the class and the method to run:
The schedule simply points to the command to run, and you can have as many schedules as you want:
WHAT ARE THE “ITEMS” FIELD FOR?
No one really knows what the items field are for, but according to old Sitecore folklore, you can add a pipe separated list of item GUIDS (or even item paths), and the “itemArray” property of the method you call will contain the list of items:
public void Execute(Item[] itemArray, CommandItem commandItem, ScheduleItem scheduleItem) { foreach (Item item in itemArray) { // do something with the item } }
MORE TO READ:
- Sitecore Scheduled Tasks – Run on certain server instance by briancaos
- Run Sitecore scheduled task at the same time every day by briancaos
- How To Create A Sitecore Scheduled Task by DEG Digital
- Repost: All About Sitecore Scheduling: Agents and Tasks by John West
- Sitecore Schedule task not running from Stackoverflow
thanks a lot for the clarification!
LikeLike
Pingback: Automate Sitecore Workflow and Email Report | .Net | Sitecore blog
Pingback: Produce and Consume Messages with Sitecore – Neil Killen
Nice article, So in CD instances we need to mention web , right? If I configure it to run every minute, will it cause performance issues in production?
LikeLike
It is very rarely that you would like your CD server to run scheduled tasks, but yes, your CD server does not (usually) have access to the MASTER database, and you will need to create a separate config section for the CD servers.
The scheduler itself does not consume many resources, so running it every minute is not an issue – unless of course, that the task you run is resource heavy.
LikeLike