-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathDurableController.cs
165 lines (144 loc) · 6.63 KB
/
DurableController.cs
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
namespace Microsoft.Azure.Functions.PowerShellWorker.Durable
{
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Management.Automation;
using WebJobs.Script.Grpc.Messages;
using PowerShellWorker.Utility;
using Microsoft.Azure.Functions.PowerShellWorker.DurableWorker;
using System;
using LogLevel = WebJobs.Script.Grpc.Messages.RpcLog.Types.Level;
/// <summary>
/// The main entry point for durable functions support.
/// </summary>
internal class DurableController
{
private readonly DurableFunctionInfo _durableFunctionInfo;
private readonly IPowerShellServices _powerShellServices;
private readonly IOrchestrationInvoker _orchestrationInvoker;
private OrchestrationBindingInfo _orchestrationBindingInfo;
private readonly ILogger _logger;
private bool isExternalDFSdkEnabled { get; } =
PowerShellWorkerConfiguration.GetBoolean(Utils.ExternalDurableSdkEnvVariable) ?? false;
public DurableController(
DurableFunctionInfo durableDurableFunctionInfo,
PowerShell pwsh,
ILogger logger)
: this(
durableDurableFunctionInfo,
new PowerShellServices(pwsh, logger),
new OrchestrationInvoker(),
logger)
{
}
internal DurableController(
DurableFunctionInfo durableDurableFunctionInfo,
IPowerShellServices powerShellServices,
IOrchestrationInvoker orchestrationInvoker,
ILogger logger)
{
_durableFunctionInfo = durableDurableFunctionInfo;
_powerShellServices = powerShellServices;
_orchestrationInvoker = orchestrationInvoker;
_logger = logger;
}
public string GetOrchestrationParameterName()
{
return _orchestrationBindingInfo?.ParameterName;
}
private void tryEnablingExternalSDK()
{
var isExternalSdkLoaded = _powerShellServices.isExternalDurableSdkLoaded();
if (isExternalDFSdkEnabled)
{
if (isExternalSdkLoaded)
{
// Enable external SDK only when customer has opted-in
_powerShellServices.EnableExternalDurableSDK();
}
else
{
// Customer attempted to enable external SDK but the module is not in the session. Default to built-in SDK.
_logger.Log(isUserOnlyLog: false, LogLevel.Error, string.Format(PowerShellWorkerStrings.ExternalSDKWasNotLoaded, Utils.ExternalDurableSdkName));
}
}
else if (isExternalSdkLoaded)
{
// External SDK is in the session, but customer did not explicitly enable it. Report the potential of runtime errors.
_logger.Log(isUserOnlyLog: false, LogLevel.Error, String.Format(PowerShellWorkerStrings.PotentialDurableSDKClash, Utils.ExternalDurableSdkName, Utils.ExternalDurableSdkEnvVariable));
}
}
public void InitializeBindings(IList<ParameterBinding> inputData, out bool hasExternalSDK)
{
this.tryEnablingExternalSDK();
// If the function is an durable client, then we set the DurableClient
// in the module context for the 'Start-DurableOrchestration' function to use.
if (_durableFunctionInfo.IsDurableClient)
{
var durableClient =
inputData.First(item => item.Name == _durableFunctionInfo.DurableClientBindingName)
.Data.ToObject();
_powerShellServices.SetDurableClient(durableClient);
}
else if (_durableFunctionInfo.IsOrchestrationFunction)
{
var numBindings = inputData.Count;
if (numBindings != 1)
{
/* Quote from https://docs.microsoft.com/en-us/azure/azure-functions/durable-functions-bindings:
*
* "Orchestrator functions should never use any input or output bindings other than the orchestration trigger binding.
* Doing so has the potential to cause problems with the Durable Task extension because those bindings may not obey the single-threading and I/O rules."
*
* Therefore, it's by design that input data contains only one item, which is the metadata of the orchestration context.
*/
var exceptionMessage = string.Format(PowerShellWorkerStrings.UnableToInitializeOrchestrator, numBindings);
throw new ArgumentException(exceptionMessage);
}
_orchestrationBindingInfo = _powerShellServices.SetOrchestrationContext(
inputData[0],
out IExternalOrchestrationInvoker externalInvoker);
_orchestrationInvoker.SetExternalInvoker(externalInvoker);
}
hasExternalSDK = _powerShellServices.HasExternalDurableSDK();
}
public void AfterFunctionInvocation()
{
_powerShellServices.ClearOrchestrationContext();
}
public bool TryGetInputBindingParameterValue(string bindingName, out object value)
{
if (_orchestrationInvoker != null
&& _orchestrationBindingInfo != null
&& string.CompareOrdinal(bindingName, _orchestrationBindingInfo.ParameterName) == 0)
{
value = _orchestrationBindingInfo.Context;
return true;
}
value = null;
return false;
}
public void AddPipelineOutputIfNecessary(Collection<object> pipelineItems, Hashtable result)
{
if (ShouldSuppressPipelineTraces())
{
var returnValue = FunctionReturnValueBuilder.CreateReturnValueFromFunctionOutput(pipelineItems);
result.Add(AzFunctionInfo.DollarReturn, returnValue);
}
}
public Hashtable InvokeOrchestrationFunction()
{
return _orchestrationInvoker.Invoke(_orchestrationBindingInfo, _powerShellServices);
}
public bool ShouldSuppressPipelineTraces()
{
return _durableFunctionInfo.Type == DurableFunctionType.ActivityFunction;
}
}
}