Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Immediately shutdown k6 cloud on second sig-c signal #1647

Merged
merged 6 commits into from
Nov 2, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -245,6 +248,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 @@ -284,12 +300,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 @@ -303,24 +318,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()
theerapatcha marked this conversation as resolved.
Show resolved Hide resolved
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