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:

workflow

  1. A task is scheduled during application startup.
  2. The ExecuteTask() method is triggered when the execution time is reached.
  3. Within ExecuteTask(), the task reschedules itself via ScheduleTask().
  4. 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

code1

Schedule Task

code2

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

  1. The first call to ScheduleTask() finds existingTask as null.
  2. ExecuteTask() is triggered when the scheduled time is reached.
  3. Inside ExecuteTask(), ScheduleTask() is called again—this time existingTask is not null.
  4. A new execution time is assigned via existingTask.ExecuteTime = executeTime.
  5. RescheduleNextRun() and SaveChanges() are called to persist the update.

Observations

  • At step 3, the is_running flag on the current task record becomes true.
  • Shortly after step 5, the database shows two records:
    • One with is_running = true
    • One with is_running = false
  • 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.

ColumnsDescription
task_nameName of class where implement the abstract class ScheduledTask
kyTask key
is_runningTrue when task is running; False when task has yet to run
execute_timeTask scheduled timing (next run)
schedule_dataTo store cron task config (not applicable in this investigate)

📚 References