@@ -74,9 +74,10 @@ public abstract class ModuleWorkerBase : BackgroundService
7474 public string ? InferenceDevice { get ; set ; } = "CPU" ;
7575
7676 /// <summary>
77- /// Gets or sets the name of the hardware acceleration execution provider
77+ /// Gets or sets the name of the hardware acceleration execution provider (meaning the
78+ /// library being used to power the GPU, such as DirectML, Torch, TF etc)
7879 /// </summary>
79- public string ? InferenceLibrary { get ; set ; } = "CPU" ;
80+ public string ? InferenceLibrary { get ; set ; } = string . Empty ;
8081
8182 /// <summary>
8283 /// Gets the logger instance.
@@ -262,89 +263,36 @@ private async Task ProcessQueue(CancellationToken token, int taskNumber)
262263 if ( request is null )
263264 continue ;
264265
266+ string ? command = request . payload ? . command ? . ToLower ( ) ;
267+ if ( command == null )
268+ continue ;
269+
265270 // Special shutdown request
266271 string ? requestModuleId = request . payload ? . GetValue ( "moduleId" ) ;
267- if ( request . payload ? . command ? . EqualsIgnoreCase ( "quit" ) == true &&
268- requestModuleId ? . EqualsIgnoreCase ( _moduleId ) == true )
272+ if ( command == "quit" && requestModuleId ? . EqualsIgnoreCase ( _moduleId ) == true )
269273 {
270274 await ShutDown ( 0 ) ;
271275 return ;
272276 }
273277
274- ExpandoObject response ;
275-
276278 Stopwatch stopWatch = Stopwatch . StartNew ( ) ;
277- if ( request . payload ? . command ? . EqualsIgnoreCase ( "get_module_status" ) == true )
278- {
279- response = GetModuleStatus ( request ) ;
280- }
281- else if ( request . payload ? . command ? . EqualsIgnoreCase ( "get_command_status" ) == true )
282- {
283- response = await GetCommandStatus ( request ) . ConfigureAwait ( false ) ;
284- }
285- else if ( request . payload ? . command ? . EqualsIgnoreCase ( "cancel_command" ) == true )
286- {
287- response = CancelRequest ( request ) . ToExpando ( ) ;
288- }
289- else
279+
280+ ExpandoObject response = command switch
290281 {
291- ModuleResponse moduleResponse = Process ( request ) ;
292- if ( ! _doNotLogCommands . Contains ( request . reqtype ) )
293- UpdateStatistics ( moduleResponse ) ;
294-
295- if ( moduleResponse . LongProcessMethod is not null )
296- {
297- if ( _longRunningTask is not null && ! _longRunningTask . IsCompleted )
298- {
299- response = new {
300- Success = false ,
301- CommandId = _longRunningCommandId ,
302- Error = "A long running command is already in progress"
303- } . ToExpando ( ) ;
304- }
305- else
306- {
307- // We have a previous long running process that is now done, but we
308- // have not stored (nor returned) the result. We can read the result
309- // now, but we have to start a new process, so...???
310- // if (_longRunningTask is not null && _longRunningTask.IsCompleted &&
311- // _lastLongRunningLastOutput is null)
312- // _lastLongRunningLastOutput = ...
313-
314- // Store request Id as the command Id for later, reset the last result
315- string ? commandId = request . reqid ;
316-
317- _longRunningCommandId = commandId ;
318- _lastLongRunningOutput = null ;
319- _longRunningTask = null ;
320-
321- // Start the long running process
322- Console . WriteLine ( "Starting long process with command ID " + commandId ) ;
323-
324- _longProcessCancellationTokenSource = new CancellationTokenSource ( ) ;
325- CancellationToken cancellationToken = _longProcessCancellationTokenSource . Token ;
326- _longRunningTask = Task . Run ( ( ) => moduleResponse . LongProcessMethod ( request , cancellationToken ) ,
327- cancellationToken ) ;
328-
329- response = new {
330- Success = true ,
331- CommandId = commandId ,
332- Message = "Command is running in the background" ,
333- CommandStatus = "running"
334- } . ToExpando ( ) ;
335- }
336- }
337- else
338- response = moduleResponse . ToExpando ( ) ;
339- }
282+ "get_module_status" => GetModuleStatus ( request ) ,
283+ "get_command_status" => await GetCommandStatus ( request ) . ConfigureAwait ( false ) ,
284+ "cancel_command" => CancelRequest ( request ) . ToExpando ( ) ,
285+ _ => ProcessModuleCommands ( request )
286+ } ;
287+
340288 stopWatch . Stop ( ) ;
341289
342290 // Fill in system-added values for the response
343291 response = response . Merge ( new {
344292 ModuleName = ModuleName ,
345293 ModuleId = _moduleId ,
346294 ProcessMs = stopWatch . ElapsedMilliseconds ,
347- Command = request . payload ? . command ?? string . Empty ,
295+ Command = command ?? string . Empty ,
348296 RequestId = request . reqid
349297 } . ToExpando ( ) ) ;
350298
@@ -379,6 +327,71 @@ private async Task ProcessQueue(CancellationToken token, int taskNumber)
379327 _cancellationTokenSource . Cancel ( ) ;
380328 }
381329
330+ /// <summary>
331+ /// Processes a command sent to a module.
332+ /// </summary>
333+ /// <param name="request">The incoming request</param>
334+ /// <returns>An expando object with the results</returns>
335+ private ExpandoObject ProcessModuleCommands ( BackendRequest request )
336+ {
337+ ExpandoObject response ;
338+
339+ ModuleResponse moduleResponse = Process ( request ) ;
340+
341+ if ( ! _doNotLogCommands . Contains ( request . reqtype ) )
342+ UpdateStatistics ( moduleResponse ) ;
343+
344+ if ( moduleResponse . LongProcessMethod is not null )
345+ {
346+ if ( _longRunningTask is not null && ! _longRunningTask . IsCompleted )
347+ {
348+ response = new
349+ {
350+ Success = false ,
351+ CommandId = _longRunningCommandId ,
352+ Error = "A long running command is already in progress"
353+ } . ToExpando ( ) ;
354+ }
355+ else
356+ {
357+ // We have a previous long running process that is now done, but we
358+ // have not stored (nor returned) the result. We can read the result
359+ // now, but we have to start a new process, so...???
360+ // if (_longRunningTask is not null && _longRunningTask.IsCompleted &&
361+ // _lastLongRunningLastOutput is null)
362+ // _lastLongRunningLastOutput = ...
363+
364+ // Store request Id as the command Id for later, reset the last result
365+ string ? commandId = request . reqid ;
366+
367+ _longRunningCommandId = commandId ;
368+ _lastLongRunningOutput = null ;
369+ _longRunningTask = null ;
370+
371+ // Start the long running process
372+ Console . WriteLine ( "Starting long process with command ID " + commandId ) ;
373+
374+ _longProcessCancellationTokenSource = new CancellationTokenSource ( ) ;
375+ CancellationToken cancellationToken = _longProcessCancellationTokenSource . Token ;
376+ _longRunningTask = moduleResponse . LongProcessMethod ( request , cancellationToken ) ;
377+
378+ response = new
379+ {
380+ Success = true ,
381+ CommandId = commandId ,
382+ Message = "Command is running in the background" ,
383+ CommandStatus = "running"
384+ } . ToExpando ( ) ;
385+ }
386+ }
387+ else
388+ {
389+ response = moduleResponse . ToExpando ( ) ;
390+ }
391+
392+ return response ;
393+ }
394+
382395 /// <summary>
383396 /// This stops the application
384397 /// </summary>
0 commit comments