SetOrder and Abort on bar side
Vladimir Bauer
7 years ago
| 19 | 19 | defer cancel() |
| 20 | 20 | |
| 21 | 21 | var wg sync.WaitGroup |
| 22 | p := mpb.New( | |
| 23 | mpb.WithWaitGroup(&wg), | |
| 24 | mpb.WithContext(ctx), | |
| 25 | ) | |
| 22 | p := mpb.NewWithContext(ctx, mpb.WithWaitGroup(&wg)) | |
| 26 | 23 | total := 300 |
| 27 | 24 | numBars := 3 |
| 28 | 25 | wg.Add(numBars) |
| 35 | 32 | decor.EwmaETA(decor.ET_STYLE_GO, 60, decor.WCSyncSpace), |
| 36 | 33 | ), |
| 37 | 34 | mpb.AppendDecorators( |
| 38 | decor.Percentage(decor.WC{W: 5}), | |
| 35 | // note that OnComplete will not be fired, because of cancel | |
| 36 | decor.OnComplete(decor.Percentage(decor.WC{W: 5}), "done"), | |
| 39 | 37 | ), |
| 40 | 38 | ) |
| 41 | 39 | |
| 33 | 33 | go func() { |
| 34 | 34 | defer wg.Done() |
| 35 | 35 | max := 100 * time.Millisecond |
| 36 | for i := 0; i < total; i++ { | |
| 36 | for i := 0; !b.Completed(); i++ { | |
| 37 | 37 | start := time.Now() |
| 38 | if b.ID() == 2 && i == 42 { | |
| 39 | p.Abort(b, true) | |
| 40 | return | |
| 38 | if b.ID() == 2 && i >= 42 { | |
| 39 | // aborting and removing while bar is running | |
| 40 | b.Abort(true) | |
| 41 | 41 | } |
| 42 | 42 | time.Sleep(time.Duration(rand.Intn(10)+1) * max / 10) |
| 43 | 43 | // ewma based decorators require work duration measurement |
| 41 | 41 | frameCh chan io.Reader |
| 42 | 42 | syncTableCh chan [][]chan int |
| 43 | 43 | completed chan bool |
| 44 | forceRefresh chan<- time.Time | |
| 45 | ||
| 46 | // shutdown is closed when bar completed and flushed | |
| 47 | shutdown chan struct{} | |
| 48 | // done is closed after cacheState is written | |
| 44 | ||
| 45 | // concel is called either by user or on complete event | |
| 46 | cancel func() | |
| 47 | // done is closed after cacheState is assigned | |
| 49 | 48 | done chan struct{} |
| 50 | 49 | // cacheState is populated, right after close(shutdown) |
| 51 | 50 | cacheState *bState |
| 55 | 54 | current int64 |
| 56 | 55 | } |
| 57 | 56 | |
| 58 | dlogger *log.Logger | |
| 59 | bpanic interface{} | |
| 57 | container *Progress | |
| 58 | dlogger *log.Logger | |
| 59 | recoveredPanic interface{} | |
| 60 | 60 | } |
| 61 | 61 | |
| 62 | 62 | type bState struct { |
| 83 | 83 | dropOnComplete bool |
| 84 | 84 | // runningBar is a key for *pState.parkedBars |
| 85 | 85 | runningBar *Bar |
| 86 | } | |
| 87 | ||
| 88 | func newBar(ctx context.Context, wg *sync.WaitGroup, bs *bState) *Bar { | |
| 86 | ||
| 87 | debugOut io.Writer | |
| 88 | } | |
| 89 | ||
| 90 | func newBar(container *Progress, bs *bState) *Bar { | |
| 89 | 91 | |
| 90 | 92 | bs.bufP = bytes.NewBuffer(make([]byte, 0, bs.width)) |
| 91 | 93 | bs.bufB = bytes.NewBuffer(make([]byte, 0, bs.width)) |
| 94 | 96 | bs.bufE = bytes.NewBuffer(make([]byte, 0, bs.width)) |
| 95 | 97 | } |
| 96 | 98 | |
| 99 | logPrefix := fmt.Sprintf("%sbar#%02d ", container.dlogger.Prefix(), bs.id) | |
| 100 | ctx, cancel := context.WithCancel(container.ctx) | |
| 97 | 101 | bar := &Bar{ |
| 102 | container: container, | |
| 98 | 103 | priority: bs.priority, |
| 99 | 104 | dropOnComplete: bs.dropOnComplete, |
| 100 | 105 | operateState: make(chan func(*bState)), |
| 102 | 107 | syncTableCh: make(chan [][]chan int), |
| 103 | 108 | completed: make(chan bool), |
| 104 | 109 | done: make(chan struct{}), |
| 105 | shutdown: make(chan struct{}), | |
| 106 | } | |
| 107 | ||
| 108 | go bar.serve(ctx, wg, bs) | |
| 110 | cancel: cancel, | |
| 111 | dlogger: log.New(bs.debugOut, logPrefix, log.Lshortfile), | |
| 112 | } | |
| 113 | ||
| 114 | go bar.serve(ctx, bs) | |
| 109 | 115 | return bar |
| 110 | 116 | } |
| 111 | 117 | |
| 221 | 227 | } |
| 222 | 228 | } |
| 223 | 229 | |
| 230 | // SetOrder changes bar's order among multiple bars. Zero is highest | |
| 231 | // priority, i.e. bar will be on top. If you don't need to set order | |
| 232 | // dynamically, better use BarPriority option. | |
| 233 | func (b *Bar) SetOrder(order int) { | |
| 234 | select { | |
| 235 | case <-b.done: | |
| 236 | default: | |
| 237 | b.container.setBarOrder(b, order) | |
| 238 | } | |
| 239 | } | |
| 240 | ||
| 241 | // Abort interrupts bar's running goroutine. Call this, if you'd like | |
| 242 | // to stop/remove bar before completion event. It has no effect after | |
| 243 | // completion event. If drop is true bar will be removed as well. | |
| 244 | func (b *Bar) Abort(drop bool) { | |
| 245 | select { | |
| 246 | case <-b.done: | |
| 247 | default: | |
| 248 | if drop { | |
| 249 | b.container.dropBar(b) | |
| 250 | } | |
| 251 | b.cancel() | |
| 252 | } | |
| 253 | } | |
| 254 | ||
| 224 | 255 | // Completed reports whether the bar is in completed state. |
| 225 | 256 | func (b *Bar) Completed() bool { |
| 226 | // omit select here, because primary usage of the method is for loop | |
| 227 | // condition, like for !bar.Completed() {...} so when toComplete=true | |
| 228 | // it is called once (at which time, the bar is still alive), then | |
| 229 | // quits the loop and never suppose to be called afterwards. | |
| 230 | return <-b.completed | |
| 257 | select { | |
| 258 | case completed := <-b.completed: | |
| 259 | return completed | |
| 260 | case <-b.done: | |
| 261 | return true | |
| 262 | } | |
| 231 | 263 | } |
| 232 | 264 | |
| 233 | 265 | func (b *Bar) wSyncTable() [][]chan int { |
| 239 | 271 | } |
| 240 | 272 | } |
| 241 | 273 | |
| 242 | func (b *Bar) serve(ctx context.Context, wg *sync.WaitGroup, s *bState) { | |
| 243 | defer wg.Done() | |
| 244 | cancel := ctx.Done() | |
| 274 | func (b *Bar) serve(ctx context.Context, s *bState) { | |
| 275 | defer b.container.bwg.Done() | |
| 245 | 276 | for { |
| 246 | 277 | select { |
| 247 | 278 | case op := <-b.operateState: |
| 248 | 279 | op(s) |
| 249 | 280 | case b.completed <- s.toComplete: |
| 250 | case <-cancel: | |
| 251 | s.toComplete = true | |
| 252 | cancel = nil | |
| 253 | case <-b.shutdown: | |
| 281 | case <-ctx.Done(): | |
| 254 | 282 | b.cacheState = s |
| 255 | 283 | close(b.done) |
| 256 | 284 | // Notifying decorators about shutdown event |
| 263 | 291 | } |
| 264 | 292 | |
| 265 | 293 | func (b *Bar) render(tw int) { |
| 266 | if b.bpanic != nil { | |
| 294 | if b.recoveredPanic != nil { | |
| 267 | 295 | b.toShutdown = false |
| 268 | 296 | b.frameCh <- b.panicToFrame(tw) |
| 269 | 297 | return |
| 274 | 302 | // recovering if user defined decorator panics for example |
| 275 | 303 | if p := recover(); p != nil { |
| 276 | 304 | b.dlogger.Println(p) |
| 277 | b.bpanic = p | |
| 305 | b.recoveredPanic = p | |
| 278 | 306 | b.toShutdown = !s.completeFlushed |
| 279 | 307 | b.frameCh <- b.panicToFrame(tw) |
| 280 | 308 | } |
| 306 | 334 | } |
| 307 | 335 | |
| 308 | 336 | func (b *Bar) panicToFrame(termWidth int) io.Reader { |
| 309 | return strings.NewReader(fmt.Sprintf(fmt.Sprintf("%%.%dv\n", termWidth), b.bpanic)) | |
| 337 | return strings.NewReader(fmt.Sprintf(fmt.Sprintf("%%.%dv\n", termWidth), b.recoveredPanic)) | |
| 310 | 338 | } |
| 311 | 339 | |
| 312 | 340 | func (s *bState) draw(termWidth int) io.Reader { |
| 374 | 402 | func (b *Bar) refreshNowTillShutdown() { |
| 375 | 403 | for { |
| 376 | 404 | select { |
| 377 | case b.forceRefresh <- time.Now(): | |
| 378 | case <-b.shutdown: | |
| 405 | case b.container.forceRefresh <- time.Now(): | |
| 406 | case <-b.done: | |
| 379 | 407 | return |
| 380 | 408 | } |
| 381 | 409 | } |
| 49 | 49 | t.Errorf("Expected bar id: %d, got %d\n", wantID, gotID) |
| 50 | 50 | } |
| 51 | 51 | |
| 52 | p.Abort(bar, true) | |
| 52 | bar.Abort(true) | |
| 53 | 53 | p.Wait() |
| 54 | 54 | } |
| 55 | 55 |
| 2 | 2 | import ( |
| 3 | 3 | "container/heap" |
| 4 | 4 | "context" |
| 5 | "fmt" | |
| 6 | 5 | "io" |
| 7 | 6 | "io/ioutil" |
| 8 | 7 | "log" |
| 38 | 37 | heapUpdated bool |
| 39 | 38 | pMatrix map[int][]chan int |
| 40 | 39 | aMatrix map[int][]chan int |
| 41 | barShutdownQueue []chan struct{} | |
| 40 | barShutdownQueue []func() | |
| 42 | 41 | |
| 43 | 42 | // following are provided/overrided by user |
| 44 | 43 | idCount int |
| 125 | 124 | priority: ps.idCount, |
| 126 | 125 | id: ps.idCount, |
| 127 | 126 | width: ps.width, |
| 127 | debugOut: ps.debugOut, | |
| 128 | 128 | } |
| 129 | 129 | for _, opt := range options { |
| 130 | 130 | if opt != nil { |
| 131 | 131 | opt(bs) |
| 132 | 132 | } |
| 133 | 133 | } |
| 134 | bar := newBar(p.ctx, p.bwg, bs) | |
| 135 | bar.forceRefresh = p.forceRefresh | |
| 136 | prefix := fmt.Sprintf("%sbar#%02d ", p.dlogger.Prefix(), bs.id) | |
| 137 | bar.dlogger = log.New(ps.debugOut, prefix, log.Lshortfile) | |
| 134 | bar := newBar(p, bs) | |
| 138 | 135 | if bs.runningBar != nil { |
| 139 | 136 | if bar.priority == ps.idCount { |
| 140 | 137 | bar.priority = bs.runningBar.priority |
| 154 | 151 | } |
| 155 | 152 | } |
| 156 | 153 | |
| 157 | // Abort is only effective while bar progress is running, it means | |
| 158 | // remove bar now without waiting for its completion. If bar is already | |
| 159 | // completed, there is nothing to abort. If you need to remove bar | |
| 160 | // after completion, use BarRemoveOnComplete BarOption. | |
| 161 | func (p *Progress) Abort(b *Bar, remove bool) { | |
| 154 | func (p *Progress) dropBar(b *Bar) { | |
| 162 | 155 | select { |
| 163 | 156 | case p.operateState <- func(s *pState) { |
| 164 | 157 | if b.index < 0 { |
| 165 | 158 | return |
| 166 | 159 | } |
| 167 | if remove { | |
| 168 | s.heapUpdated = heap.Remove(s.bHeap, b.index) != nil | |
| 169 | } | |
| 170 | s.barShutdownQueue = append(s.barShutdownQueue, b.shutdown) | |
| 160 | s.heapUpdated = heap.Remove(s.bHeap, b.index) != nil | |
| 171 | 161 | }: |
| 172 | 162 | case <-p.done: |
| 173 | 163 | } |
| 174 | 164 | } |
| 175 | 165 | |
| 176 | // UpdateBarPriority provides a way to change bar's order position. | |
| 177 | // Zero is highest priority, i.e. bar will be on top. | |
| 166 | // UpdateBarPriority is deprecated. Please use *Bar.SetOrder. | |
| 178 | 167 | func (p *Progress) UpdateBarPriority(b *Bar, priority int) { |
| 168 | p.setBarOrder(b, priority) | |
| 169 | } | |
| 170 | ||
| 171 | func (p *Progress) setBarOrder(b *Bar, order int) { | |
| 179 | 172 | select { |
| 180 | case p.operateState <- func(s *pState) { s.bHeap.update(b, priority) }: | |
| 173 | case p.operateState <- func(s *pState) { s.bHeap.update(b, order) }: | |
| 181 | 174 | case <-p.done: |
| 182 | 175 | } |
| 183 | 176 | } |
| 270 | 263 | // shutdown at next flush, in other words decrement underlying WaitGroup |
| 271 | 264 | // only after the bar with completed state has been flushed. this |
| 272 | 265 | // ensures no bar ends up with less than 100% rendered. |
| 273 | s.barShutdownQueue = append(s.barShutdownQueue, bar.shutdown) | |
| 266 | s.barShutdownQueue = append(s.barShutdownQueue, bar.cancel) | |
| 274 | 267 | if parkedBar := s.parkedBars[bar]; parkedBar != nil { |
| 275 | 268 | heap.Push(s.bHeap, parkedBar) |
| 276 | 269 | s.heapUpdated = true |
| 288 | 281 | } |
| 289 | 282 | |
| 290 | 283 | for i := len(s.barShutdownQueue) - 1; i >= 0; i-- { |
| 291 | close(s.barShutdownQueue[i]) | |
| 284 | s.barShutdownQueue[i]() | |
| 292 | 285 | s.barShutdownQueue = s.barShutdownQueue[:i] |
| 293 | 286 | } |
| 294 | 287 | |
| 37 | 37 | t.Errorf("BarCount want: %q, got: %q\n", 1, count) |
| 38 | 38 | } |
| 39 | 39 | |
| 40 | p.Abort(b, true) | |
| 40 | b.Abort(true) | |
| 41 | 41 | p.Wait() |
| 42 | 42 | } |
| 43 | 43 | |
| 51 | 51 | b := p.AddBar(100) |
| 52 | 52 | bars[i] = b |
| 53 | 53 | go func(n int) { |
| 54 | for i := 0; i < 100; i++ { | |
| 55 | if n == 0 && i == 33 { | |
| 56 | p.Abort(b, true) | |
| 54 | for i := 0; !b.Completed(); i++ { | |
| 55 | if n == 0 && i >= 33 { | |
| 56 | b.Abort(true) | |
| 57 | 57 | wg.Done() |
| 58 | 58 | } |
| 59 | 59 | b.Increment() |
| 67 | 67 | if count != 2 { |
| 68 | 68 | t.Errorf("BarCount want: %q, got: %q\n", 2, count) |
| 69 | 69 | } |
| 70 | p.Abort(bars[1], true) | |
| 71 | p.Abort(bars[2], true) | |
| 70 | bars[1].Abort(true) | |
| 71 | bars[2].Abort(true) | |
| 72 | 72 | p.Wait() |
| 73 | 73 | } |
| 74 | 74 | |