Skip to content

Defining Tasks

rosaliafx edited this page Mar 26, 2015 · 16 revisions

Overview

Once you have defined the workflow, you can start definig the tasks, Rosalia.Core.Api.Workflow class has a bunch of registration methods that should be called in RegisterTasks method. In general the signature of registration methods looks like this:

Task(TASK_ID, TASK_OBJECT, BEHAVIORS);

where

Argument Type Description
TASK_ID String A string identifier of the task, used for external references and logging
TASK_OBJECT ITask, Action, Func or Linq-comprehension object An object representing task "body"
BEHAVIORS ITaskBahavior[] Optional array of task modifiers

Every overload of Task method returns a special TaskFuture object as a result. The meaning of this object will be explained below, for now let's just assume that task registration syntax should look like this:

var fooTask = Task(TASK_ID, TASK_OBJECT, BEHAVIORS);

Action Tasks

The simplest kind of task is a task that performs some operation and does not return any result. Rosalia allows to register such tasks as a simple Action:

protected override void RegisterTasks()
{
    var fooTask = Task(
        "fooTask", 
        () => {
            // do work or throw an exception here
        });
}

There is also an overload with TaskContext. Context is a special object that could be used for logging, getting environment info etc:

protected override void RegisterTasks()
{
    var fooTask = Task(
        "fooTask", 
        context => {
            context.Log.Info("Starting foo task...");
            // do work or throw an exception here
        });
}

It's also possible to create a class encapsulating task logic:

using Rosalia.Core.Tasks;

public class FooTask : AbstractTask 
{
    protected override ITaskResult<Nothing> SafeExecute(TaskContext context)
    {
        context.Log.Info("Starting foo task...");
        // do work or throw an exception here
        return Success;
    }
}

..and register an instance of that class:

protected override void RegisterTasks()
{
    var fooTask = Task(
        "fooTask", 
        new FooTask());
}

Result Tasks

Some build tasks need to return data as a result of execution. It could be version extracted from the source control, code quality counters, list of generated files or something like this. Such tasks could be registered as a Func:

protected override void RegisterTasks()
{
    var getVersionTask = Task(
        "getVersion", 
        context => {
            context.Log("Extracting version from source control");
            // ...
            return "1.0.42".AsTaskResult();
        });
}

...or using separate task class:

using Rosalia.Core.Tasks;

public class ExtractVersionTask : AbstractTask<string>
{
    protected override ITaskResult<string> SafeExecute(TaskContext context)
    {
        context.Log("Extracting version from source control");
        // ...
        return "1.0.42".AsTaskResult();
    }
}
protected override void RegisterTasks()
{
    var getVersionTask = Task(
        "getVersion", 
        new ExtractVersionTask());
}

Once the task is registred, it's result value could be consumed by other tasks.