Agents vs schedule task in sitecore

Introduction

Any frequent or regular task could be achieved with both approaches available in sitecore although we have another option of windows task scheduler which is not that much feasible because of ASP.NET architecture that make it unrealistic to expect to invoke a process at a very specific time, and the fact that the ASP.NET worker process might not be active when you want Sitecore to invoke a process.

Both approaches are best and worst in a some specific scenario, Let’s have an overview of both approaches.

Agents:

In sitecore agents are simple and straightforward to configure in web.config just create a new config file in include and patch your agents in <schedule> as below

<?xml version="1.0"?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <scheduling>
      <agent type="YourNameSpace.ClassName" method="Run" interval="00:01:00" />
    </scheduling>    
  </sitecore>
</configuration>

Here type is your agent’s class reference and in method attribute need to pass starting method which needs to be triggered when agent is called whereas interval is standard .Net time span “HH:MM:SS”

Sample Code:

namespace YourNameSpace.Agents
{
    public class FormEntriesReportSender
    {
        static object lockObject = new object();
        static bool inProgress = false;       
        public string Message
        {
            get { return "FormEntriesReportSender: " + DateTime.Now.ToString(); }
        }
        public void Run()
        {
            lock (lockObject)
            {
                if (inProgress)
                {
                    return;
                }
                inProgress = true;
            }

            try
            {
               // You logic goes here
            }
            catch (Exception ex)
            {
                Sitecore.Diagnostics.Log.Info(this + " : " + this.Message + ": " + ex.Message, this);
            }
            finally
            {
                lock (lockObject)
                {
                    inProgress = false;
                }
            }
        }                
    }
}

Scheduled Task:

Schedule tasks are more often used when need to run task on a specific time frame with less deviation and wants to have the ability to manage schedules from sitecore backend instead of config file which cause the app to restart on every change.

First we need to create a task’s definition item in sitecore’s backend to reference physical class that have the logic for task that need to be run.

Sample Task:

namespace YourNameSpace.Scheduler
{
    public class FormEntriesReportScheduler
    {

        public void Run(Sitecore.Data.Items.Item[] items, Sitecore.Tasks.CommandItem command,
        Sitecore.Tasks.ScheduleItem schedule)
        {
            try
            {
                // Do some basic stuff -- Your main logic goes here
            }
            catch (Exception ex)
            {
                Log.Info("Exception:1 Thrown in Form Schedule, Error: " + ex.Message, new object());
                // Log error
            }
            finally
            {
               // Logic that need to run after scheduled task completed, Like reset schedule
            }
        }
    }
}

Here are the steps to create task’s definition item in database

Step 1:
Login to sitecore’s backend and navigate to “/sitecore/system/Tasks/Commands” item

Step 2:
Right Click “Insert > Command”

sitecore-agent-vs-schedule-2

 

Step 3:
Provide type and method name for task

sitecore-agent-vs-schedule-1

Now we need to create schedule for our task

Step 1:
Login to sitecore’s backend and navigate to “/sitecore/system/Tasks/Schedules” item

Step 2:
Right Click “Insert > Schedule

sitecore-agent-vs-schedule-3

Step 3:
Provide Command, schedule, last run and Async(recomended) etc.

sitecore-agent-vs-schedule-4

In command field select defination item of task that you wants to schedule where as Last Run defines the timestamp when task was ran last time and would be executed again after interval(timespan) configured in Schedule field last run field could be configured at anytime. You should select Async true if wants to run method asynchronously.

Schedule field configuration is a bit complex and you may use an utility called Sitecore Shell Wax to make it simple and understandable for content authors.

Let’s explore schedule field, Here in example we are running this task every day starting from 01 jan 2000 to 01 jan 2100 with delay of 23:59:59 (almost 24 hours) pipe separated.

Sitecore scheduled task

20000101|21000101|127|23:59:59



20000101 Start Date 01 jan 2000

21000101 End Date 01 jan 2100

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

23:59:59 Delay

Some time it happens that tasks run late or take too much time for executing or for some unknown reasons last run time does not change as required so may do a trick to reset Last Run field and will look into this logic to run a task everyday at almost same time in next Post.

Leave a Reply

Your email address will not be published. Required fields are marked *

rfwbs-slide