|
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
| 2 | +// Licensed under the MIT License. See License.txt in the project root for license information. |
| 3 | + |
| 4 | +using Microsoft.Azure.Functions.Worker; |
| 5 | +using Microsoft.Azure.Functions.Worker.Http; |
| 6 | +using Microsoft.DurableTask; |
| 7 | +using Microsoft.DurableTask.Client; |
| 8 | +using Microsoft.Extensions.Logging; |
| 9 | + |
| 10 | +namespace Microsoft.Azure.Durable.Tests.E2E; |
| 11 | + |
| 12 | +public static class VersionedOrchestration |
| 13 | +{ |
| 14 | + [Function(nameof(VersionedOrchestration))] |
| 15 | + public static async Task<string> RunOrchestrator( |
| 16 | + [OrchestrationTrigger] TaskOrchestrationContext context) |
| 17 | + { |
| 18 | + ILogger logger = context.CreateReplaySafeLogger(nameof(VersionedOrchestration)); |
| 19 | + logger.LogInformation($"Versioned orchestration! Version: '{context.Version}'"); |
| 20 | + |
| 21 | + return await context.CallActivityAsync<string>(nameof(SayVersion), context.Version); |
| 22 | + } |
| 23 | + |
| 24 | + [Function(nameof(RunWithSubOrchestrator))] |
| 25 | + public static async Task<string> RunWithSubOrchestrator( |
| 26 | + [OrchestrationTrigger] TaskOrchestrationContext context, |
| 27 | + string? subVersion) |
| 28 | + { |
| 29 | + ILogger logger = context.CreateReplaySafeLogger(nameof(VersionedOrchestration)); |
| 30 | + logger.LogInformation($"Versioned orchestration! Version: '{context.Version}' Sub Version: '{subVersion}'"); |
| 31 | + |
| 32 | + string subOrchestrationResponse; |
| 33 | + if (subVersion == null) |
| 34 | + { |
| 35 | + subOrchestrationResponse = await context.CallSubOrchestratorAsync<string>(nameof(VersionedSubOrchestration)); |
| 36 | + } |
| 37 | + else |
| 38 | + { |
| 39 | + subOrchestrationResponse = await context.CallSubOrchestratorAsync<string>(nameof(VersionedSubOrchestration), new SubOrchestrationOptions |
| 40 | + { |
| 41 | + Version = subVersion, |
| 42 | + }); |
| 43 | + } |
| 44 | + return $"Parent Version: '{context.Version}' | Sub {subOrchestrationResponse}"; |
| 45 | + } |
| 46 | + |
| 47 | + [Function(nameof(VersionedSubOrchestration))] |
| 48 | + public static async Task<string> VersionedSubOrchestration([OrchestrationTrigger] TaskOrchestrationContext context) |
| 49 | + { |
| 50 | + ILogger logger = context.CreateReplaySafeLogger(nameof(VersionedOrchestration)); |
| 51 | + logger.LogInformation($"Versioned sub-orchestration! Version: '{context.Version}'"); |
| 52 | + |
| 53 | + return await context.CallActivityAsync<string>(nameof(SayVersion), context.Version); |
| 54 | + } |
| 55 | + |
| 56 | + [Function(nameof(SayVersion))] |
| 57 | + public static string SayVersion([ActivityTrigger] string version, FunctionContext executionContext) |
| 58 | + { |
| 59 | + ILogger logger = executionContext.GetLogger("SayVersion"); |
| 60 | + logger.LogInformation("Activity running with version: {name}.", version); |
| 61 | + return $"Version: '{version}'"; |
| 62 | + } |
| 63 | + |
| 64 | + [Function("OrchestrationVersion_HttpStart")] |
| 65 | + public static async Task<HttpResponseData> HttpStart( |
| 66 | + [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequestData req, |
| 67 | + [DurableClient] DurableTaskClient client, |
| 68 | + FunctionContext executionContext, |
| 69 | + string? version) |
| 70 | + { |
| 71 | + ILogger logger = executionContext.GetLogger("VersionedOrchestration_HttpStart"); |
| 72 | + |
| 73 | + // Function input comes from the request content. |
| 74 | + string instanceId; |
| 75 | + if (version != null) |
| 76 | + { |
| 77 | + instanceId = await client.ScheduleNewOrchestrationInstanceAsync(nameof(VersionedOrchestration), new StartOrchestrationOptions |
| 78 | + { |
| 79 | + Version = version, |
| 80 | + }); |
| 81 | + } |
| 82 | + else |
| 83 | + { |
| 84 | + instanceId = await client.ScheduleNewOrchestrationInstanceAsync(nameof(VersionedOrchestration)); |
| 85 | + } |
| 86 | + |
| 87 | + logger.LogInformation("Started orchestration with ID = '{instanceId}' and Version = '{version}'.", instanceId, version); |
| 88 | + |
| 89 | + // Returns an HTTP 202 response with an instance management payload. |
| 90 | + // See https://learn.microsoft.com/azure/azure-functions/durable/durable-functions-http-api#start-orchestration |
| 91 | + return await client.CreateCheckStatusResponseAsync(req, instanceId); |
| 92 | + } |
| 93 | + |
| 94 | + [Function("OrchestrationSubVersion_HttpStart")] |
| 95 | + public static async Task<HttpResponseData> HttpSubStart( |
| 96 | + [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequestData req, |
| 97 | + [DurableClient] DurableTaskClient client, |
| 98 | + FunctionContext executionContext, |
| 99 | + string? subOrchestrationVersion) |
| 100 | + { |
| 101 | + ILogger logger = executionContext.GetLogger("OrchestrationSubVersion_HttpStart"); |
| 102 | + |
| 103 | + // Function input comes from the request content. |
| 104 | + string instanceId; |
| 105 | + if (subOrchestrationVersion != null) |
| 106 | + { |
| 107 | + instanceId = await client.ScheduleNewOrchestrationInstanceAsync(nameof(RunWithSubOrchestrator), input: subOrchestrationVersion); |
| 108 | + } |
| 109 | + else |
| 110 | + { |
| 111 | + instanceId = await client.ScheduleNewOrchestrationInstanceAsync(nameof(RunWithSubOrchestrator)); |
| 112 | + } |
| 113 | + |
| 114 | + logger.LogInformation("Started orchestration with ID = '{instanceId}' and Version = '{subOrchestrationVersion}'.", instanceId, subOrchestrationVersion); |
| 115 | + |
| 116 | + // Returns an HTTP 202 response with an instance management payload. |
| 117 | + // See https://learn.microsoft.com/azure/azure-functions/durable/durable-functions-http-api#start-orchestration |
| 118 | + return await client.CreateCheckStatusResponseAsync(req, instanceId); |
| 119 | + } |
| 120 | +} |
0 commit comments