There are anyways to use WaitForExternalEvent in sub-orchestration?

27 Views Asked by At

I want to use WaitForExternalEvent in sub-orchestration. But it seems the same sub-orchestration can't be run twice. Main orchestration:

[FunctionName(FunctionNameConstants.ScenarioOrchestrator)]
public async Task<string> ScenarioOrchestrator(
    [OrchestrationTrigger] IDurableOrchestrationContext context, ILogger log)
{
    OnboardingAlwaysOnRequest onboardingProfile = context.GetInput<OnboardingAlwaysOnRequest>();

    await context.CallSubOrchestratorAsync<string>(FunctionNameConstants.ExternalApproval, onboardingProfile);

    // returns ["Hello Tokyo!", "Hello Seattle!", "Hello London!"]
    return "completed";
}

sub-orchestration:

[FunctionName(FunctionNameConstants.ExternalApproval)]
public async Task ExternalApprovalAsync(
    [OrchestrationTrigger] IDurableOrchestrationContext context,
    ILogger log)
{
    OnboardingAlwaysOnRequest profile = context.GetInput<OnboardingAlwaysOnRequest>();

    log.LogInformation($"Wait {profile.ProductProfile.Name} score card approval");
    DateTime expireTime = context.CurrentUtcDateTime.AddDays(1);
    using var timeoutCts = new CancellationTokenSource();
    Task durableTimeout = context.CreateTimer(expireTime, timeoutCts.Token);
    Task approved = context.WaitForExternalEvent<bool>(FunctionNameConstants.ScopeCardApproval);

    if (approved == await Task.WhenAny(approved, durableTimeout))
    {
        timeoutCts.Cancel();
        log.LogInformation($"{profile.ProductProfile.Name} score card approval succeed!");
    }
    else
    {
        log.LogInformation($"{profile.ProductProfile.Name} score card approval timeout!");
    }
}

logs:

[2024-02-01T11:54:12.298Z] Executing 'StartScenarioProvision' (Reason='This function was programmatically called via the host APIs.', Id=6755325c-eb36-4a3d-9c01-3a5ad0f75526)
[2024-02-01T11:54:12.404Z] Started product name = 'Loop', orchestration with ID = 'a5acc504-1c9d-4b6c-84ed-4cfda6b2fd0e'.
[2024-02-01T11:54:13.043Z] Executed 'StartScenarioProvision' (Succeeded, Id=6755325c-eb36-4a3d-9c01-3a5ad0f75526, Duration=685ms)
[2024-02-01T11:54:13.500Z] Executing 'ScenarioOrchestrator' (Reason='(null)', Id=127fe1e7-a7a2-46d0-9633-3d176b8ebc30)
[2024-02-01T11:54:13.542Z] Executed 'ScenarioOrchestrator' (Succeeded, Id=127fe1e7-a7a2-46d0-9633-3d176b8ebc30, Duration=47ms)
[2024-02-01T11:54:13.828Z] Executing 'ExternalApproval' (Reason='(null)', Id=2d5153d1-6ef5-4120-a5fa-de53d2a82713)
[2024-02-01T11:54:13.871Z] Wait Loop score card approval
[2024-02-01T11:54:14.058Z] Executed 'ExternalApproval' (Succeeded, Id=2d5153d1-6ef5-4120-a5fa-de53d2a82713, Duration=230ms)
[2024-02-01T11:54:23.644Z] Executing 'StartScopeCardApproval' (Reason='This function was programmatically called via the host APIs.', Id=f78c4f78-b970-452b-b298-80229cd4294e)
[2024-02-01T11:54:23.850Z] Executed 'StartScopeCardApproval' (Succeeded, Id=f78c4f78-b970-452b-b298-80229cd4294e, Duration=206ms)
[2024-02-01T11:54:24.021Z] Executing 'ScenarioOrchestrator' (Reason='(null)', Id=fa4240d0-d0f2-4318-a14f-213a69ad5f0c)
[2024-02-01T11:54:24.030Z] Executed 'ScenarioOrchestrator' (Succeeded, Id=fa4240d0-d0f2-4318-a14f-213a69ad5f0c, Duration=9ms)

Can use WaitForExternalEvent in sub-orchestration.

1

There are 1 best solutions below

0
Jie Yan On BEST ANSWER

I should use the instance id of sub-orchestration rather than parent's for http trigger ExternalApproval.