Skip to content

Feature Sub orchestrations (aka nested orchestrations)

Affan Dar edited this page Jan 30, 2015 · 1 revision

Orchestrations can also start and wait on other orchestrations using the sub orchestration feature. This is useful for cases where you have a library of orchestrations and you want to build a larger orchestration around these.

public class PeriodicBillingJob : TaskOrchestration<string, int>
{
    // hardcoded list of apps to run billing orchestrations on
    static readonly string[] ApplicationList = new string[] { "app1", "app2" };

    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);

            List<Task> billingTasks = new List<Task>();

            foreach (string appName in PeriodicBillingJob.ApplicationList)
            {
                billingTasks.Add(
                    context.CreateSubOrchestrationInstance<bool>(typeof (BillingOrchestration), appName));
            }
            await Task.WhenAll(billingTasks);
        }

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

// a reusable orchestration which can either be triggered directly by the admin or via 
// some master recurring periodic billing orchestration
public class BillingOrchestration : TaskOrchestration<bool, string>
{
    public override async Task<bool> RunTask(OrchestrationContext context, string applicationName)
    {
        // TODO : process billing information for 'applicationName'
        return true;
    }
}