|
6 | 6 | namespace Microsoft.Azure.Functions.PowerShellWorker.Durable
|
7 | 7 | {
|
8 | 8 | using System;
|
| 9 | + using System.Collections.ObjectModel; |
| 10 | + using System.Linq; |
9 | 11 | using System.Management.Automation;
|
10 |
| - using PowerShell; |
| 12 | + using Microsoft.Azure.Functions.PowerShellWorker.Utility; |
| 13 | + using Microsoft.Azure.WebJobs.Script.Grpc.Messages; |
| 14 | + using Newtonsoft.Json; |
11 | 15 |
|
12 | 16 | internal class PowerShellServices : IPowerShellServices
|
13 | 17 | {
|
14 |
| - private const string SetFunctionInvocationContextCommand = |
15 |
| - "Microsoft.Azure.Functions.PowerShellWorker\\Set-FunctionInvocationContext"; |
| 18 | + private readonly string SetFunctionInvocationContextCommand; |
| 19 | + private const string ExternalDurableSDKName = "DurableSDK"; |
| 20 | + private const string InternalDurableSDKName = "Microsoft.Azure.Functions.PowerShellWorker"; |
16 | 21 |
|
17 | 22 | private readonly PowerShell _pwsh;
|
18 |
| - private bool _hasSetOrchestrationContext = false; |
| 23 | + private bool _hasInitializedDurableFunction = false; |
| 24 | + private readonly bool _useExternalDurableSDK = false; |
19 | 25 |
|
20 | 26 | public PowerShellServices(PowerShell pwsh)
|
21 | 27 | {
|
| 28 | + //This logic will be commented out until the external SDK is published on the PS Gallery |
| 29 | + |
| 30 | + // We attempt to import the external SDK upon construction of the PowerShellServices object. |
| 31 | + // We maintain the boolean member _useExternalDurableSDK in this object rather than |
| 32 | + // DurableController because the expected input and functionality of SetFunctionInvocationContextCommand |
| 33 | + // may differ between the internal and external implementations. |
| 34 | + |
| 35 | + try |
| 36 | + { |
| 37 | + pwsh.AddCommand(Utils.ImportModuleCmdletInfo) |
| 38 | + .AddParameter("Name", ExternalDurableSDKName) |
| 39 | + .AddParameter("ErrorAction", ActionPreference.Stop) |
| 40 | + .InvokeAndClearCommands(); |
| 41 | + _useExternalDurableSDK = true; |
| 42 | + } |
| 43 | + catch (Exception e) |
| 44 | + { |
| 45 | + // Check to see if ExternalDurableSDK is among the modules imported or |
| 46 | + // available to be imported: if it is, then something went wrong with |
| 47 | + // the Import-Module statement and we should throw an Exception. |
| 48 | + // Otherwise, we use the InternalDurableSDK |
| 49 | + var availableModules = pwsh.AddCommand(Utils.GetModuleCmdletInfo) |
| 50 | + .AddParameter("Name", ExternalDurableSDKName) |
| 51 | + .InvokeAndClearCommands<PSModuleInfo>(); |
| 52 | + if (availableModules.Count() > 0) |
| 53 | + { |
| 54 | + // TODO: evaluate if there is a better error message or exception type to be throwing. |
| 55 | + // Ideally, this should never happen. |
| 56 | + throw new InvalidOperationException("The external Durable SDK was detected, but unable to be imported.", e); |
| 57 | + } |
| 58 | + _useExternalDurableSDK = false; |
| 59 | + } |
| 60 | + //_useExternalDurableSDK = false; |
| 61 | + |
| 62 | + if (_useExternalDurableSDK) |
| 63 | + { |
| 64 | + SetFunctionInvocationContextCommand = $"{ExternalDurableSDKName}\\Set-FunctionInvocationContext"; |
| 65 | + } |
| 66 | + else |
| 67 | + { |
| 68 | + SetFunctionInvocationContextCommand = $"{InternalDurableSDKName}\\Set-FunctionInvocationContext"; |
| 69 | + } |
22 | 70 | _pwsh = pwsh;
|
23 | 71 | }
|
24 | 72 |
|
| 73 | + public bool UseExternalDurableSDK() |
| 74 | + { |
| 75 | + return _useExternalDurableSDK; |
| 76 | + } |
| 77 | + |
| 78 | + public PowerShell GetPowerShell() |
| 79 | + { |
| 80 | + return this._pwsh; |
| 81 | + } |
| 82 | + |
25 | 83 | public void SetDurableClient(object durableClient)
|
26 | 84 | {
|
27 | 85 | _pwsh.AddCommand(SetFunctionInvocationContextCommand)
|
28 | 86 | .AddParameter("DurableClient", durableClient)
|
29 | 87 | .InvokeAndClearCommands();
|
30 |
| - |
31 |
| - _hasSetOrchestrationContext = true; |
| 88 | + _hasInitializedDurableFunction = true; |
32 | 89 | }
|
33 | 90 |
|
34 |
| - public void SetOrchestrationContext(OrchestrationContext orchestrationContext) |
| 91 | + public OrchestrationBindingInfo SetOrchestrationContext( |
| 92 | + ParameterBinding context, |
| 93 | + out IExternalInvoker externalInvoker) |
35 | 94 | {
|
36 |
| - _pwsh.AddCommand(SetFunctionInvocationContextCommand) |
37 |
| - .AddParameter("OrchestrationContext", orchestrationContext) |
38 |
| - .InvokeAndClearCommands(); |
| 95 | + externalInvoker = null; |
| 96 | + OrchestrationBindingInfo orchestrationBindingInfo = new OrchestrationBindingInfo( |
| 97 | + context.Name, |
| 98 | + JsonConvert.DeserializeObject<OrchestrationContext>(context.Data.String)); |
| 99 | + |
| 100 | + if (_useExternalDurableSDK) |
| 101 | + { |
| 102 | + Collection<Func<PowerShell, object>> output = _pwsh.AddCommand(SetFunctionInvocationContextCommand) |
| 103 | + // The external SetFunctionInvocationContextCommand expects a .json string to deserialize |
| 104 | + // and writes an invoker function to the output pipeline. |
| 105 | + .AddParameter("OrchestrationContext", context.Data.String) |
| 106 | + .InvokeAndClearCommands<Func<PowerShell, object>>(); |
| 107 | + if (output.Count() == 1) |
| 108 | + { |
| 109 | + externalInvoker = new ExternalInvoker(output[0]); |
| 110 | + } |
| 111 | + else |
| 112 | + { |
| 113 | + throw new InvalidOperationException($"Only a single output was expected for an invocation of {SetFunctionInvocationContextCommand}"); |
| 114 | + } |
| 115 | + } |
| 116 | + else |
| 117 | + { |
| 118 | + _pwsh.AddCommand(SetFunctionInvocationContextCommand) |
| 119 | + .AddParameter("OrchestrationContext", orchestrationBindingInfo.Context) |
| 120 | + .InvokeAndClearCommands(); |
| 121 | + } |
| 122 | + _hasInitializedDurableFunction = true; |
| 123 | + return orchestrationBindingInfo; |
| 124 | + } |
| 125 | + |
39 | 126 |
|
40 |
| - _hasSetOrchestrationContext = true; |
| 127 | + public void AddParameter(string name, object value) |
| 128 | + { |
| 129 | + _pwsh.AddParameter(name, value); |
41 | 130 | }
|
42 | 131 |
|
43 | 132 | public void ClearOrchestrationContext()
|
44 | 133 | {
|
45 |
| - if (_hasSetOrchestrationContext) |
| 134 | + if (_hasInitializedDurableFunction) |
46 | 135 | {
|
47 | 136 | _pwsh.AddCommand(SetFunctionInvocationContextCommand)
|
48 | 137 | .AddParameter("Clear", true)
|
49 | 138 | .InvokeAndClearCommands();
|
50 | 139 | }
|
51 | 140 | }
|
52 | 141 |
|
| 142 | + public void TracePipelineObject() |
| 143 | + { |
| 144 | + _pwsh.AddCommand("Microsoft.Azure.Functions.PowerShellWorker\\Trace-PipelineObject"); |
| 145 | + } |
| 146 | + |
53 | 147 | public IAsyncResult BeginInvoke(PSDataCollection<object> output)
|
54 | 148 | {
|
55 | 149 | return _pwsh.BeginInvoke<object, object>(input: null, output);
|
|
0 commit comments