Sitecore - Task Schedulers and how to schedule a task

Heythere! Welcome to "Sitecore Series"

Post is all about sharing the details about "Sitecore Schedulers and how to create and use Schedulers".

There are scenarios, where we want a particular task to be executed after some time intervals like for each 15 mins or for each 3 hrs etc..,


Below are some sample scenarios:

1) To create a items in CMS tree after reading it from feed.

2) To rebuild/refresh a solr index in specified intervals.

3) To send an email in specified intervals.

4) To clean up items in database.

To achieve this, Sitecore provides a feature called scheduler. Where it will allow us to schedule a task to be executed in the specified intervals. Want to know about scheduler creation and how to configure it? 


Let's check below steps :

1) Task Creation

2) Configuring a Task in CMS

3) Scheduling a Task.


1) Creating a Task:


using Sitecore.Data.Items;
using Sitecore.Diagnostics;
using Sitecore.Tasks;

namespace Testing.Foundation.Testing.Scheduler
{
    public class TestScheduler
    {   
        public void Execute(Item[] items, CommandItem command, ScheduleItem scheduleItem)
        {
            try

            {
            	//add logic as per requirement
                //ex: logic to fetch items from feed and creating items 
                //logic t rebuild/refresh index
                //logic to cleanup items
                //logic to remove versions from items 
                

                Log.Info("job ran through Scheduler", "");
            }
            catch (System.Exception ex)
            {
                //Log exception.....
            }
        }
    }
}

Refer my previous post, contains sample logic for rebuilding the solr index through scheduler.


2) Configuring Task in CMS:

Configuring task involves two steps: 

 a) Command Creation:

Path: /sitecore/system/Tasks/Commands/

Create a command item under this path and fill the type and method:

Type: class created in step1, namespace (ex: Testing.Foundation.Testing.Scheduler,TestScheduler, Testing.Foundation.Testing)


b) Schedule Creation:

Path: /sitecore/system/Tasks/Schedules/

Create a schedule item under this path and add the command created in above step(step a) and specify the schedule time in "schedule" field.


3) Scheduling a Task:

Schedule the task by setting time interval, once the command is added in schedule item in step b. 

For Scheduling a task:

Right click on schedule ->selects Scripts - > edit schedule and a pop up will open with details.

Just add the details as per your requirement, refer below screenshot for details:

Once the details are set -> click on "Change".

Now the task will run as per the scheduled time and after the task run, it will show the task last run time in the "Last Run" field.


Additional Details:

-> Context Database returns null inside scheduler, use DatabaseSwitcher to point to specific database.

-> Context Item return null inside scheduler, use ContextItemSwitcher or use DatabaseSwitcher and get the item by passing the path or id .

-> Sitecore Checks the tasks for each 5 mins to see if any tasks are due and if any task is due , it will try to execute accordingly. 

By default frequency is set to 5 mins, this can be updated by adding the patch as per your requirement.


Hope this post helps you. Happy Sitecoring... 😊.

Comments

Popular posts from this blog

Overwriting sxa content field in solr document

Displaying Custom 404 error page for urls with unallowed file extension in sitecore

How to specify custom richtext menu as default richtext field setting when no source is specified for richtext field in sitecore

How to rebuild custom solr index programmatically in Sitecore

Handling URL requests with different file extensions in Sitecore

Rendering Parameters and Rendering Parameter Templates in Sitecore

Accessing Items from Sitecore Database in different scenarios