From c720768d84e65a1360dfabcdd6c9d86f1fbaa925 Mon Sep 17 00:00:00 2001 From: Nedyalko Andreev Date: Thu, 8 Dec 2022 03:49:35 +0200 Subject: [PATCH] Simplify Engine stopping from the REST API --- core/engine.go | 25 ++++++++----------------- 1 file changed, 8 insertions(+), 17 deletions(-) diff --git a/core/engine.go b/core/engine.go index d7cc1497bec..4e3df278a0d 100644 --- a/core/engine.go +++ b/core/engine.go @@ -43,6 +43,7 @@ type Engine struct { logger *logrus.Entry stopOnce sync.Once stopChan chan struct{} + abortFn func(error) // temporary Samples chan metrics.SampleContainer } @@ -107,6 +108,7 @@ func (e *Engine) Init(globalCtx, runCtx context.Context) (run func() error, wait // TODO: move all of this in a separate struct? see main TODO above runSubCtx, runSubAbort := execution.NewTestRunContext(runCtx, e.logger) + e.abortFn = runSubAbort execRunResult := make(chan error) engineRunResult := make(chan error) @@ -188,23 +190,6 @@ func (e *Engine) startBackgroundProcesses( } }() - // Listen for stop calls from the REST API - processes.Add(1) - go func() { - defer processes.Done() - select { - case <-e.stopChan: - e.logger.Debug("run: stopped by user via REST API; exiting...") - err := errext.WithAbortReasonIfNone( - errext.WithExitCodeIfNone(errors.New("test run stopped from the REST API"), exitcodes.ScriptStoppedFromRESTAPI), - errext.AbortedByUser, - ) - runSubAbort(err) - case <-runCtx.Done(): - // do nothing - } - }() - return processes.Wait } @@ -285,6 +270,12 @@ func (e *Engine) IsTainted() bool { // Stop closes a signal channel, forcing a running Engine to return func (e *Engine) Stop() { e.stopOnce.Do(func() { + e.logger.Debug("run: stopped by user via REST API; exiting...") + err := errext.WithAbortReasonIfNone( + errext.WithExitCodeIfNone(errors.New("test run stopped from the REST API"), exitcodes.ScriptStoppedFromRESTAPI), + errext.AbortedByUser, + ) + e.abortFn(err) close(e.stopChan) }) }