(refactor): clean up child processes on cancel

This commit is contained in:
Maksym Sadovnychyy 2024-01-03 13:46:26 +01:00
parent c52e3db122
commit 635423b083
2 changed files with 6 additions and 0 deletions

View File

@ -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:

View File

@ -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: