Skip to content

Commit

Permalink
Immediately shutdown k6 cloud on second Ctrl+C signal (grafana#1647)
Browse files Browse the repository at this point in the history
  • Loading branch information
theerapatcha authored and salem84 committed Nov 5, 2020
1 parent 7416ff2 commit b3c22ae
Showing 1 changed file with 22 additions and 14 deletions.
36 changes: 22 additions & 14 deletions cmd/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,9 @@ This will execute the test on the k6 cloud service. Use "k6 login cloud" to auth
name = filepath.Base(filename)
}

globalCtx, globalCancel := context.WithCancel(context.Background())
defer globalCancel()

// Start cloud test run
modifyAndPrintBar(progressBar, pb.WithConstProgress(0, "Validating script options"))
client := cloud.NewClient(logger, cloudConfig.Token.String, cloudConfig.Host.String, consts.Version)
Expand Down Expand Up @@ -226,7 +229,7 @@ This will execute the test on the k6 cloud service. Use "k6 login cloud" to auth
pb.WithConstProgress(0, "Initializing the cloud test"),
)

progressCtx, progressCancel := context.WithCancel(context.Background())
progressCtx, progressCancel := context.WithCancel(globalCtx)
progressBarWG := &sync.WaitGroup{}
progressBarWG.Add(1)
defer progressBarWG.Wait()
Expand All @@ -240,6 +243,19 @@ This will execute the test on the k6 cloud service. Use "k6 login cloud" to auth
sigC := make(chan os.Signal, 1)
signal.Notify(sigC, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
defer signal.Stop(sigC)
go func() {
sig := <-sigC
logger.WithField("sig", sig).Print("Stopping k6 in response to signal...")
err := client.StopCloudTestRun(refID)
if err != nil {
logger.WithError(err).Error("Stop cloud test error")
}
globalCancel()

sig = <-sigC
logger.WithField("sig", sig).Error("Aborting k6 in response to signal")
os.Exit(externalAbortErrorCode)
}()

var (
startTime time.Time
Expand Down Expand Up @@ -279,12 +295,11 @@ This will execute the test on the k6 cloud service. Use "k6 login cloud" to auth
)

ticker := time.NewTicker(time.Millisecond * 2000)
shouldExitLoop := false
if showCloudLogs {
go func() {
logger.Debug("Connecting to cloud logs server...")
// TODO replace with another context
if err := cloudConfig.StreamLogsToLogger(context.Background(), logger, refID, 0); err != nil {
if err := cloudConfig.StreamLogsToLogger(globalCtx, logger, refID, 0); err != nil {
logger.WithError(err).Error("error while tailing cloud logs")
}
}()
Expand All @@ -298,24 +313,17 @@ This will execute the test on the k6 cloud service. Use "k6 login cloud" to auth
if progressErr == nil {
if (newTestProgress.RunStatus > lib.RunStatusRunning) ||
(exitOnRunning && newTestProgress.RunStatus == lib.RunStatusRunning) {
shouldExitLoop = true
globalCancel()
break runningLoop
}
testProgressLock.Lock()
testProgress = newTestProgress
testProgressLock.Unlock()
} else {
logger.WithError(progressErr).Error("Test progress error")
}
if shouldExitLoop {
break runningLoop
}
case sig := <-sigC:
logger.WithField("sig", sig).Print("Exiting in response to signal...")
err := client.StopCloudTestRun(refID)
if err != nil {
logger.WithError(err).Error("Stop cloud test error")
}
shouldExitLoop = true // Exit after the next GetTestProgress call
case <-globalCtx.Done():
break runningLoop
}
}

Expand Down

0 comments on commit b3c22ae

Please sign in to comment.