@@ -132,10 +132,10 @@ internal async void InitializeWebhostLanguageWorkerChannel()
132
132
workerChannel . SendFunctionLoadRequests ( ) ;
133
133
}
134
134
135
- internal void ShutdownWebhostLanguageWorkerChannels ( )
135
+ internal async void ShutdownWebhostLanguageWorkerChannels ( )
136
136
{
137
137
_logger . LogDebug ( "{workerRuntimeConstant}={value}. Will shutdown all the worker channels that started in placeholder mode" , LanguageWorkerConstants . FunctionWorkerRuntimeSettingName , _workerRuntime ) ;
138
- _webHostLanguageWorkerChannelManager . ShutdownChannels ( ) ;
138
+ await _webHostLanguageWorkerChannelManager ? . ShutdownChannelsAsync ( ) ;
139
139
}
140
140
141
141
private void StartWorkerProcesses ( int startIndex , Action startAction )
@@ -187,16 +187,20 @@ public async Task InitializeAsync(IEnumerable<FunctionMetadata> functions)
187
187
if ( Utility . IsSupportedRuntime ( _workerRuntime , _workerConfigs ) )
188
188
{
189
189
State = FunctionDispatcherState . Initializing ;
190
- IEnumerable < ILanguageWorkerChannel > initializedChannels = _webHostLanguageWorkerChannelManager . GetChannels ( _workerRuntime ) ;
191
- if ( initializedChannels != null )
190
+ Dictionary < string , TaskCompletionSource < ILanguageWorkerChannel > > webhostLanguageWorkerChannels = _webHostLanguageWorkerChannelManager . GetChannels ( _workerRuntime ) ;
191
+ if ( webhostLanguageWorkerChannels != null )
192
192
{
193
- foreach ( var initializedChannel in initializedChannels )
193
+ foreach ( string workerId in webhostLanguageWorkerChannels . Keys )
194
194
{
195
- _logger . LogDebug ( "Found initialized language worker channel for runtime: {workerRuntime} workerId:{workerId}" , _workerRuntime , initializedChannel . Id ) ;
196
- initializedChannel . SetupFunctionInvocationBuffers ( _functions ) ;
197
- initializedChannel . SendFunctionLoadRequests ( ) ;
195
+ if ( webhostLanguageWorkerChannels . TryGetValue ( workerId , out TaskCompletionSource < ILanguageWorkerChannel > initializedLanguageWorkerChannelTask ) )
196
+ {
197
+ _logger . LogDebug ( "Found initialized language worker channel for runtime: {workerRuntime} workerId:{workerId}" , _workerRuntime , workerId ) ;
198
+ ILanguageWorkerChannel initializedLanguageWorkerChannel = await initializedLanguageWorkerChannelTask . Task ;
199
+ initializedLanguageWorkerChannel . SetupFunctionInvocationBuffers ( _functions ) ;
200
+ initializedLanguageWorkerChannel . SendFunctionLoadRequests ( ) ;
201
+ }
198
202
}
199
- StartWorkerProcesses ( initializedChannels . Count ( ) , InitializeWebhostLanguageWorkerChannel ) ;
203
+ StartWorkerProcesses ( webhostLanguageWorkerChannels . Count ( ) , InitializeWebhostLanguageWorkerChannel ) ;
200
204
State = FunctionDispatcherState . Initialized ;
201
205
}
202
206
else
@@ -207,11 +211,11 @@ public async Task InitializeAsync(IEnumerable<FunctionMetadata> functions)
207
211
}
208
212
}
209
213
210
- public void Invoke ( ScriptInvocationContext invocationContext )
214
+ public async Task InvokeAsync ( ScriptInvocationContext invocationContext )
211
215
{
212
216
try
213
217
{
214
- IEnumerable < ILanguageWorkerChannel > workerChannels = GetInitializedWorkerChannels ( ) ;
218
+ IEnumerable < ILanguageWorkerChannel > workerChannels = await GetInitializedWorkerChannelsAsync ( ) ;
215
219
var languageWorkerChannel = _functionDispatcherLoadBalancer . GetLanguageWorkerChannel ( workerChannels , _maxProcessCount ) ;
216
220
if ( languageWorkerChannel . FunctionInputBuffers . TryGetValue ( invocationContext . FunctionMetadata . FunctionId , out BufferBlock < ScriptInvocationContext > bufferBlock ) )
217
221
{
@@ -229,9 +233,21 @@ public void Invoke(ScriptInvocationContext invocationContext)
229
233
}
230
234
}
231
235
232
- internal IEnumerable < ILanguageWorkerChannel > GetInitializedWorkerChannels ( )
236
+ internal async Task < IEnumerable < ILanguageWorkerChannel > > GetInitializedWorkerChannelsAsync ( )
233
237
{
234
- IEnumerable < ILanguageWorkerChannel > webhostChannels = _webHostLanguageWorkerChannelManager . GetChannels ( _workerRuntime ) ;
238
+ Dictionary < string , TaskCompletionSource < ILanguageWorkerChannel > > webhostChannelDictionary = _webHostLanguageWorkerChannelManager . GetChannels ( _workerRuntime ) ;
239
+ List < ILanguageWorkerChannel > webhostChannels = null ;
240
+ if ( webhostChannelDictionary != null )
241
+ {
242
+ webhostChannels = new List < ILanguageWorkerChannel > ( ) ;
243
+ foreach ( string workerId in webhostChannelDictionary . Keys )
244
+ {
245
+ if ( webhostChannelDictionary . TryGetValue ( workerId , out TaskCompletionSource < ILanguageWorkerChannel > initializedLanguageWorkerChannelTask ) )
246
+ {
247
+ webhostChannels . Add ( await initializedLanguageWorkerChannelTask . Task ) ;
248
+ }
249
+ }
250
+ }
235
251
IEnumerable < ILanguageWorkerChannel > workerChannels = webhostChannels == null ? _jobHostLanguageWorkerChannelManager . GetChannels ( ) : webhostChannels . Union ( _jobHostLanguageWorkerChannelManager . GetChannels ( ) ) ;
236
252
IEnumerable < ILanguageWorkerChannel > initializedWorkers = workerChannels . Where ( ch => ch . State == LanguageWorkerChannelState . Initialized ) ;
237
253
if ( initializedWorkers . Count ( ) > _maxProcessCount )
@@ -247,7 +263,7 @@ public async void WorkerError(WorkerErrorEvent workerError)
247
263
{
248
264
_logger . LogDebug ( "Handling WorkerErrorEvent for runtime:{runtime}, workerId:{workerId}" , workerError . Language , workerError . WorkerId ) ;
249
265
_languageWorkerErrors . Add ( workerError . Exception ) ;
250
- bool isPreInitializedChannel = _webHostLanguageWorkerChannelManager . ShutdownChannelIfExists ( workerError . Language , workerError . WorkerId ) ;
266
+ bool isPreInitializedChannel = await _webHostLanguageWorkerChannelManager . ShutdownChannelIfExistsAsync ( workerError . Language , workerError . WorkerId ) ;
251
267
if ( ! isPreInitializedChannel )
252
268
{
253
269
_logger . LogDebug ( "Disposing errored channel for workerId: {channelId}, for runtime:{language}" , workerError . WorkerId , workerError . Language ) ;
@@ -257,8 +273,11 @@ public async void WorkerError(WorkerErrorEvent workerError)
257
273
_jobHostLanguageWorkerChannelManager . DisposeAndRemoveChannel ( erroredChannel ) ;
258
274
}
259
275
}
260
- _logger . LogDebug ( "Restarting worker channel for runtime:{runtime}" , workerError . Language ) ;
261
- await RestartWorkerChannel ( workerError . Language , workerError . WorkerId ) ;
276
+ if ( _workerRuntime . Equals ( workerError . Language , StringComparison . InvariantCultureIgnoreCase ) )
277
+ {
278
+ _logger . LogDebug ( "Restarting worker channel for runtime:{runtime}" , workerError . Language ) ;
279
+ await RestartWorkerChannel ( workerError . Language , workerError . WorkerId ) ;
280
+ }
262
281
}
263
282
}
264
283
@@ -279,6 +298,7 @@ protected virtual void Dispose(bool disposing)
279
298
{
280
299
if ( ! _disposed && disposing )
281
300
{
301
+ _logger . LogDebug ( "Disposing FunctionDispatcher" ) ;
282
302
_workerErrorSubscription . Dispose ( ) ;
283
303
_processStartCancellationToken . Cancel ( ) ;
284
304
_processStartCancellationToken . Dispose ( ) ;
0 commit comments