Sitecore Scheduling with SiteCron 2.1.6

SiteCron has been around for a while now and I have used it on production systems for the past two years. I use it for triggering Sitemap file generation, PowerShell scripts for data syncs with external API and maintenance tasks. Shout out to Mike Edwards for fixing the OnItemSaved handler.

SiteCron NuGet Link: https://www.nuget.org/packages/SiteCron

SiteCron GitHub Link: https://github.com/akshaysura/Sitecron

SiteCron Marketplace Link: https://marketplace.sitecore.net/Modules/S/Sitecron.aspx?sc_lang=en

 

What’s New?

I use this module in every project I build. One of the things which bugged me the most was that it needed the Context DB to run in. For instance, on production CD’s we need it to run in the context of the Web or a custom db name like prod etc. Unfortunately, with the previous versions this was not possible.

  1. Ability to auto Archive the item after it executes once. This will be useful for those one time jobs. More exciting things to come off of this.
  2. Ability to run in a different context db than Master. This will allow you to use publish restrictions to run Jobs only on CM, CM & CD.

 

Installing SiteCron

To Install SiteCron, run the following command in your Visual Studio solution’s Package Manager Console.

Install-Package SiteCron

Once installed the NuGet package will add a few files to your solution.

Once the NuGet package is installed. Use the SiteCron Items Only Sitecore Package.zip file and install the package on your Sitecore instance via the Installation Wizard.

Once installed you can publish your code and you are good to go.

 

Using SiteCron

Now for the fun part. Using SiteCron truly depends on your needs. Out of the box, it can handle a Smart Publish and running a Sitecore PowerShell script. Apart from these two, you are free to implement your own code as long as you inherit from the IJob interface.

I personally use the PowerShell job to run custom PowerShell scripts for API Sync and Sitemap generation.

Let’s look at the Smart Publish job and see if we can figure out the mechanics of the Quartz jobs.

Smart Publish

Smart Publish job is a custom job I wrote to take care of smart publishing in Sitecore and be able to specify the target db. Here is how the Sitecore Item is setup:

Notice that the type filed has been set to a specific class in the SiteCron library. You could easily point this to your own library and write your custom Job. As long as you inherit from IJob interface and implement the Execute method you are golden. You can choose to use the Parameters AND/OR the Items field in your Job as needed. In this case I am only using the Parameters field.

Here is the code for this call:

 

Please review the comments to understand how we are leveraging Quartz to get our work done.

 

PowerShell

This by far is the most powerful Job in SiteCron. Sitecore PowerShell is a wonderful module and I do not know how I can get my work done without it. Adam Najmanowicz and Michael West have done such a wonderful job.

I use this Job to execute custom PowerShell scripts to run API syncs on the CM, Sitemap generation on the CD’s and nightly cleanup tasks.

I will go through this job code as well. First the Sitecore Item:

Notice that this job code exists in the SiteCron library as well. The code to this library is available in GitHub.

Essentially the Job is capable of running ONE Sitecore PowerShell script. Why ONE you ask? Well because there could be inter-dependencies among scripts, long running scripts and complications arising from it. I do however have commented code in the Powershell Execute Script Job which can tackle more than one PowerShell script at a time.

Please be sure that if you are scheduling PowerShell scripts, no client interaction commands exist in your script. Otherwise you will be hit by “Non interactive session cannot perform an interactive ‘WriteProgress’ operation”. I was able to clean up my PowerShell script and silence it by using the $ProgressPreference = ‘SilentlyContinue’.

Future versions of the Sitecore PowerShell module 4.7 and above might not need this.

Here is the code for this Job:

 

Custom Job

Now if you were to get started with a Custom Job. Here is what you need to do:

  1. Install Quartz from NuGet
  2. Create a new class lets call it HelloWorld.cs
  3. Inherit from IJob interface
  4. Implement Execute method
  5. Place your code inside of the Job
  6. Go to Sitecore admin and navigate to /sitecore/system/Modules/Sitecron
  7. Insert a new Sitecron Job
  8. Add in your library and class information for example: Sitecron.Jobs.PowerShell.ExecuteScript, Sitecron
  9. Setup the Cron Expression – use the link on the field name or http://www.cronmaker.com/
  10. Specify any Parameters or Items based on your Job needs

That is it!

If you have any questions or concerns, please get in touch with me. (@akshaysura13 on twitter or on Slack).

 

4 thoughts on “Sitecore Scheduling with SiteCron 2.1.6”

  1. Hi Akshay,

    Thanks for the latest version 2.1.6, we have implemented this on Sitecore CMS 8.2 rev. 161115. We got exception in logs as below:
    ManagedPoolThread #18 2018:02:02 00:08:59 ERROR One or more exceptions occurred while processing the subscribers to the ‘item:deleted:remote’ event.
    Exception[1]: System.InvalidOperationException
    Message[1]: Invalid event arg type: Sitecore.Data.Events.ItemDeletedRemoteEventArgs. Expected: SitecoreEventArgs
    Source[1]: Sitecore.Kernel
    at Sitecore.Events.Event.ExtractParameters(EventArgs args)
    at Sitecore.Events.Event.ExtractParameter(EventArgs args, Int32 index)
    at Sitecron.Events.SitecronDeletedHandler.OnItemDeleted(Object sender, EventArgs args)
    at Sitecore.Events.Event.EventSubscribers.RaiseEvent(String eventName, Object[] parameters, EventResult result)

    Please suggest.

  2. Hi Akshay,

    Small update its latest version 2.1.9 which is been installed on Sitecore CMS 8.2 rev. 161115, and throwing above error.

    Regards,

  3. Sanjeev,

    In the later versions we commented out the remote events in the SiteCron.config. THanks.

Leave a Reply

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