Skip to content

Schedule timers

Gurmit Teotia edited this page Jan 28, 2019 · 7 revisions

In a workflow, timers can be part of workflow branches as shown in following example:

public class TranscodeWorkflow : Workflow
{
  public TranscodeWorkflow()
  {
     ScheduleTimer(name:"DelayBeforeDownload").FireAfter(TimeSpan.FromSeconds(2));
     ScheduleActivity<DownloadActivity>().AfterTimer("DelayBeforeDownload");
  }
}

Just like activities, timer are scheduled in Amazon SWF and they do not rely on local system time.

In a workflow, timers are also implicitly used with custom action as shown in following example:

public class TranscodeWorkflow : Workflow
{
   public TranscodeWorkflow()
   {
      //In following example workflow will schedule the timer on failure and when timer is fired after 4 seconds it will 
      // reschedule the activity
      ScheduleActivity<DownloadActivity>().OnFailure(e=>Reschedule(e).After(TimeSpan.FromSeconds(4));
   }
}

ScheduleTimer API will let you react different based on conditions. e.g. when the timer is fired it will schedule its child items but you can change it to behave differently.

public class TranscodeWorkflow : Workflow
{
  public TranscodeWorkflow()
  {
     ScheduleTimer(name:"DelayBeforeDownload").FireAfter(TimeSpan.FromSeconds(2))
              .OnFired(a=>CompleteWorkflow("result"));
   }
}

Reset/Reschedule:

You can reset or reschedule the timer as show in following example:

public class OrderWorkflow : Workflow
{
   public OrderWorkflow()
   {
      ScheduleTimer("GracePeriod").FireAfter(TimeSpan.FromMinutes(10));
      ScheduleLambda("ShipOrder").AfterTimer("GracePeriod");
   }

   [SignalEvent]
   public WorkflowAction ResetGracePeriod() =>
       Timer("GracePeriod").IsActive ? Timer("GracePeriod").Reset() : Ignore;

   [SignalEvent]
   public WorkflowAction RescheduleGracePeriod() =>
       Timer("GracePeriod").IsActive ? Timer("GracePeriod").Reset(TimeSpan.FromMinutes(10)) : Ignore;
}

For a timer to be Reset/Reschedule it should be already be active otherwise an exception will be thrown.

Clone this wiki locally