Skip to content

Commit

Permalink
Simplify Engine stopping from the REST API
Browse files Browse the repository at this point in the history
  • Loading branch information
na-- committed Jan 27, 2023
1 parent 391c063 commit c720768
Showing 1 changed file with 8 additions and 17 deletions.
25 changes: 8 additions & 17 deletions core/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type Engine struct {
logger *logrus.Entry
stopOnce sync.Once
stopChan chan struct{}
abortFn func(error) // temporary

Samples chan metrics.SampleContainer
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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)
})
}
Expand Down

0 comments on commit c720768

Please sign in to comment.