-
Notifications
You must be signed in to change notification settings - Fork 292
Expand file tree
/
Copy pathDurableFunctionExecutor.Orchestration.cs
More file actions
78 lines (65 loc) · 3.1 KB
/
Copy pathDurableFunctionExecutor.Orchestration.cs
File metadata and controls
78 lines (65 loc) · 3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
using System;
using System.Threading.Tasks;
using Google.Protobuf;
using Microsoft.DurableTask;
using Microsoft.DurableTask.Worker.Grpc;
using P = Microsoft.DurableTask.Protobuf;
namespace Microsoft.Azure.Functions.Worker.Extensions.DurableTask.Execution;
internal partial class DurableFunctionExecutor
{
// Must point to a PUBLIC method.
// Functions runtime will validate this, even though it is never called.
public static readonly string OrchestrationEntryPoint =
$"{typeof(DurableFunctionExecutor).FullName}.{nameof(Orchestration)}";
public void Orchestration()
{
throw new NotImplementedException(
"Do not call this method. It is a placeholder for orchestration function metadata.");
}
private async ValueTask RunOrchestrationAsync(FunctionContext context, BindingMetadata triggerBinding)
{
InputBindingData<object> triggerInputData = await context.BindInputAsync<object>(triggerBinding);
if (triggerInputData?.Value is not string encodedOrchestratorState)
{
throw new InvalidOperationException(
"Orchestration history state was either missing from the input or not a string value.");
}
await using IDisposableOrchestrator orchestrator = this.CreateOrchestrator(context, triggerInputData);
string orchestratorOutput = GrpcOrchestrationRunner.LoadAndRun(
encodedOrchestratorState, orchestrator, extendedSessionsCache, context.InstanceServices);
if (this.IsWorkerDraining)
{
orchestratorOutput = FlagOrchestratorDraining(orchestratorOutput);
}
// Send the encoded orchestrator output as the return value seen by the functions host extension
context.GetInvocationResult().Value = orchestratorOutput;
}
private static string FlagOrchestratorDraining(string encodedOrchestratorOutput)
{
byte[] responseBytes = Convert.FromBase64String(encodedOrchestratorOutput);
P.OrchestratorResponse response = P.OrchestratorResponse.Parser.ParseFrom(responseBytes);
response.IsWorkerDraining = true;
return Convert.ToBase64String(response.ToByteArray());
}
private IDisposableOrchestrator CreateOrchestrator(
FunctionContext context, InputBindingData<object> triggerInputData)
{
if (context.FunctionDefinition.EntryPoint == OrchestrationEntryPoint)
{
return this.CreateWrapper(context);
}
return new FunctionsOrchestrator(context, inner, triggerInputData);
}
private WrapperOrchestrator CreateWrapper(FunctionContext context)
{
if (!factory.TryCreateOrchestrator(
context.FunctionDefinition.Name, context.InstanceServices, out ITaskOrchestrator? orchestrator))
{
throw new InvalidOperationException(
$"No orchestrator with name '{context.FunctionDefinition.Name}' is registered.");
}
return new(orchestrator, context);
}
}