Skip to content
August 9, 2014 / kiranpatils

Scheduled Task Basics

Challenge:

I’m sure, All of you must’ve worked on Sitecore Schedule Task in your day-to-day activities. And yes, if you are new to Sitecore world [First of all welcome to Sitecore community!] then it’s good to know a weapon named as Sitecore Scheduled Task. For automating lot of your business processes like Uploading feed daily, Generating some files daily, Generating some reports daily – This all you can configure — While you and everyone are sleeping — Sitecore scheduled task will make sure your task is done as per your configured time!

Now, coming to main point — While working with Scheduled tasks in last couple of months. We came across lot of nice challenges. Which I haven’t found documented on the web — Again they were not Rocket Science. But unfortunately can’t find blogs on it. And thought to document it. So, it may help you in future. And you no need to invest the same hours we invested within it. [That’s the main motivation to we write blogs — Correct? — You have been working on Sitecore since so long? OR Started learning Sitecore? Why don’t you share it with others? Because Sharing is Caring!]

Solution:

Before we delve in to those challenges. Would like to share the Basics of Scheduled Task :

scheduled tasks are run in sequence by the Sitecore scheduler. The scheduler checks within a certain interval (defined in the web.config in the /scheduling/frequency and /scheduling/agent settings) for tasks to be run. If a task is over due, it is run, and the time where the task finished is recorded as the next checkpoint.

Source : http://briancaos.wordpress.com/2011/06/28/run-sitecore-scheduled-task-at-the-same-time-every-day/

Nice blogs:

  1. http://www.sitecore.net/Learn/Blogs/Technical-Blogs/John-West-Sitecore-Blog/Posts/2010/11/All-About-Sitecore-Scheduling-Agents-and-Tasks.aspx
  2. http://sitecoreblog.alexshyba.com/2008/04/sitecore-scheduling-explained.html

So, done with Basics? Now, let’s move to specific challenges and solution:

  1. Context Database NULL : When you run a Sitecore Scheduled Task, Sitecore.Context.Database object will be always NULL. And if any method which you are calling from your Scheduled Task, relies on it. Then it won’t work. To Resolve this — It’s good to wrap your Schedule Task method calls using DatabaseSwitcher:
    using (new DatabaseSwitcher(Sitecore.Configuration.Factory.GetDatabase("YOURDBNAME")))
    {
    }
    
  2. Context Item NULL : When you run a Sitecore Scheduled Task, Sitecore.Context.Item object will be always NULL. And if any method which you are calling from your Scheduled Task, relies on it. Then it won’t work. To Resolve this — It’s good to wrap your Schedule Task method calls using ContextItemSwitcher:
    using (new ContextItemSwitcher(aItem))
    {
    }
    

    Just a note : It’s good to pass rootItem via Data Section

  3. Workflow bypass code is not working : If you’ve some code written which is by passing workflow programmatically, and you are calling it via Scheduled task. It won’t work. Because it needs to be executed from “shell” as a Context Site, and in scheduler’s case it is “scheduler” site. To resolve this change Active Site :
    Sitecore.Context.SetActiveSite("shell");
    

    Source :  http://naveedahmad.co.uk/2011/09/26/sitecore-programmatically-publish-an-item/ Also, This challenge is faced by my colleague — Bhavesh Maniya — And he found this trick — So, credit goes to him

  4. How to Debug/Test scheduled Task? : Now, you may ask, how to debug Sitecore Scheduled Task? How to trigger it forcefully to run now?  To Debug it, You can use the same approach, which you use for debugging any Sitecore code – Attach To Process and so on. And now on triggering it forcefully, There are lot of ways available here :                                                                                                  http://www.sitecore.net/deutsch/Community/Technical-Blogs/John-West-Sitecore-Blog/Posts/2012/03/Run-Scheduled-Agents-Interactively-in-the-Sitecore-ASPNET-CMS.aspx
    http://sitecorepm.wordpress.com/2012/11/30/execute-sitecore-scheduled-task-from-ribbon/
    http://blog.xcentium.com/2013/03/scheduled-task-helper/
    https://marketplace.sitecore.net/en/Modules/Sitecore_Task_Initiator.aspx [I use this one. Because of its simplicity]
    http://blog.istern.dk/2011/10/06/running-scheduledtasks-with-the-jobmanager-in-sitecore/                                          https://marketplace.sitecore.net/en/Modules/Scheduled_task_utils.aspx
    http://sitecoreblog.blogspot.in/2011/07/new-module-on-trac.html
  5. Miscellaneous
    1. You might find configuring schedule field bit complex — Here’s how it works : In the schedule definition item, in the Data section, in the Schedule field, enter parameters to control the schedule, separated by piple (“|”) characters. The first parameter indicates the start date for the schedule in yyyyMMdd format. The second parameter indicates the end date for the schedule in the same format, The third parameter indicates the days of the week on which to run the task. This basically works like a bit mask, where 1=Sunday, 2=Monday, 4=Tuesday, 8=Wednesday, 16=Thursday, 32=Friday, and 64=Saturday. So Monday through Friday is 2+4+8+16+32=62, while every day is 1+2+4+8+16+32+64=127. These values come from the Sitecore.DaysOfWeek enum. The fourth parameter is the minimum interval between invocations of the task in HH:mm:ss format.                                                                                                                                                                                                                             And this Module simplifies configuring it for you : https://marketplace.sitecore.net/en/Modules/Sitecore_Shell_Wax.aspx

My initial purpose for this post was to share our learning. But while I’m completing  this post, This blog post, will be a ONE STOP SHOP for all great scheduled task related resources! 🙂 That’s how life works, You start walking with a Good Cause, and when you look back, it looks like you did a great work! So, Keep walking!

Happy Sitecoring! 🙂

Good Reads:

  1. http://www.nehemiahj.com/2013/08/sitecore-scheduler-job-in-production.html
  2. https://sitecorebasics.wordpress.com/2011/04/12/scheduled-task-is-not-getting-executed/
Advertisements

4 Comments

Leave a Comment
  1. Harsh Baid / Sep 17 2015 12:56 am

    In my case Sitecore.Context.Database was NULL. Thanks for sharing 🙂

  2. Deepthi / Jul 16 2016 2:53 am

    Great Post! Just an additional note for people who want to schedule job every 24 hours the format of HH:MM:SS would not work properly if you give 24:00:00, you need follow format like –
    See format example below 20160714|21000714|127|1.00:00:00 You can refer my blog post for more details – https://deepthikatta.blogspot.com/2016/07/sitecore-81-update-3-bug-on-scheduling.html

    • kiranpatils / Aug 22 2016 5:13 am

      Thanks Deepthi!

Trackbacks

  1. Do you know why you check Async Checkbox while configuring Scheduled Task? | Sitecore basics!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: