Santa

Let’s Talk

We would love to hear from you. Want to know more about
our services or have any questions? Say Hi!

What is Sitecore Job Scheduler?

March 24, 2023
What is Sitecore Job Scheduler?
Juanita Xavier
Juanita Xavier
Project Manager
sitecore-learning-what-is-sitecore-jobs-scheduler

The Sitecore Jobs Scheduler is a type of activity that automatically runs to perform a pre-defined task.

Sometimes, it is necessary to automate the common tasks for decreasing the manual efforts. Let us say that we want to trigger some emails on different intervals based on conditions. Or that we want to schedule the publishing of an item.

Sitecore provides you a way to schedule such routine tasks known as Scheduled tasks. It is a very simple way to execute the code logic within the defined interval.

Scheduling the tasks allows you to predefine the actions that are to be executed automatically whenever a certain set of conditions are met or found to be true. For an example, we can schedule a backup activity in the SQL Server Management Studio at specific time intervals. This will ensure that it automatically starts the process of backup and take the latest backup of the database.

Similarly, the Sitecore Schedulers also do the same activities for conducting some specific operations.

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

sitecore-learning-what-is-sitecore-jobs-scheduler-1

Scheduling a task involves two main elements or two types of Sitecore items in the content tree and those are:

  1. Commands
  2. Schedules

Usually, creating or scheduling the tasks is a three-step process:

  1. Writing the code for execution
  2. Creating the command
  3. Creating the scheduler

For creating a command, we have to write a code that is going to be executed. So, one has to create a Dot Net class that contains the logic written within a method that is named as a command from the scheduler.

The method has to accepted these below given three arguments:

A. An array of the 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 an example

We have created a class known as SendEmail and also a method Execute inside the very same class. The following method will then be called when the scheduler will run within the specified time interval and the code that is written inside this method will be executed every single time.

namespace Sitecore.Common.lWebsite.Services

public class SendEmail 
{ 
    public void Execute(Sitecore.Data.Ttems.Ttem[] items, Sitecore.Tasks.CommandItem commandItem, Sitecore.Tasks.ScheduleTtem scheduleItem) 
        { 
            try 
            { 
                //write your logic here 
            } 
            catch (Exception e) 
            { 
                Log.Error("Scheduler Exception:" + e.InnerException.Message, this); 
            } 
        } 
}                        
                    

Now, for calling this method from a scheduler, we have to create a command under /Sitecore/Sytem/Tasks/Commands/

Create a new command with the name SendEmail using the template for the path /Sitecore/templates/Sytem/tasks/Command

sitecore-learning-what-is-sitecore-jobs-scheduler-2

Now, you have to provide the information in the content section for the newly created command as it is 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

sitecore-learning-what-is-sitecore-jobs-scheduler-3

After this, we need to create a schedule for this task under the path /sitecore/system/Tasks/Schedulers/ for running the command at a specific interval.

Now, create a new scheduler named SendEmailSchedule using the template from the path /sitecore/templates/System/Tasks/Scheduler

Then, provide the information in the content section for the newly created scheduler.

  1. In the command field, select or specify the command path (the command that was created in the earlier step)
  2. In the scheduler field, specify the time for example how often the job needs to run.
  3. Items field: If we want to populate the Sitecore items array in our class method, then here is where it can be defined.
  4. Last run field: The last run field will be updated by Sitecore automatically whenever the task gets executed.
  5. Async field: We can mark Async field to run the command asynchronously.

(The tasks that are long-running or never ending will run side by side simultaneously in the case of the async execution which may also cause Sitecore to slow down.)

Auto remove field: We can make Auto remove field to automatically remove the schedule definition item whenever the tasks are getting expired.

sitecore-learning-what-is-sitecore-jobs-scheduler-4 sitecore-learning-what-is-sitecore-jobs-scheduler-5

20190903|99990101|127|00:05:00

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

{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.

  2. 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.,

    • To run the task on Saturday and Sunday, add the values, 1+64 = 65.

    • 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

    • 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 you occasionally need to execute a task. But, if at all it is needed for running an everyday task like publishing content once in a day and at a specific time, then it is not exactly that simple.

We of course can schedule a task to run every 24 hours but we cannot make sure that the execution will be at the same time every day because of the interval-based nature of the scheduled execution.

The task schedules are not based on absolute time but they are actually based on interval execution. Hence when you are scheduling tasks at a defined time that is hour or minute of the day, it is impossible.

Sitecore usually periodically performs a check for any tasks that due for running. This checking interval is defined in Sitecore configuration file called Sitecore.config as it is shown below:

00:05:00

master

/sitecore/system/Tasks/Schedules

true

The frequency value is set for a default of 5 minutes and it determines when the global Sitecore task runner should run if at all.

The agent determines when the tasks that are configured in the master database at the root /sitecore/system/Tasks/Schedules should run.

The agent will then run with 5-minute intervals and if another task is running, it could even block the task runner and delay the agent from running. Task execution requires an active AppPool so if Sitecore is not running then the scheduled tasks also won’t be executed.


YOU MAY ALSO LIKE