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?
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With