Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run sitecore scheduled task at the same time every day

I need to run a sitecore scheduled task at the exact time every day. Currently the task is scheduled the following way:

Schedule: 20100201T235900|20200201T235900|127|23:59:59
Last run: 2011 06 22 01:32:25

The task takes about 5 minutes to execute so 'Last run' gradually slips and is run later and later.

My main idea is to create a windows scheduled task that calls a webservice and resets the Last run time for the task in question.

Is there another way? Am I missing some configuration property that could achieve this much easier?

like image 785
marto Avatar asked Feb 24 '23 19:02

marto


1 Answers

I have developed my own solution for this.

First of all, modify your task to run every 1 minutes or so. It must run pretty frequently. Instead of forcing the task to run once, you make your task execute it's functionality at the time you like, and then wait untill the next day before executing the functionality again:

In this example I force my task to run once between 03:00 am and 04:00 am:

public void Execute(Item[] itemArray, CommandItem commandItem, ScheduleItem scheduleItem)
{
  if (!IsDue(scheduleItem))
    return;

  // DO MY STUFF!!
}

/// <summary>
/// Determines whether the specified schedule item is due to run.
/// </summary>
/// <remarks>
/// The scheduled item will only run between defined hours (usually at night) to ensure that the
/// email sending will not interfere with daily operations, and to ensure that the task is only run
/// once a day. 
/// Make sure you configure the task to run at least double so often than the time span between 
/// SendNotifyMailsAfter and SendNotifyMailsBefore
/// </remarks>
/// <param name="scheduleItem">The schedule item.</param>
/// <returns>
///     <c>true</c> if the specified schedule item is due; otherwise, <c>false</c>.
/// </returns>
private bool IsDue(ScheduleItem scheduleItem)
{
  DateTime timeBegin;
  DateTime timeEnd;

  DateTime.TryParse("03:00:00", out timeBegin);
  DateTime.TryParse("04:00:00", out timeEnd);



  return (CheckTime(DateTime.Now, timeBegin, timeEnd) && !CheckTime(scheduleItem.LastRun, timeBegin, timeEnd));
}

private bool CheckTime(DateTime time, DateTime after, DateTime before)
{
  return ((time >= after) && (time <= before));
}

Check the article for more detailed information: http://briancaos.wordpress.com/2011/06/28/run-sitecore-scheduled-task-at-the-same-time-every-day/

like image 142
Brian Pedersen Avatar answered Mar 15 '23 13:03

Brian Pedersen