Skip to content

Activity method

Gurmit Teotia edited this page Jan 2, 2018 · 13 revisions

"Activity method" is a method defined in your activity class which Guflow will invoke when it receive a new worker task from Amazon SWF. To define a "Activity method" you need to mark a method with "ActivityMethodAttribute".

You can write "Activity method" in following different ways to suite you style and requirements:

Sync/Async

  • Asynchronous: Activity method supports async/await programming model. If you are doing some I/O work then your activity can benifit from asynchronous programming approach.
[ActivityDescription("1.0")]
public class OrderActivity : Activity
{
   [ActivityMethod]
   public async Task<ActivityResponse> ExecuteAsync(ActivityArgs args)
   {
     //.....do your work
     await io.work();
     return Complete("result");
   }
}
  • Synchronous: If you think that your activity is not going to benefit from asynchronous programming model then you can write the "Activity method" using synchronous style, as shown below:
[ActivityDescription("1.0")]
public class OrderActivity : Activity
{
   [ActivityMethod]
   public ActivityResponse ExecuteAsync(ActivityArgs args)
   {
     //.....do your work
     return Complete("result");
   }
}

Return types:

  • void/async Task return type: If you do not have the activity response to return to Amazon SWF immediately then the return type of "Activity method" can be either "void" or "async Task". It is equal to returning "Defer" response. Following two examples have same meaning:
[ActivityDescription("1.0")]
public class ShipOrderActivity : Activity
{
   [ActivityMethod]
   public async Task ExecuteAsync(ActivityArgs args)
   {
     //.....do your work
     await io.LogRequest(args);
   }
}
//OR
[ActivityDescription("1.0")]
public class ShipOrderActivity : Activity
{
   [ActivityMethod]
   public async Task<ActivityResponse> ExecuteAsync(ActivityArgs args)
   {
     //.....do your work
     await io.LogRequest(args);
     return Defer;
   }
}
  • Custom return type: Activity execute method can have a primitive type of custome as return type. Guflow will convert primitive type to string and serialize the custom type to JSON format and return the outcome as completed response to Amazon SWF. Some of the following examples shows various return type possibilities:
//This example returns a custom type- OrderResponse. It will be deserialized to JSON when sending the response to Amazon SWF.
[ActivityDescription("1.0")]
public class OrderActivity : Activity
{
   [ActivityMethod]
   public async Task<OrderResponse> ExecuteAsync(ActivityArgs args)
   {
     //.....do your work

     return new OrderResponse(){...};
   }
}

//This example returns a int. It will be converted to string and returned to Amazon SWF.
[ActivityDescription("1.0")]
public class OrderActivity : Activity
{
   [ActivityMethod]
   public async Task<int> ExecuteAsync(ActivityArgs args)
   {
     //.....do your work

     return 10;
   }
}
  • ActivityResponse return type: Activity method can also has "ActivityResponse" as return type. This gives you more control on returning either of allowed responses to Amazon SWf.
[ActivityDescription("1.0")]
public class OrderActivity : Activity
{
   [ActivityMethod]
   public async Task<ActivityResponse> ExecuteAsync(ActivityArgs args)
   {
     //.....do your work
     if(success) return Complete("raw result");
     if(failed) return Fail("reason", "detail");
     if(cancelled) return Cancel("detail")
     if(doNotHaveResponse) return Defer;
     throw new Exception("I do not know what to do");
   }
}

Note: If an unhandled exception escape the activity method then it will be processed as below:

  • If the exception is OperationCanceledException then cancelled response will be send to Amazon SWF. Please look at activity cancellation for more detail.
  • For any other type of exception, fail response will be send to Amazon SWF. However you can configure activity to not to handle the exception and in this case unescaped exception will be forwarded to ActivitiesHost's generic handler and by default it is ignored. Please look at exception handling section to understand how exceptions are propagated in Guflow. Following example configure the activity to not convert the exception in to fail response:
[ActivityDescription("1.0")]
public class ShipOrderActivity : Activity
{
  public ShipOrderActivity()
  {
    FailOnException = false;
  }
   [ActivityMethod]
   public async Task<ActivityResponse> ExecuteAsync(ActivityArgs args)
   {
     //.....do your work
     await io.LogRequest(args);
     return Defer;
   }
}

Arguments:

  • Pass ActivityArgs to access all the information in raw form.
  • Pass individual properties of ActivityArgs: In this case Guflow will try to populate the argument from the same name property for ActivityArg and if required if can convert/deserialized the property value to argument value. Following example clarify it further:
[ActivityDescription("1.0")]
public class TranscodeActivity : Activity
{
  [ActivityMethod]
  public async Task<ActivityResponse> Execute(string workflowId, Input input)
  {
    return Complete("done");
  }
  public class Input
  {
    public string Format;
    public string FilePath;
  }
}
Clone this wiki locally