Run sitecore scheduled task at the same time every day
I need to run a sitecore scheduled ta开发者_StackOverflow中文版sk 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/
Try turning on Async execution, that should cause the "Last run" time to be updated immediately, even while the task is still running. Also, for your last argument, you can use "1.00:00:00" instead of "23:59:59" to get it to execute every 1 day.
EDIT: Another possibility is the frequency of the DatabaseAgent. How often is it running? If it is only running every 10 minutes, the default, then the check would not necessarily happen on the minute you want it to. Only when it wakes up. Try changing the interval to 1 minute or even less.
<!-- Agent to process schedules embedded as items in a database -->
<agent type="Sitecore.Tasks.DatabaseAgent" method="Run" interval="00:10:00">
<param desc="database">master</param>
<param desc="schedule root">/sitecore/system/tasks/schedules</param>
<LogActivity>true</LogActivity>
</agent>
It's quite hard to cause something to run at the same time every day. Most of the time it's because Sitecore thinks of time in terms of ticks, not time of day. So time between app recycles are the ticks.
We've done this same thing before by implementing a Windows Scheduled Task. Our task calls wget (Windows version) and wget makes a request to a special page on our site. We have IP restrictions on that page so only the server can hit it via a request through wget. We've never had any problems with it, and if we ever need to manually run the code, we either hit the page from the server or just log onto the server and run the scheduled task at that time.
精彩评论