@@ -262,9 +262,13 @@ private async Task ProcessQueue(CancellationToken token, int taskNumber)
262262 if ( request is null )
263263 continue ;
264264
265+ string ? command = request . payload ? . command ? . ToLower ( ) ;
266+ if ( command == null )
267+ continue ;
268+
265269 // Special shutdown request
266270 string ? requestModuleId = request . payload ? . GetValue ( "moduleId" ) ;
267- if ( request . payload ? . command ? . EqualsIgnoreCase ( "quit" ) == true &&
271+ if ( command == "quit" &&
268272 requestModuleId ? . EqualsIgnoreCase ( _moduleId ) == true )
269273 {
270274 await ShutDown ( 0 ) ;
@@ -274,77 +278,24 @@ private async Task ProcessQueue(CancellationToken token, int taskNumber)
274278 ExpandoObject response ;
275279
276280 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
281+
282+ // the switch statement is easier to read than a series of if/else statements
283+ response = command switch
290284 {
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- }
285+ "get_module_status" => GetModuleStatus ( request ) ,
286+ "get_command_status" => await GetCommandStatus ( request ) . ConfigureAwait ( false ) ,
287+ "cancel_command" => CancelRequest ( request ) . ToExpando ( ) ,
288+ _ => ProcessModuleCommands ( request )
289+ } ;
290+
340291 stopWatch . Stop ( ) ;
341292
342293 // Fill in system-added values for the response
343294 response = response . Merge ( new {
344295 ModuleName = ModuleName ,
345296 ModuleId = _moduleId ,
346297 ProcessMs = stopWatch . ElapsedMilliseconds ,
347- Command = request . payload ? . command ?? string . Empty ,
298+ Command = command ?? string . Empty ,
348299 RequestId = request . reqid
349300 } . ToExpando ( ) ) ;
350301
@@ -379,6 +330,61 @@ private async Task ProcessQueue(CancellationToken token, int taskNumber)
379330 _cancellationTokenSource . Cancel ( ) ;
380331 }
381332
333+ private ExpandoObject ProcessModuleCommands ( BackendRequest request )
334+ {
335+ ExpandoObject response ;
336+ ModuleResponse moduleResponse = Process ( request ) ;
337+ if ( ! _doNotLogCommands . Contains ( request . reqtype ) )
338+ UpdateStatistics ( moduleResponse ) ;
339+
340+ if ( moduleResponse . LongProcessMethod is not null )
341+ {
342+ if ( _longRunningTask is not null && ! _longRunningTask . IsCompleted )
343+ {
344+ response = new
345+ {
346+ Success = false ,
347+ CommandId = _longRunningCommandId ,
348+ Error = "A long running command is already in progress"
349+ } . ToExpando ( ) ;
350+ }
351+ else
352+ {
353+ // We have a previous long running process that is now done, but we
354+ // have not stored (nor returned) the result. We can read the result
355+ // now, but we have to start a new process, so...???
356+ // if (_longRunningTask is not null && _longRunningTask.IsCompleted &&
357+ // _lastLongRunningLastOutput is null)
358+ // _lastLongRunningLastOutput = ...
359+
360+ // Store request Id as the command Id for later, reset the last result
361+ string ? commandId = request . reqid ;
362+
363+ _longRunningCommandId = commandId ;
364+ _lastLongRunningOutput = null ;
365+ _longRunningTask = null ;
366+
367+ // Start the long running process
368+ Console . WriteLine ( "Starting long process with command ID " + commandId ) ;
369+
370+ _longProcessCancellationTokenSource = new CancellationTokenSource ( ) ;
371+ CancellationToken cancellationToken = _longProcessCancellationTokenSource . Token ;
372+ _longRunningTask = moduleResponse . LongProcessMethod ( request , cancellationToken ) ;
373+
374+ response = new
375+ {
376+ Success = true ,
377+ CommandId = commandId ,
378+ Message = "Command is running in the background" ,
379+ CommandStatus = "running"
380+ } . ToExpando ( ) ;
381+ }
382+ }
383+ else
384+ response = moduleResponse . ToExpando ( ) ;
385+ return response ;
386+ }
387+
382388 /// <summary>
383389 /// This stops the application
384390 /// </summary>
0 commit comments