@@ -25,8 +25,7 @@ internal class RequestProcessor
25
25
private readonly PowerShellManagerPool _powershellPool ;
26
26
private readonly DependencyManager _dependencyManager ;
27
27
28
- // Holds the exception if an issue is encountered while installing the function app dependencies
29
- // or while initializing PowerShell.
28
+ // Holds the exception if an issue is encountered while processing the function app dependencies.
30
29
private Exception _initTerminatingError ;
31
30
32
31
// Indicate whether the FunctionApp has been initialized.
@@ -167,7 +166,11 @@ internal StreamingMessage ProcessFunctionLoadRequest(StreamingMessage request)
167
166
try
168
167
{
169
168
_isFunctionAppInitialized = true ;
170
- InitializeForFunctionApp ( request ) ;
169
+ _dependencyManager . Initialize ( functionLoadRequest ) ;
170
+
171
+ // Setup the FunctionApp root path and module path.
172
+ FunctionLoader . SetupWellKnownPaths ( functionLoadRequest ) ;
173
+ _dependencyManager . ProcessDependencyDownload ( _msgStream , request ) ;
171
174
}
172
175
catch ( Exception e )
173
176
{
@@ -200,15 +203,12 @@ internal StreamingMessage ProcessFunctionLoadRequest(StreamingMessage request)
200
203
/// </summary>
201
204
internal StreamingMessage ProcessInvocationRequest ( StreamingMessage request )
202
205
{
203
- AzFunctionInfo functionInfo = null ;
204
- PowerShellManager psManager = null ;
206
+ Exception error = null ;
205
207
206
208
try
207
209
{
208
- if ( _dependencyManager . DependencyDownloadTask != null
209
- && ( _dependencyManager . DependencyDownloadTask . Status != TaskStatus . Canceled
210
- || _dependencyManager . DependencyDownloadTask . Status != TaskStatus . Faulted
211
- || _dependencyManager . DependencyDownloadTask . Status != TaskStatus . RanToCompletion ) )
210
+ if ( _dependencyManager . DependencyDownloadTask != null &&
211
+ ! _dependencyManager . DependencyDownloadTask . IsCompleted )
212
212
{
213
213
var rpcLogger = new RpcLogger ( _msgStream ) ;
214
214
rpcLogger . SetContext ( request . RequestId , request . InvocationRequest ? . InvocationId ) ;
@@ -218,43 +218,42 @@ internal StreamingMessage ProcessInvocationRequest(StreamingMessage request)
218
218
219
219
if ( _dependencyManager . DependencyError != null )
220
220
{
221
- StreamingMessage response = NewStreamingMessageTemplate ( request . RequestId ,
222
- StreamingMessage . ContentOneofCase . InvocationResponse ,
223
- out StatusResult status ) ;
224
- status . Status = StatusResult . Types . Status . Failure ;
225
- status . Exception = _dependencyManager . DependencyError . ToRpcException ( ) ;
226
- response . InvocationResponse . InvocationId = request . InvocationRequest . InvocationId ;
227
- return response ;
228
- }
229
-
230
- functionInfo = _functionLoader . GetFunctionInfo ( request . InvocationRequest . FunctionId ) ;
231
- psManager = _powershellPool . CheckoutIdleWorker ( request , functionInfo ) ;
232
-
233
- if ( _powershellPool . UpperBound == 1 )
234
- {
235
- // When the concurrency upper bound is 1, we can handle only one invocation at a time anyways,
236
- // so it's better to just do it on the current thread to reduce the required synchronization.
237
- ProcessInvocationRequestImpl ( request , functionInfo , psManager ) ;
221
+ error = _dependencyManager . DependencyError ;
238
222
}
239
223
else
240
224
{
241
- // When the concurrency upper bound is more than 1, we have to handle the invocation in a worker
242
- // thread, so multiple invocations can make progress at the same time, even though by time-sharing.
243
- Task . Run ( ( ) => ProcessInvocationRequestImpl ( request , functionInfo , psManager ) ) ;
225
+ AzFunctionInfo functionInfo = _functionLoader . GetFunctionInfo ( request . InvocationRequest . FunctionId ) ;
226
+ PowerShellManager psManager = _powershellPool . CheckoutIdleWorker ( request , functionInfo ) ;
227
+
228
+ if ( _powershellPool . UpperBound == 1 )
229
+ {
230
+ // When the concurrency upper bound is 1, we can handle only one invocation at a time anyways,
231
+ // so it's better to just do it on the current thread to reduce the required synchronization.
232
+ ProcessInvocationRequestImpl ( request , functionInfo , psManager ) ;
233
+ }
234
+ else
235
+ {
236
+ // When the concurrency upper bound is more than 1, we have to handle the invocation in a worker
237
+ // thread, so multiple invocations can make progress at the same time, even though by time-sharing.
238
+ Task . Run ( ( ) => ProcessInvocationRequestImpl ( request , functionInfo , psManager ) ) ;
239
+ }
244
240
}
245
241
}
246
242
catch ( Exception e )
247
243
{
248
- _powershellPool . ReclaimUsedWorker ( psManager ) ;
244
+ error = e ;
245
+ }
249
246
247
+ if ( error != null )
248
+ {
250
249
StreamingMessage response = NewStreamingMessageTemplate (
251
250
request . RequestId ,
252
251
StreamingMessage . ContentOneofCase . InvocationResponse ,
253
252
out StatusResult status ) ;
254
253
255
254
response . InvocationResponse . InvocationId = request . InvocationRequest . InvocationId ;
256
255
status . Status = StatusResult . Types . Status . Failure ;
257
- status . Exception = e . ToRpcException ( ) ;
256
+ status . Exception = error . ToRpcException ( ) ;
258
257
259
258
return response ;
260
259
}
@@ -314,22 +313,6 @@ internal StreamingMessage ProcessFunctionEnvironmentReloadRequest(StreamingMessa
314
313
315
314
#region Helper_Methods
316
315
317
- /// <summary>
318
- /// Initialize the worker based on the FunctionApp that the worker will deal with.
319
- /// </summary>
320
- private void InitializeForFunctionApp ( StreamingMessage request )
321
- {
322
- var functionLoadRequest = request . FunctionLoadRequest ;
323
- if ( functionLoadRequest . ManagedDependencyEnabled )
324
- {
325
- _dependencyManager . Initialize ( functionLoadRequest ) ;
326
- }
327
-
328
- // Setup the FunctionApp root path and module path.
329
- FunctionLoader . SetupWellKnownPaths ( functionLoadRequest ) ;
330
- _dependencyManager . ProcessDependencyDownload ( _msgStream , request ) ;
331
- }
332
-
333
316
/// <summary>
334
317
/// Create an object of 'StreamingMessage' as a template, for further update.
335
318
/// </summary>
0 commit comments