Skip to content

Schedule activities

Gurmit Teotia edited this page Nov 15, 2017 · 15 revisions

Note: As of writing this document Guflow allows you to schedule activities and timer in workflow. Supports of child workflows and lambda are coming soon.

Workflows are the place where you write coordination logic and in activity you carry out actual work. An activity can take anywhere between few milliseconds to days to complete its work. You can learn more about activity hosting and implementation in activity section.

In Guflow you can schedule the activity as shown below:

public class TranscodeWorkflow : Workflow
{
    public TranscodeWorkflow()
    {
        ScheduleActivity<DownloadActivity>();
    }
}

Note: For above code to compile you need to create the DownloadAcitivity class. Please refer to Activity section on how to create activities in Guflow. For workflow to schedule DownloadActivity it must be registered with Amazon SWF. In above version workflow will extract out the activity information from DownloadActivity and schedule it with Amazon SWF when started.

Above API is good if you have access to activity class however if for some reason you don't have access to activity class or your activity is implemented in other framework/language then you can directly provide scheduling information, as shown below:

public class TranscodeWorkflow : Workflow
{
    public TranscodeWorkflow()
    {
        ScheduleActivity(name:"DownloadActivity", version: "1.0");
    }
}

Guflow gives you full control in managing input and events of scheduling activities. Some of following examples will help you to understand the usage of scheduling APIs

//Following example will schedule the DownloadActivity with custom input.
public class TranscodeWorkflow : Workflow
{
    public TranscodeWorkflow()
    {
        ScheduleActivity<DownloadActivity>().WithInput(a=>new {Location ="S3Bucket"});
    }
}
//Following example will reschedule the activity on failure.
public class TranscodeWorkflow : Workflow
{
    public TranscodeWorkflow()
    {
        ScheduleActivity<DownloadActivity>().WithInput(a=>new {Location ="S3Bucket"})
                                            .OnFailure(Reschedule);
    }
}

Scheduling same activity multiple times:

Both ScheduleActivity methods allow you to pass additional argument - "positional name", which you can use to schedule same activity at different places in a workflow.

Dependency graph:

You can schedule multiple activities or timer in sequence or in parallel in a workflow. When a workflow starts it will schedule the startup items (activities or timers). Startup items are those which does not have any parent items and there can be many startup activities or timer. Once a activity or timer is completed it will schedule its children.

In following example DownloadAcivity will be the startup activities. Once the DownloadActivity is completed then TranscodeWorkflow schedule the TrancodeActivity.

public class TranscodeWorkflow : Workflow
{
    public TranscodeWorkflow()
    {
        //DownloadActivity is the startup activity, which will be scheduled when workflow is started.
        ScheduleActivity<DownloadActivity>().WithInput(a=>new {Location ="S3Bucket"})
                                            .OnFailure(Reschedule);
        //Let us assume for simiplicity that TranscodeActivity will run on same machine where DownloadActivity has run.
        //While scheduling TranscodeActivity, it read the result of its parent and pass it is input.
        ScheduleActivity<TranscodeActivity>().After<DownloadActivity>().WithInput(a=>a.Parent().Result().DownloadedPath);
    }
}
Clone this wiki locally