Home > .Net, C#, Sitecore 6 > Running Scheduledtasks with the jobmanager in Sitecore

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.

 

Categories: .Net, C#, Sitecore 6 Tags:
  1. 06/10/2011 at 13:01

    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

  2. 17/10/2011 at 09:24

    My tool “Scheduled Task Utils” : http://trac.sitecore.net/ScheduledTaskUtils use this technique to list and easily launch a scheduled task.

  1. 06/02/2012 at 12:36
  2. 09/08/2014 at 20:10
  3. 11/11/2014 at 13:59

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 )

Facebook photo

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

Connecting to %s

%d bloggers like this: