From 635423b0837991bf7c9c0b5e3e71b316aafb8a81 Mon Sep 17 00:00:00 2001 From: Maksym Sadovnychyy Date: Wed, 3 Jan 2024 13:46:26 +0100 Subject: [PATCH] (refactor): clean up child processes on cancel --- src/UScheduler/BackgroundServices/PSScriptBackgroundService.cs | 3 +++ src/UScheduler/BackgroundServices/ProcessBackgroundService.cs | 3 +++ 2 files changed, 6 insertions(+) diff --git a/src/UScheduler/BackgroundServices/PSScriptBackgroundService.cs b/src/UScheduler/BackgroundServices/PSScriptBackgroundService.cs index bee0e63..1dd322e 100644 --- a/src/UScheduler/BackgroundServices/PSScriptBackgroundService.cs +++ b/src/UScheduler/BackgroundServices/PSScriptBackgroundService.cs @@ -51,10 +51,13 @@ namespace UScheduler.BackgroundServices { // When the stopping token is canceled, for example, a call made from services.msc, // we shouldn't exit with a non-zero exit code. In other words, this is expected... _logger.LogInformation("Stopping PSScriptBackgroundService due to cancellation request"); + _psScriptService.TerminateAllScripts(); } catch (Exception ex) { _logger.LogError(ex, "{Message}", ex.Message); + _psScriptService.TerminateAllScripts(); + // Terminates this process and returns an exit code to the operating system. // This is required to avoid the 'BackgroundServiceExceptionBehavior', which // performs one of two scenarios: diff --git a/src/UScheduler/BackgroundServices/ProcessBackgroundService.cs b/src/UScheduler/BackgroundServices/ProcessBackgroundService.cs index 5037542..02c8036 100644 --- a/src/UScheduler/BackgroundServices/ProcessBackgroundService.cs +++ b/src/UScheduler/BackgroundServices/ProcessBackgroundService.cs @@ -55,10 +55,13 @@ public sealed class ProcessBackgroundService : BackgroundService { // When the stopping token is canceled, for example, a call made from services.msc, // we shouldn't exit with a non-zero exit code. In other words, this is expected... _logger.LogInformation("Stopping ProcessBackgroundService due to cancellation request"); + _processService.TerminateAllProcesses(); } catch (Exception ex) { _logger.LogError(ex, "{Message}", ex.Message); + _processService.TerminateAllProcesses(); + // Terminates this process and returns an exit code to the operating system. // This is required to avoid the 'BackgroundServiceExceptionBehavior', which // performs one of two scenarios: