Skip to content

Feature Eternal Orchestrations (aka infinite loops)

Chris Gillum edited this page Jul 2, 2020 · 2 revisions

As mentioned in Writing Task Orchestrations, the framework replays the execution history to recreate program state for the user’s TaskOrchestration instance. This history is bounded by size so it is not possible to have TaskOrchestration classes with infinite loops.

Using the generation feature, users can "checkpoint" the orchestration instance and create a new one.

public class CronOrchestration : TaskOrchestration<string, int>
{
    public override async Task<string> RunTask(OrchestrationContext context, int intervalHours)
    {
        // bounded loop
        for (int i = 0; i < 10; i++)
        {
            await context.CreateTimer<object>(
                context.CurrentUtcDateTime.Add(TimeSpan.FromHours(intervalHours)), null);
            // TODO : do something interesting 
        }

        // create a new instance of self with the same input (or different if needed)
        context.ContinueAsNew(intervalHours);
        return null;
    }
}

In this snippet, the user is telling the framework to create a brand new instance of itself (i.e. a new generation or execution) and forwards the input it received as the input to the new instance. This orchestration can run indefinitely without running into the history size limitations.