Click here to Skip to main content
15,889,742 members
Articles / Programming Languages / C#
Tip/Trick

Scheduling in Sitecore using Sitecron

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
20 Dec 2015CPOL3 min read 11K  
Using newly released Sitecron module in Sitecore for scheduling tasks within Sitecore

Introduction

Sitecore is a pioneer Content Management System (CMS) based on C# for .NET platform. It comes with a rich set of features to perform various CMS tasks. Kindly note that Sitecore is not free and requires a license which is a pre-requisite for this whole implementation.

There are certain situations in which we need to perform repeated tasks in CMS using jobs/scheduled tasks. Although Sitecore had built-in mechanism to do scheduling, recently SiteCron component has been included with Sitecore 7.2 and higher to make it a seamless task with minimal effort. 

What is SiteCron

SiteCron is Quartz.NET based job scheduler module supported in Sitecore. SiteCron can easily create simple as well as complex schedules to perform everyday scheduling. Quartz CronTrigger is used to implement and execute these jobs.

Quartz.NET is a pure .NET library implemented in C#. It is ported from Java scheduling framework, Quartz

Prior to SiteCron, sitecore scheduling used to get implemented using Agents and Tasks: All about sitecore scheduling:Agents and Tasks.   

CronTriggers

The core of implementing Schedules in SiteCron is CronTriggers. Some handy Cron expressions are provided below:

  • 0 30 9 * * ?    Fire at 9:30am every day
  • 0 30 21 * * ? *    Fire at 9:30pm (21:30) every day
  • 0 30 10 * * ? 2016    Fire at 10:30am every day during the year 2016
  • 0 30 9 L * ?    Fire at 9:30am on the last day of every month
  • 0 30 9 15 * ?    Fire at 9:30am on the 15th day of every month
  • 0 30 10 ? * MON-WED   Fire at 10:30am every Monday, Tuesday and Wednesday
  • 0 0 13 1/5 * ?    Fire at 1pm every 5 days every month, starting on the first day of the month.

For more information on CronTriggers, please visit Quartz.NET - Lesson 6: CronTrigger.

To try and test Cron Expressions, you can refer to Cron Maker Utility.

Download and Setup

You can download the SiteCron for sitecore from Sitecore Marketplace. Currently, the latest version is SiteCron 1.1.6. Download the package and install it into your sitecore instance. Once downloaded, a new config file Sitecron.config will be included into App_config/include folder. 

The below code snippet initializes the module with the sitecore initialize pipeline:

XML
<pipelines>
      <initialize>
        <processor type="Sitecron.InitializeSitecron, Sitecron" />
      </initialize>
</pipelines>

In case when any job is updated or deleted, it is automatically taken care by OnSaved and OnDeleted event by Sitecron:

XML
<events>
      <event name="item:saved">
        <handler type="Sitecron.Events.SitecronSavedHandler, Sitecron" method="OnItemSaved"/>
      </event>
      <event name="item:deleted">
        <handler type="Sitecron.Events.SitecronDeletedHandler, Sitecron" method="OnItemDeleted"/>
      </event>
</events>

CronTrigger is internally implemented by the below code snippet:

C#
IJobDetail jobDetail = JobBuilder.Create(jobType).Build();

ITrigger trigger = TriggerBuilder.Create()
    .WithIdentity(i.Name)
    .WithCronSchedule(i[cronExpressionField])
    .ForJob(jobDetail)
    .Build();
scheduler.ScheduleJob(jobDetail, trigger);

Here is an example of the IJob implementation:

C#
using Quartz;
using Sitecore.Diagnostics;

namespace Sitecron.Samples
{
    public class SampleLogJob : IJob
    {
        public void Execute(IJobExecutionContext context)
        {
            Log.Info("Sample Log Job - Add Log Entry.", this);
        }
    }
}

The above source code is included from SiteCron Marteketplace SiteCron 1.1.4 Source.

SiteCron module internally validates Publishing.PublishingInstance setting. This has no impact on single -server sitecore environment. However, if you have a multi-server environment, the schedule is triggered only on primary designated server. This setting ensures that the check to validate the current instance to the publishing instance is verified.

How to Setup?

Perform the below steps to set up a new job with SiteCron:

  1. Add a reference to Quartz.dll in your project using Nuget package update. For the purpose of this example, Quartz version: 2.3.2 is used.
  2. Create a new class which implements Quartz IJob interface.
  3. Build the project and deploy it into your sitecore folder.
  4. Login to Sitecore admin and navigate to /sitecore/system/Modules/Sitecron.
  5. Add a new SiteCron job.
  6. Set the Type to Class Namespace, Assembly.
  7. Set the Cron Expression for the schedule and save. Refer to the screenshot below:

    Image 1

The scheduled jobs are generally performed during the night to avoid peak traffic and for minimal impact on users.

References

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead 3 Pillar Global
India India
Working as Technical lead in 3 Pillar Global. Having experience in ASP.NET, C#, Web API, WebServices, SQL Server and other Microsoft technologies.

Comments and Discussions

 
-- There are no messages in this forum --