Sometimes it is necessary to automate some common tasks to decrease manual efforts. Let’s say we want to trigger some emails in an interval, based on some conditions. Or we want to schedule the publishing of an item. Sitecore provides a way to schedule routine tasks called scheduled tasks. It is a simple way to execute code logic within a defined interval.

Scheduling allows any predefined actions to automatically get executed whenever a certain set of conditions is met or found to be true. For e.g., We can schedule a task to send an email to a specified user whenever a certain event occurs within a system.

We can configure scheduled tasks in the Sitecore under the location /Sitecore/System/Tasks

Scheduled tasks in Sitecore

Task scheduling involves two elements, or we can say there are two types of Sitecore items in the Sitecore content tree, and those are:

1:Commands
2:Schedules

Creating and scheduling tasks involves a three-step process:
1: Writing the code to be executed
2: Creating a command
3: Creating a scheduler

To create a command, we will have to write a code that will be executed. So, one must create a .net class that contains the logic written within a method named as a command from the scheduler.

The method must accept these three arguments:

A: An array of items of type Sitecore.Data.Items.Item
B: Sitecore task CommandItem of type Sitecore.Tasks.CommandItem
C: Sitecore task ScheduleItem of type Sitecore.Tasks.ScheduleItem

For e.g.,

We have created a class by the name SendEmail, and a method Execute inside the class.

The following method will be called when the scheduler will run within a specified interval, and the code written inside this method will be executed every time.

Scheduled tasks in Sitecore

Now to call this method from a scheduler, we will create a command under /sitecore/system/Tasks/Commands/

Create a new command named SendEmail using the template from the path

/sitecore/templates/System/Tasks/Command

Scheduled tasks in Sitecore

Provide information in the content section for the newly created command, as shown below:

  • In the Type field: Enter the assembly qualified type
  • In the Method field: Enter the name of the method to be called from class

Type: Sitecore.Common.Website.Services.SendEmail, Sitecore.Common.Website

Method: Execute

Scheduled tasks in Sitecore

Now we need to create a scheduler for this task to run the command at a specific interval, under the path

/sitecore/system/Tasks/Schedulers/

Create a new scheduler named SendEmailSchedule using the template from the path

/sitecore/templates/System/Tasks/Scheduler

Provide information in the content section for the newly created scheduler.

1: In the command field: Select/Specify the command path (Command created in the earlier step).
2: In the scheduler field: Specify the time ie., how often the job needs to run.
3: Items field: If we want to populate Sitecore items array in our class method, then it can be defined here.
4: Last run field: It will be updated by Sitecore automatically whenever the task gets executed.
5: Async field: We can mark this filed to run the command asynchronously.

(Long running or never-ending tasks will run simultaneously in the case of async execution which may slow down Sitecore)

  • Auto remove field: We can mark this field to automatically remove the schedule definition item whenever the task gets expired.

Scheduled tasks in Sitecore 5

The configuration setting for the field named ‘schedule’ uses a pipe separated string as below, which determines when the task should run:

{start timestamp} | {end timestamp} | {days to run bit pattern} |{interval}

  1. Start timestamp and end timestamp: It determines the start date and end date of the scheduled task. The date format is YYYYMMDD.

Example: 20190903 i.e. September 3rd, 2019.

  1. Days to run: It uses a 7-bit pattern which determines, the days when a task must run:

1 = Sunday

2 = Monday

4 = Tuesday

8 = Wednesday

16 = Thursday

32 = Friday

64 = Saturday

Days to run is the sum of the day number when the task must run

E.g.,

  1. To run the task on Saturday and Sunday, add the values, 1+64 = 65.
  2. To run the task every day the number will be 127 i.e. sum of all the days

(Sunday + Monday + Tuesday + Wednesday + Thursday + Friday + Saturday)

= (1+2+4+8+16+32+64) = 127

  1. Interval: It determines the time lag between each run. 00:05:00 means that the task will run after every five-minute interval.

The minimum interval for the task to be executed is in HH.MM.SS format.

The task is now set up to run at a specified interval.

Additional information:

  • Sitecore's built-in task scheduler works well when we need to execute a task occasionally. But if is needed to run an everyday task like publishing content once a day and at a specific time, then it is not that simple. We can schedule a task to run every 24 hours, but we cannot make sure that it will get executed at the same time every day because of the interval-based nature of the scheduled execution.
  • Task schedules are not based on absolute time but are based on interval execution. Hence scheduling tasks to execute at a defined time – hour or minute of the day is impossible.
  • Sitecore periodically performs a check for any tasks that are due to run. This checking interval is defined in Sitecore configuration file, named as Sitecore.config as shown below:

00:05:00

master

/sitecore/system/Tasks/Schedules

true

  • Frequency value is set default to 5 minutes, it determines when the global Sitecore task runner should run at all.
  • The agent determines when the tasks configured in the master database at the root /sitecore/system/Tasks/Schedules should run.
  • The agent will run with 5-minute intervals. If another task is running, it could block the task runner, delaying the agent from running.
  • Task Execution requires an active AppPool, so if Sitecore isn't running, scheduled tasks won't execute.

Need more guidance on Sitecore? Get in touch with our experienced consultants who have helped businesses around the globe in implementing and migrating to Sitecore. Write to us to discuss how we can help you and your team become more efficient. In case of query, add your comments below.

Posted by Advaiya

    Let's connect to make technology work for you.





    Please tick the options most relevant to your business challenges
    Decision makingBusiness productivityCustomer experienceTechnology-led innovationDigital transformation


    [By using this form you agree with the storage and handling of your data by this website.]