Skip to content

Sample Child Workflows

Akash Kava edited this page Nov 10, 2021 · 1 revision

To reuse workflow in another, you can use ExecuteAsync method.

Child

public class ChildWorkflow : Workflow<ChildWorkflow, int, int>
{
    public override async Task<int> RunAsync(int input)
    {
        var at = TimeSpan.FromSeconds(1);
        input = await Add(at, input);
        input = await Add(at, input);
        input = await Add(at, input);
        return input;
    }

    [Activity(true)]
    public virtual async Task<int> Add(
        [Schedule] TimeSpan at, int i)
    {
        await Task.Delay(100);
        return i + i;
    }
}

Parent

public class ParentWorkflow : Workflow<ParentWorkflow, int, int>
{
    public async override Task<int> RunAsync(int input)
    {
        // we will call `ExecuteAsync` method of `ChildWorkflow` here
        // Here execution of ParentWorkflow will be suspended till
        // child workflow is executed successfully.
        var result = await ChildWorkflow.ExecuteAsync(this, input);
    }
}

Parellel Child Workflow Execution

Never use Task.WhenAll in Eternity Workflow.

The correct way to execute many children workflow is to use this.WhenAll method, which will take care of ActivitySuspendedException correctly.

public class ParentWorkflow : Workflow<ParentWorkflow, int, int>
{
    public async override Task<int> RunAsync(int input)
    {
        int n = new int [1,2,3,4,5,6,7];
        var results = await this.WhenAll(
            n.Select(
               x => ChildWorkflow.ExecuteAsync(this, x)));
    }
}
Clone this wiki locally