Skip to content

First activity

Gurmit Teotia edited this page Sep 14, 2019 · 6 revisions

To create a activity in Guflow you need to:

  • Create a class and derive it from Guflow.Worker.Activity class
  • Decorate the activity class with ActivityDescriptionAttribute with required version property
  • Implement execution method in activity to carry out the work.

Following example creates the "OrderActivity" which will be successfully completed on execution:

[ActivityDescription("1.0")]
public class OrderActivity : Activity
{
     [ActivityMethod]
     public ActivityResponse Execute(string input)
     {
         return Complete(new {Status ="OK"});
     }
}

By convention Guflow uses the class's name as activity name while interacting with Amazon SWF. However you can override it providing a value for "Name" property of ActivityDescriptionAttribute. In above example Guflow will name above activity as "OrderActivity" while interacting with Amazon SWF.

ActivityDescription has many other properties which are required to be present when scheduling activity. However Guflow (and also Amazon SWF) provides you the flexibility to supply them at different stage- either during registration or when you are about to schedule the activity.

In above example we are returning the activity completed response immediately. But in real scenarios you will carry out some work and based on its outcome you will return the response to Amazon SWF.

From an activity you can return following responses:

  1. Complete: It will return the success response to Amazon SWF.
  2. Cancel: It will indicate that activity is cancelled.
  3. Fail: It will tell the Amazon SWF that activity has failed. By default activity covert any unhandled exception in "activity method" to failed response.
  4. Defer: It will not return any response to Amazon SWF but will help you to return from "activity execution method". This is useful in the case where you do not have response to return to Amazon SWF immediately or you rely on human intervention to return the response. Once the deferred response is returned then activity can not be cancelled from workflow. You can use following approach to complete/fail the activity for which you have already returned the Defer response:
[ActivityDescription("1.0")]
public class MyActivity : Activity
{
  [ActivityMethod]
  public ActivityResponse Execute(string taskToken, ActivityInput input)
  {
   //Save or persist the task token in database, config or cache ( or whatever suits your implementation)
   PersitedTokens.TaskToken = taskToken
   ....

   return Defer;
  }
}

Later on whenever you wish to complete/fail the activity then send the response to Amazon SWF using Guflow.Domain class:

var domain = ....
var completeResponse = new ActivityCompletedResponse(PersistedTokens.TaskToken, "result");
await domain.SentActivityResponse(completedResponse)

Clone this wiki locally