能否在Durable Functions子编排中使用WaitForExternalEvent?
子编排中使用WaitForExternalEvent的验证结果
原本以为在子编排(sub-orchestration)中使用WaitForExternalEvent后,同一个子编排无法运行两次,实际验证后确认可以在子编排中使用WaitForExternalEvent。
主编排代码
[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"; }
子编排代码
[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!"); } }
运行日志
[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)
结论
可以在子编排中使用WaitForExternalEvent。
内容的提问来源于stack exchange,提问作者Jie Yan
相关产品推荐
相关产品推荐

