Sitefinity – Investigate Schedule Task

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

  1. Schedule a task during application start
  2. Trigger ExecuteTask()
  3. Run ScheduleTask()
  4. 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,

  1. First time scall ScheduleTask(), the existingTask is null
  2. ExecuteTask() triggered
  3. Call ScheduleTask(), existingTask is not null
  4. Set a new execute time –> existingTask.ExecuteTime = executeTime;
  5. 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:

ColumnDescription
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

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 )

Google photo

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

Twitter picture

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

Facebook photo

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

Connecting to %s