Skip to content

Schedule timers

Gurmit Teotia edited this page Jan 12, 2018 · 7 revisions

In a workflow you can explicitly schedule the timer or you can implicitly use in workflow actions. In a workflow you can explicitly schedule a timer like below:

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

Just like activity, timer will be scheduled in Amazon SWF and it does not rely on your local system time.

In workflow you can also timer implicitly with custom action. Following example clarify it further:

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"));
   }
}
Clone this wiki locally