Intro
Scheduled tasks in Sitefinity CMS are objects that can execute a predefined set of operation at a particular point in time. You can use scheduled task… (read from reference..)
Expected Workflow

- Schedule a task during application start
- Trigger ExecuteTask()
- Run ScheduleTask()
- Repeat 2-3
Schedule Task Code
There are two important method,
- ExecuteTask() -> It will trigger when date time now = execute_time

- ScheduleTask() -> To schedule new task

Then we rewrite the code:
#SampleTask.cs public class SampleTask : ScheduledTask { public SampleTask() { this.Key = "MyKey"; } public override string TaskName { get { return this.GetType().FullName; } } public override void ExecuteTask() { // Code to Execute Task ScheduleTask(); } public static void ScheduleTask() { SchedulingManager schedulingManager = SchedulingManager.GetManager(); var executeTime = DateTime.Now.AddMinutes(5); var existingTask = schedulingManager.GetTaskData().FirstOrDefault(x => x.Key == this.Key); if (existingTask == null) { // Create a new scheduled task SampleTask newTask = new SampleTask() { ExecuteTime = executeTime }; schedulingManager.AddTask(newTask); } else { existingTask.ExecuteTime = executeTime; } SchedulingManager.RescheduleNextRun(); schedulingManager.SaveChanges(); } }
#Global.asax.cs protected void Application_Start(object sender, EventArgs e) { Bootstrapper.Bootstrapped += Bootstrapper_Bootstrapped; } private void Bootstrapper_Bootstrapped(object sender, EventArgs e) { HousekeepingTask.ScheduleTask(init: true, rescheduleNewTask: false); InterfaceTask.ScheduleTask(init: true, rescheduleNewTask: false); }
Schedule Task Table
dbo.sf_scheduled_tasks
Columns |
---|
task_name |
ky |
is_running |
execute_time |
schedule_data |
Conclusion
Then we try to put debug point at ScheduleTask() method and got conclusion below.
Steps
Then check ScheduleTask() method,
- First time scall ScheduleTask(), the existingTask is null
- ExecuteTask() triggered
- Call ScheduleTask(), existingTask is not null
- Set a new execute time –> existingTask.ExecuteTime = executeTime;
- call RescheduleNextRun() and SaveChange()
Findings:
- Notice during the 3) step, the existing task is_running is true.
- Notice right after 5) step, there will be two records in sf_scheduled_tasks table. one with is_running = true, one with is_running = false
- About 10 seconds later, the record with is_running = true is gone
Then we got the conclusion on the columns as below:
Column | Description |
---|---|
task_name | Name of class where implement the abstract class ScheduledTask |
ky | Task key |
is_running | True when task is running; False when task has yet to run |
execute_time | Task scheduled timing (next run) |
schedule_data | To store cron task config (not applicable in this investigate) |