Skip to content

Custom polling strategy

Gurmit Teotia edited this page Dec 1, 2022 · 7 revisions

Guflow provides following two strategies for reading history events

ReadAllEvents: This is the default strategy. WorkflowHost retrieves all the history events from Amazon SWF whenever a decision task is scheduled and that is correct behaviour for the majority of workflows. Having all the history events gives you utmost flexibility during workflow execution. You can execute any workflow action/API when whole event history is available. Following three examples uses "ReadAllEvents" strategy:

//Example 1: 
using (var workflowHost = domain.Host(new Workflow[] { new OrderWorkflow() }))
{
   //By default WorkflowHost will read all the history events on default task list. You will not be configure anything.
   workflowHost.StartExecution();
   Console.WriteLine("Press a key to terminate");
   Console.ReadKey();
}

 //Example 2:
using (var workflowHost = domain.Host(new Workflow[] { new OrderWorkflow() }))
{ 
   //By default WorkflowHost will read all the history events on given task list- "priority-task-list". You will not need to be configure anything.
   workflowHost.StartExecution(new TaskList("priority-task-list"));
   Console.WriteLine("Press a key to terminate");
   Console.ReadKey();
}

//Example 3: 
using (var workflowHost = domain.Host(new Workflow[] { new OrderWorkflow() }))
{
   //Read all history events on "priority-task-list"
   var taskList = new TaskList("priority-task-list")
   taskList.ReadStrategy = TaskList.ReadAllEvents;
   workflowHost.StartExecution(taskList);
   Console.WriteLine("Press a key to terminate");
   Console.ReadKey();
}

ReadFirstPage: Read only the latest 1000 events from Amazon SWF. This can be useful when you do not need to access the whole execution history of workflow. This example shows one of its usage. You have following restriction when using this strategy:

  • You always can not read Workflow input. Workflow input is the stored in very first event- WorkflowStartedEvent of history. As soon as number of history events go above 1000 you will lose access to workflow input.
  • You can not traverse the history events. Some of actions/API that will result in traversing the history events are:
    • AllEvents
    • LastEvent
    • IsActive
    • Reschedule(e).UpTo(times:..)

Custom read strategy:

You can provide a custom strategy to read the history events as show in following example:

  var taskList = new TaskList("my-list");
  taskList.ReadStrategy = MyCustomStrategy
 ...
 private static async Task<WorkflowTask> MyCustomStrategy(Domain domain, TaskList taskList, 
                                 string pollingIdentity, CancellationToken token)
{
   var decisionTask = await domain.PollForDecisionTaskAsync(taskList, pollingIdentity , token);
   return WorkflowTask.Create(decisionTask);
}

Things to take care:

  • Return the history events in descending order (latest events first) from your method
  • You should be careful in calling the specific actions/API which traverses the history events, in case all history events are not returned from your strategy method.

Providing the custom strategy is an advance scenario in usage of the Guflow library and should be carefully used.

Clone this wiki locally