Skip to content

Schedule activities

Gurmit Teotia edited this page Jun 17, 2019 · 15 revisions

Workflow is the place where you-

  • Instruct Amazon SWF to schedule activities
  • Write coordination logic to schedule the activities
  • Write logic to collaborate with other workflows
  • Write logic to handle errors in activities.

Workflow logic should be pretty quick in processing the Amazon SWF events and returning the decision back to Amazon SWF. While in activities 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:

[WorkflowDescription("1.0")]
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 example workflow will extract out the activity information from ActivityDescriptionAttribute of DownloadActivity and schedule it with Amazon SWF.

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");
        ....
    }
}

Activity input:

By default workflow will pass its input to each activity however you have full control on passing the input to activity. You can compose an activity input either from workflow input or from result of another activity or in any other way you like. Following example show various ways to create activity input

[WorkflowDescription("1.0")]
public class TranscodeWorkflow : Workflow
{
 public TranscodeWorkflow()
 {
   ScheduleActivity<DownloadActivity>().WithInput(_=>new {Location= Input.Bucket});
   
   ScheduleActivity<TranscodeActivity>().AfterActivity<DownloadActivity>()
        .WithInput(a=>new {InputFile = a.Parent().Result().DownloadedPath});
 }
}

Note: Please do not pass huge information in activity input, Amazon SWF has the limitation of 1 MB in request size.

Error handling:

By default workflow will be failed when an activity has failed/timedout however you can configure it to take custom action as shown in following example:

[WorkflowDescription("1.0")]
public class TranscodeWorkflow : Workflow
{
 public TranscodeWorkflow()
 {
   //DownloadActivity will be rescheduled after 2 second timeout, everytime on failure.
   ScheduleActivity<DownloadActivity>().WithInput(_=>new {Location= Input.Bucket})
      .OnFailure(a=>Reschedule(a).After(TimeSpan.FromSeconds(2));
   
   //TranscodeActivity will be rescheduled immediately on timedout.
   ScheduleActivity<TranscodeActivity>().AfterActivity<DownloadActivity>()
        .WithInput(a=>new {InputFile = a.Parent().Result().DownloadedPath})
        .OnTimeout(Reschedule);
 }
}

Scheduling same activity multiple times:

Both ScheduleActivity methods allow you to pass additional argument - "positionalName", which you can use to schedule same activity at different places in a workflow, as shown in following example:

[WorkflowDescription("1.0")]
public class TranscodeWorkflow : Workflow
{
 public TranscodeWorkflow()
 {
    ScheduleActivity<DownloadActivity>().WithInput(_=>new {Location= Input.Bucket});
      
   //TranscodeActivity will be scheduled two times
   ScheduleActivity<TranscodeActivity>("MPEGFormat").AfterActivity<DownloadActivity>()
        .WithInput(a=>new {Format = "MPEG"});

   ScheduleActivity<TranscodeActivity>("WAVFormat").AfterActivity<DownloadActivity>()
        .WithInput(a=>new {Format = "WAV"});
  
 }
}

Not all the activity APIs are documented here but they all have inline document/help. All you need is the intellisense support in your IDE to read API's help.

Clone this wiki locally