Startup Tasks Extension

The Startup Tasks extension will scan your application for classes that implement IStartupTask and will execute it's Run method.

Installation

The Startup Task extension is part of the Bootstrapper main assembly. You can install it via nuget over here.

Creating Startup Tasks

To create startup tasks you must declare classes that implements IStartupTask and implement the Run and Reset method. For example:
    public class TestStartupTask: IStartupTask
    {
        public void Run()
        {
            //do something important
        }
        public void Reset()
        {
            //undo something important
        }
    }

Using the extension

To use the extension simply write this code:
Bootstrapper.With.StartupTasks().Start();

Execution Order

You can specify the execution order of tasks by using the Task attribute or by using the UsingThisExecutionOrder method.

Execution Order with Attribute

You can use the PositionInSequence property of the Task attribute to specify the execution order of a task. For example
[Task(PositionInSequence = 2)] 
    public class TaskAlpha : IStartupTask 
    {
         public void Run() {}
         public void Reset() {}
    }
In the previous example, TaskAlpha will be the second task to execute when Bootstrapper.Start() is invoked. Conversely, TaskAlpha will be the second to last task to execute when Bootstrapper.Reset is invoked since the Reset method execute tasks in reverse order. Any task that doesn't specify a PositionInSequence will be executed after all taks with sequence position have executed. Similarly, these tasks will execute first when Reset is invoked.

Execution Order with Fluent interface

You can use the UsingThisExecutionOrder method to specify the execution order of tasks. For example.
Bootstrapper.With.StartupTasks()
        .UsingThisExecutionOrder(s => s
                        .First<TaskAlpha>()
                        .Then<TaskBeta>()
                        .Then<TaskOmega>()
                        .Then().TheRest())
        .Start()
In the example above, TaskAlpha will execute first, then TaskBeta, then TaskOmega and then any other task that is not listed. Conversely when invoking Reset, the unlisted tasks will execute first then TaskOmega, then TaskBeta and finally TaskAlpha. You can use TheRest in any part of the sequence as can be seen in the following example
Bootstrapper.With.StartupTasks()
        .UsingThisExecutionOrder(s => s
                        .First<TaskAlpha>()
                        .Then().TheRest()
                        .Then<TaskOmega>())
        .Start()
 
        //Also
Bootstrapper.With.StartupTasks()
        .UsingThisExecutionOrder(s => s
                        .First().TheRest()
                        .Then<TaskAlpha>()
                        .Then<TaskOmega>())
        .Start()

Start Delay

You can specify a delay for a task to start by using the Task Attribute or the DelayStartBy method.

Delay with Attribute

You can use the DelayStartBy property of the Task attribute to specify the time in milliseconds that should be waited before executing the task. For example:
[Task(DelayStartBy=100)]
    public class TaskAlpha : IStartupTask 
    {
         public void Run() {}
         public void Reset() {}
    }
In the previous example, when TaskAlpha's turn to execute happens the code will wait for 100 milliseconds and then will execute TaskAlpha. The DelayStartBy attribute is ignored when Reset is invoked.

Delay with Fluent Interface

You can use the DelayStartBy method to specify a task's start delay. For example
Bootstrapper.With.StartupTasks()
        .UsingThisExecutionOrder(s => s
                        .First<TaskAlpha>().DelayStartBy(250)
                        .Then<TaskBeta>().DelayStartBy(100).Milliseconds
                        .Then<TaskOmega>().DelayStartBy(2).Seconds
                        .Then().TheRest())
        .Start()
In the previous example the start of TaskAlpha will be delayed by 250 milliseconds, the start of TaskBeta by 100 milliseconds and the start of TaskOmega by 2 seconds. All other tasks will start as soon as it's turn occurs. When DelayStartBy is applied to TheRest, only the first task of the group of unlisted tasks will be delayed. The DelayStartBy method is ignored when Reset is invoked.

Parallel Task Execution

You can instruct tasks to execute in parallel by using the Task Attribute or the Group method.

Parallel Task Execution with Attribute

You can use the Group property of the Task attribute to specify parallel execution. Tasks with the same group number will execute sequentially. Tasks in different groups will execute in parallel. For example:
[Task(Group=2)]
    public class TaskAlpha : IStartupTask 
    {
         public void Run() {}
         public void Reset() {}
    }
In the previous example, TaskAlpha will execute in task group 2. Other tasks in group 2 will execute sequentially with Task Alpha. Tasks in group 1 will execute in parallel with Task Alpha. When Reset is invoked tasks are reseted sequentially using the groups in reverse order. Tasks that don't specify the Group will all default to Group 1.

Parallel Task Execution with Fluent Interface

You can use the Group method to specify Parallel Task Execution. Each Group must specify it's execution order. For example:
Bootstrapper.With.StartupTasks()
         .WithGroup(s => s
                    .First<TaskBeta>()
                    .Then<TaskAlpha>())
         .AndGroup(s => s
                    .First<TaskGamma>()
                    .Then().TheRest())
         .Start()
In the example above two groups of tasks with execute in parallel. In the first group TaskBeta will executed followed by TaskAlpha. In the second group TaskGamma will execute followed by any other unlisted task. When Reset is invoked tasks are reseted sequentially using the groups in reverse order. Unlisted tasks will be handled by TheRest method or will default to the first group if TheRest is not specified.

Last edited Mar 3, 2013 at 7:58 AM by luisbocaletti, version 15

Comments

No comments yet.