@@ -11,80 +11,95 @@ namespace Microsoft.Azure.Functions.PowerShellWorker.Durable
1111 using System . Linq ;
1212 using System . Management . Automation ;
1313
14- // using PowerShellWorker.Utility;
1514 using Microsoft . Azure . Functions . PowerShellWorker . Durable . Actions ;
1615
1716 internal class OrchestrationInvoker : IOrchestrationInvoker
1817 {
19- private Action < PowerShell > externalInvoker = null ;
18+ private IExternalInvoker _externalInvoker ;
2019
21- public Hashtable Invoke ( OrchestrationBindingInfo orchestrationBindingInfo , IPowerShellServices pwsh )
20+ public Hashtable Invoke (
21+ OrchestrationBindingInfo orchestrationBindingInfo ,
22+ IPowerShellServices powerShellServices )
2223 {
23-
2424 try
2525 {
26- if ( pwsh . UseExternalDurableSDK ( ) )
26+ if ( powerShellServices . UseExternalDurableSDK ( ) )
2727 {
28- externalInvoker . Invoke ( pwsh . GetPowerShell ( ) ) ;
29- var result = orchestrationBindingInfo . Context . ExternalResult ;
30- var isError = orchestrationBindingInfo . Context . ExternalIsError ;
31- if ( isError )
32- {
33- throw ( Exception ) result ;
34- }
35- else
36- {
37- return ( Hashtable ) result ;
38- }
28+ return InvokeExternalDurableSDK ( orchestrationBindingInfo , powerShellServices ) ;
3929 }
30+ return InvokeInternalDurableSDK ( orchestrationBindingInfo , powerShellServices ) ;
31+ }
32+ finally
33+ {
34+ powerShellServices . ClearStreamsAndCommands ( ) ;
35+ }
36+ }
4037
41- var outputBuffer = new PSDataCollection < object > ( ) ;
42- var context = orchestrationBindingInfo . Context ;
38+ public Hashtable InvokeExternalDurableSDK (
39+ OrchestrationBindingInfo orchestrationBindingInfo ,
40+ IPowerShellServices powerShellServices )
41+ {
4342
44- // context.History should never be null when initializing CurrentUtcDateTime
45- var orchestrationStart = context . History . First (
46- e => e . EventType == HistoryEventType . OrchestratorStarted ) ;
47- context . CurrentUtcDateTime = orchestrationStart . Timestamp . ToUniversalTime ( ) ;
43+ _externalInvoker . Invoke ( ) ;
44+ var result = orchestrationBindingInfo . Context . ExternalSDKResult ;
45+ var isError = orchestrationBindingInfo . Context . ExternalSDKIsError ;
46+ if ( isError )
47+ {
48+ throw ( Exception ) result ;
49+ }
50+ else
51+ {
52+ return ( Hashtable ) result ;
53+ }
54+ }
55+
56+ public Hashtable InvokeInternalDurableSDK (
57+ OrchestrationBindingInfo orchestrationBindingInfo ,
58+ IPowerShellServices powerShellServices )
59+ {
60+ var outputBuffer = new PSDataCollection < object > ( ) ;
61+ var context = orchestrationBindingInfo . Context ;
4862
49- // Marks the first OrchestratorStarted event as processed
50- orchestrationStart . IsProcessed = true ;
63+ // context.History should never be null when initializing CurrentUtcDateTime
64+ var orchestrationStart = context . History . First (
65+ e => e . EventType == HistoryEventType . OrchestratorStarted ) ;
66+ context . CurrentUtcDateTime = orchestrationStart . Timestamp . ToUniversalTime ( ) ;
5167
52- // Finish initializing the Function invocation
53- pwsh . AddParameter ( orchestrationBindingInfo . ParameterName , context ) ;
54- pwsh . TracePipelineObject ( ) ;
68+ // Marks the first OrchestratorStarted event as processed
69+ orchestrationStart . IsProcessed = true ;
5570
56- var asyncResult = pwsh . BeginInvoke ( outputBuffer ) ;
71+ // Finish initializing the Function invocation
72+ powerShellServices . AddParameter ( orchestrationBindingInfo . ParameterName , context ) ;
73+ powerShellServices . TracePipelineObject ( ) ;
5774
58- var ( shouldStop , actions ) =
59- orchestrationBindingInfo . Context . OrchestrationActionCollector . WaitForActions ( asyncResult . AsyncWaitHandle ) ;
75+ var asyncResult = powerShellServices . BeginInvoke ( outputBuffer ) ;
6076
61- if ( shouldStop )
77+ var ( shouldStop , actions ) =
78+ orchestrationBindingInfo . Context . OrchestrationActionCollector . WaitForActions ( asyncResult . AsyncWaitHandle ) ;
79+
80+ if ( shouldStop )
81+ {
82+ // The orchestration function should be stopped and restarted
83+ powerShellServices . StopInvoke ( ) ;
84+ // return (Hashtable)orchestrationBindingInfo.Context.OrchestrationActionCollector.output;
85+ return CreateOrchestrationResult ( isDone : false , actions , output : null , context . CustomStatus ) ;
86+ }
87+ else
88+ {
89+ try
6290 {
63- // The orchestration function should be stopped and restarted
64- pwsh . StopInvoke ( ) ;
65- return CreateOrchestrationResult ( isDone : false , actions , output : null , context . CustomStatus ) ;
91+ // The orchestration function completed
92+ powerShellServices . EndInvoke ( asyncResult ) ;
93+ var result = CreateReturnValueFromFunctionOutput ( outputBuffer ) ;
94+ return CreateOrchestrationResult ( isDone : true , actions , output : result , context . CustomStatus ) ;
6695 }
67- else
96+ catch ( Exception e )
6897 {
69- try
70- {
71- // The orchestration function completed
72- pwsh . EndInvoke ( asyncResult ) ;
73- var result = CreateReturnValueFromFunctionOutput ( outputBuffer ) ;
74- return CreateOrchestrationResult ( isDone : true , actions , output : result , context . CustomStatus ) ;
75- }
76- catch ( Exception e )
77- {
78- // The orchestrator code has thrown an unhandled exception:
79- // this should be treated as an entire orchestration failure
80- throw new OrchestrationFailureException ( actions , context . CustomStatus , e ) ;
81- }
98+ // The orchestrator code has thrown an unhandled exception:
99+ // this should be treated as an entire orchestration failure
100+ throw new OrchestrationFailureException ( actions , context . CustomStatus , e ) ;
82101 }
83102 }
84- finally
85- {
86- pwsh . ClearStreamsAndCommands ( ) ;
87- }
88103 }
89104
90105 public static object CreateReturnValueFromFunctionOutput ( IList < object > pipelineItems )
@@ -107,9 +122,9 @@ private static Hashtable CreateOrchestrationResult(
107122 return new Hashtable { { "$return" , orchestrationMessage } } ;
108123 }
109124
110- public void SetExternalInvoker ( Action < PowerShell > externalInvoker )
125+ public void SetExternalInvoker ( IExternalInvoker externalInvoker )
111126 {
112- this . externalInvoker = externalInvoker ;
127+ _externalInvoker = externalInvoker ;
113128 }
114129 }
115130}
0 commit comments