Running Scheduledtasks with the jobmanager in Sitecore
I’ve recently had the need to execute a scheduled task. The simple way would be to fetch the scheduled task item from Sitecore an instantiate it as a ScheduledItem type found in Sitecore kernel namespace, and the Call the execute method found in the class.
Item item = database.GetItem(taskId); ScheduleItem scheduleItem = new ScheduleItem(item); scheduleItem.Execute();
This of course Will run the task in the context of the current user. This could lead to some minor issues where the user running task doesn’t have the same rights as the Sitecore build JobManager.The solution is to execute the task via the JobManager, which also is used by scheduler. This is done by providing simple JobOptions. The example belows instantiate a new MyTask and runs the execute method.
JobOptions options = new JobOptions("Job Runner", "schedule", "scheduler", new MyTask(), "Execute", new object[] { scheduleItem.Items, scheduleItem.CommandItem, scheduleItem }); options.SiteName = "scheduler"; JobManager.Start(options).WaitHandle.WaitOne();
The last parameter passed in as a new object is the parameter list passed on to the Execute Method.
Pretty cool. The Job Manager is actually a cool piece of architecture of Sitecore.
I once wrote a small Shared Source Module which lists active executing background threads (Jobs). It is a copy of the Windows Task Manager combined with a performance monitor.
http://trac.sitecore.net/TaskManager/wiki/UserDocumentation
My tool “Scheduled Task Utils” : http://trac.sitecore.net/ScheduledTaskUtils use this technique to list and easily launch a scheduled task.