Sitefinity Schedule Task
sitefinity | 2020-09-20
Introduction
Scheduled tasks in Sitefinity CMS are components designed to execute predefined operations at a specific point in time.
They are typically used to run background processes, automate repetitive jobs, or schedule time-based logic without direct user interaction.
Expected Workflow
The following diagram outlines the expected lifecycle of a scheduled task in Sitefinity:
- A task is scheduled during application startup.
- The
ExecuteTask()
method is triggered when the execution time is reached. - Within
ExecuteTask()
, the task reschedules itself viaScheduleTask()
. - Steps 2–3 repeat at the defined interval.
Schedule Task Code
There are two core methods involved:
ExecuteTask()
→ Executes when the current time matches the scheduled execution time.ScheduleTask()
→ Schedules a new task for future execution.
Execute Task
Schedule Task
Below is the rewritten implementation:
Application Schedule Task Hook
#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();
}
}
Application Startup Hook
#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
After placing breakpoints within the ScheduleTask()
method, the following behavior was observed:
Execution Steps
- The first call to
ScheduleTask()
findsexistingTask
asnull
. ExecuteTask()
is triggered when the scheduled time is reached.- Inside
ExecuteTask()
,ScheduleTask()
is called again—this timeexistingTask
is not null. - A new execution time is assigned via
existingTask.ExecuteTime = executeTime
. RescheduleNextRun()
andSaveChanges()
are called to persist the update.
Observations
- At step 3, the
is_running
flag on the current task record becomestrue
. - Shortly after step 5, the database shows two records:
- One with
is_running = true
- One with
is_running = false
- One with
- Approximately 10 seconds later, the record with
is_running = true
is automatically removed.
This behavior helps clarify the meaning and lifecycle of each column in the scheduled task table.
Columns | 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) |