Skip to content

Commit

Permalink
Fix usage report block of k6 execution if it takes longer than 3s (gr…
Browse files Browse the repository at this point in the history
…afana#2938)

(cherry picked from commit 4463298)
  • Loading branch information
na-- committed Feb 27, 2023
1 parent eddb5a6 commit f8e7459
Showing 1 changed file with 8 additions and 13 deletions.
21 changes: 8 additions & 13 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,22 +306,17 @@ func (c *cmdRun) run(cmd *cobra.Command, args []string) (err error) {
// Init has passed successfully, so unless disabled, make sure we send a
// usage report after the context is done.
if !conf.NoUsageReport.Bool {
backgroundProcesses.Add(2)
reportCtx, reportCancel := context.WithCancel(globalCtx)
reportDone := make(chan error)
backgroundProcesses.Add(1)
go func() {
defer backgroundProcesses.Done()
reportCtx, reportCancel := context.WithTimeout(globalCtx, 3*time.Second)
defer reportCancel()
logger.Debug("Sending usage report...")
reportDone <- reportUsage(reportCtx, execScheduler)
close(reportDone)
backgroundProcesses.Done()
}()
go func() {
select {
case <-reportDone:
case <-time.After(3 * time.Second):
if rerr := reportUsage(reportCtx, execScheduler); rerr != nil {
logger.WithError(rerr).Debug("Error sending usage report")
} else {
logger.Debug("Usage report sent successfully")
}
reportCancel()
backgroundProcesses.Done()
}()
}

Expand Down

0 comments on commit f8e7459

Please sign in to comment.