Skip to content

Commit

Permalink
Merge pull request #18 from winebarrel/wait_progress_close
Browse files Browse the repository at this point in the history
Wait progress close
  • Loading branch information
winebarrel authored Nov 11, 2023
2 parents 6435e58 + 2cc283d commit 54640ca
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
7 changes: 5 additions & 2 deletions progress.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type Progress struct {
noop bool
prevDPLen int
nDeadAgents int32
closed chan struct{}
}

func NewProgress(w io.Writer, noop bool) *Progress {
Expand All @@ -36,6 +37,7 @@ func (progress *Progress) Start(ctx context.Context, rec *Recorder) {
return
}

progress.closed = make(chan struct{})
tk := time.NewTicker(InterimReportIntvl)

go func() {
Expand All @@ -50,7 +52,7 @@ func (progress *Progress) Start(ctx context.Context, rec *Recorder) {
}
}

progress.clear()
close(progress.closed)
}()
}

Expand Down Expand Up @@ -86,11 +88,12 @@ func (progress *Progress) report(rec *Recorder) {
fmt.Fprintf(progress.w, "\r%-*s", width, line)
}

func (progress *Progress) clear() {
func (progress *Progress) Close() {
if progress.noop {
return
}

<-progress.closed
width, _, err := term.GetSize(0)

if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion recorder.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,14 @@ func NewRecorder(id string, options *Options) *Recorder {
ID: id,
DataPoints: []DataPoint{},
ch: make(chan []DataPoint, options.Nagents*3),
closed: make(chan struct{}),
}

return rec
}

func (rec *Recorder) Start() {
rec.closed = make(chan struct{})

push := func(dps []DataPoint) {
rec.Lock()
defer rec.Unlock()
Expand Down
3 changes: 2 additions & 1 deletion task.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ func (task *Task) Run() (*Report, error) {
progress.Start(ctx, rec)
err = eg.Wait()
cancel()
rec.Close() // wait for buffer flush
progress.Close() // wait for ticker to stop
rec.Close() // wait for buffer flush

if err != nil && !errors.Is(err, context.DeadlineExceeded) {
return nil, err
Expand Down

0 comments on commit 54640ca

Please sign in to comment.