@@ -20,7 +20,7 @@ internal class RequestProcessor
20
20
private readonly FunctionLoader _functionLoader ;
21
21
private readonly RpcLogger _logger ;
22
22
private readonly MessagingStream _msgStream ;
23
- private readonly PowerShellManager _powerShellManager ;
23
+ private readonly PowerShellManagerPool _powershellPool ;
24
24
25
25
// Indicate whether the FunctionApp has been initialized.
26
26
private bool _isFunctionAppInitialized ;
@@ -29,7 +29,7 @@ internal RequestProcessor(MessagingStream msgStream)
29
29
{
30
30
_msgStream = msgStream ;
31
31
_logger = new RpcLogger ( msgStream ) ;
32
- _powerShellManager = new PowerShellManager ( _logger ) ;
32
+ _powershellPool = new PowerShellManagerPool ( _logger ) ;
33
33
_functionLoader = new FunctionLoader ( ) ;
34
34
}
35
35
@@ -98,9 +98,7 @@ internal StreamingMessage ProcessFunctionLoadRequest(StreamingMessage request)
98
98
if ( ! _isFunctionAppInitialized )
99
99
{
100
100
FunctionLoader . SetupWellKnownPaths ( functionLoadRequest ) ;
101
- _powerShellManager . PerformWorkerLevelInitialization ( ) ;
102
- _powerShellManager . PerformRunspaceLevelInitialization ( ) ;
103
-
101
+ _powershellPool . Initialize ( ) ;
104
102
_isFunctionAppInitialized = true ;
105
103
}
106
104
@@ -122,6 +120,7 @@ internal StreamingMessage ProcessFunctionLoadRequest(StreamingMessage request)
122
120
/// </summary>
123
121
internal StreamingMessage ProcessInvocationRequest ( StreamingMessage request )
124
122
{
123
+ PowerShellManager psManager = null ;
125
124
InvocationRequest invocationRequest = request . InvocationRequest ;
126
125
127
126
StreamingMessage response = NewStreamingMessageTemplate (
@@ -130,18 +129,18 @@ internal StreamingMessage ProcessInvocationRequest(StreamingMessage request)
130
129
out StatusResult status ) ;
131
130
response . InvocationResponse . InvocationId = invocationRequest . InvocationId ;
132
131
133
- // Invoke powershell logic and return hashtable of out binding data
134
132
try
135
133
{
136
134
// Load information about the function
137
135
var functionInfo = _functionLoader . GetFunctionInfo ( invocationRequest . FunctionId ) ;
138
- _powerShellManager . RegisterFunctionMetadata ( functionInfo ) ;
136
+ psManager = _powershellPool . CheckoutIdleWorker ( functionInfo ) ;
139
137
138
+ // Invoke the function and return a hashtable of out binding data
140
139
Hashtable results = functionInfo . Type == AzFunctionType . OrchestrationFunction
141
- ? InvokeOrchestrationFunction ( functionInfo , invocationRequest )
142
- : InvokeSingleActivityFunction ( functionInfo , invocationRequest ) ;
140
+ ? InvokeOrchestrationFunction ( psManager , functionInfo , invocationRequest )
141
+ : InvokeSingleActivityFunction ( psManager , functionInfo , invocationRequest ) ;
143
142
144
- BindOutputFromResult ( response . InvocationResponse , functionInfo , results ) ;
143
+ BindOutputFromResult ( psManager , response . InvocationResponse , functionInfo , results ) ;
145
144
}
146
145
catch ( Exception e )
147
146
{
@@ -150,7 +149,7 @@ internal StreamingMessage ProcessInvocationRequest(StreamingMessage request)
150
149
}
151
150
finally
152
151
{
153
- _powerShellManager . UnregisterFunctionMetadata ( ) ;
152
+ _powershellPool . ReclaimUsedWorker ( psManager ) ;
154
153
}
155
154
156
155
return response ;
@@ -188,15 +187,15 @@ private StreamingMessage NewStreamingMessageTemplate(string requestId, Streaming
188
187
/// <summary>
189
188
/// Invoke an orchestration function.
190
189
/// </summary>
191
- private Hashtable InvokeOrchestrationFunction ( AzFunctionInfo functionInfo , InvocationRequest invocationRequest )
190
+ private Hashtable InvokeOrchestrationFunction ( PowerShellManager psManager , AzFunctionInfo functionInfo , InvocationRequest invocationRequest )
192
191
{
193
192
throw new NotImplementedException ( "Durable function is not yet supported for PowerShell" ) ;
194
193
}
195
194
196
195
/// <summary>
197
196
/// Invoke a regular function or an activity function.
198
197
/// </summary>
199
- private Hashtable InvokeSingleActivityFunction ( AzFunctionInfo functionInfo , InvocationRequest invocationRequest )
198
+ private Hashtable InvokeSingleActivityFunction ( PowerShellManager psManager , AzFunctionInfo functionInfo , InvocationRequest invocationRequest )
200
199
{
201
200
// Bundle all TriggerMetadata into Hashtable to send down to PowerShell
202
201
var triggerMetadata = new Hashtable ( StringComparer . OrdinalIgnoreCase ) ;
@@ -210,16 +209,13 @@ private Hashtable InvokeSingleActivityFunction(AzFunctionInfo functionInfo, Invo
210
209
}
211
210
}
212
211
213
- return _powerShellManager . InvokeFunction (
214
- functionInfo ,
215
- triggerMetadata ,
216
- invocationRequest . InputData ) ;
212
+ return psManager . InvokeFunction ( functionInfo , triggerMetadata , invocationRequest . InputData ) ;
217
213
}
218
214
219
215
/// <summary>
220
216
/// Set the 'ReturnValue' and 'OutputData' based on the invocation results appropriately.
221
217
/// </summary>
222
- private void BindOutputFromResult ( InvocationResponse response , AzFunctionInfo functionInfo , Hashtable results )
218
+ private void BindOutputFromResult ( PowerShellManager psManager , InvocationResponse response , AzFunctionInfo functionInfo , Hashtable results )
223
219
{
224
220
switch ( functionInfo . Type )
225
221
{
@@ -231,14 +227,14 @@ private void BindOutputFromResult(InvocationResponse response, AzFunctionInfo fu
231
227
string outBindingName = binding . Key ;
232
228
if ( string . Equals ( outBindingName , AzFunctionInfo . DollarReturn , StringComparison . OrdinalIgnoreCase ) )
233
229
{
234
- response . ReturnValue = results [ outBindingName ] . ToTypedData ( _powerShellManager ) ;
230
+ response . ReturnValue = results [ outBindingName ] . ToTypedData ( psManager ) ;
235
231
continue ;
236
232
}
237
233
238
234
ParameterBinding paramBinding = new ParameterBinding ( )
239
235
{
240
236
Name = outBindingName ,
241
- Data = results [ outBindingName ] . ToTypedData ( _powerShellManager )
237
+ Data = results [ outBindingName ] . ToTypedData ( psManager )
242
238
} ;
243
239
244
240
response . OutputData . Add ( paramBinding ) ;
@@ -247,7 +243,7 @@ private void BindOutputFromResult(InvocationResponse response, AzFunctionInfo fu
247
243
248
244
case AzFunctionType . OrchestrationFunction :
249
245
case AzFunctionType . ActivityFunction :
250
- response . ReturnValue = results [ AzFunctionInfo . DollarReturn ] . ToTypedData ( _powerShellManager ) ;
246
+ response . ReturnValue = results [ AzFunctionInfo . DollarReturn ] . ToTypedData ( psManager ) ;
251
247
break ;
252
248
253
249
default :
0 commit comments