Better Stop()
Vladimir Bauer
9 years ago
| 0 | 0 | package mpb |
| 1 | 1 | |
| 2 | 2 | import ( |
| 3 | "errors" | |
| 4 | 3 | "fmt" |
| 5 | 4 | "io" |
| 6 | 5 | "os" |
| 11 | 10 | |
| 12 | 11 | "github.com/vbauerster/mpb/cwriter" |
| 13 | 12 | ) |
| 14 | ||
| 15 | // ErrCallAfterStop thrown by panic, if Progress methods like (*Progress).AddBar() | |
| 16 | // are called after (*Progress).Stop() has been called | |
| 17 | var ErrCallAfterStop = errors.New("method call on stopped Progress instance") | |
| 18 | 13 | |
| 19 | 14 | // default RefreshRate |
| 20 | 15 | var rr = 100 * time.Millisecond |
| 67 | 62 | // WaitGroup for internal rendering sync |
| 68 | 63 | wg *sync.WaitGroup |
| 69 | 64 | |
| 70 | done chan struct{} | |
| 71 | userConfCh chan userConf | |
| 72 | bCommandCh chan *bCommandData | |
| 73 | barCountCh chan int | |
| 74 | beforeStopCh chan struct{} | |
| 65 | done chan struct{} | |
| 66 | userConfCh chan userConf | |
| 67 | bCommandCh chan *bCommandData | |
| 68 | barCountCh chan int | |
| 69 | stopReqCh chan struct{} | |
| 75 | 70 | |
| 76 | 71 | // follawing is used after (*Progress.done) is closed |
| 77 | 72 | conf userConf |
| 82 | 77 | // If you don't plan to cancel, it is safe to feed with nil |
| 83 | 78 | func New() *Progress { |
| 84 | 79 | p := &Progress{ |
| 85 | wg: new(sync.WaitGroup), | |
| 86 | done: make(chan struct{}), | |
| 87 | userConfCh: make(chan userConf), | |
| 88 | bCommandCh: make(chan *bCommandData), | |
| 89 | barCountCh: make(chan int), | |
| 90 | beforeStopCh: make(chan struct{}), | |
| 80 | wg: new(sync.WaitGroup), | |
| 81 | done: make(chan struct{}), | |
| 82 | userConfCh: make(chan userConf), | |
| 83 | bCommandCh: make(chan *bCommandData), | |
| 84 | barCountCh: make(chan int), | |
| 85 | stopReqCh: make(chan struct{}), | |
| 91 | 86 | } |
| 92 | 87 | go p.server(userConf{ |
| 93 | 88 | width: pwidth, |
| 212 | 207 | // 100 %. It is NOT for cancelation. Use WithContext or WithCancel for |
| 213 | 208 | // cancelation purposes. |
| 214 | 209 | func (p *Progress) Stop() { |
| 215 | if isClosed(p.done) { | |
| 210 | select { | |
| 211 | case <-p.done: | |
| 216 | 212 | return |
| 217 | } | |
| 218 | p.wg.Wait() | |
| 219 | ||
| 220 | p.beforeStopCh <- struct{}{} | |
| 221 | // wait for p.server to quit | |
| 222 | <-p.done | |
| 213 | default: | |
| 214 | p.wg.Wait() // wait for all bars to quit | |
| 215 | p.stopReqCh <- struct{}{} | |
| 216 | <-p.done // wait for p.server to quit | |
| 217 | } | |
| 223 | 218 | } |
| 224 | 219 | |
| 225 | 220 | func (p *Progress) getConf() userConf { |
| 288 | 283 | } |
| 289 | 284 | case p.barCountCh <- len(bars): |
| 290 | 285 | case <-conf.ticker.C: |
| 291 | if conf.cancel != nil && isClosed(conf.cancel) { | |
| 286 | // stop ticking if cancel requested | |
| 287 | select { | |
| 288 | case <-conf.cancel: | |
| 292 | 289 | conf.ticker.Stop() |
| 293 | 290 | break |
| 291 | default: | |
| 294 | 292 | } |
| 295 | 293 | |
| 296 | 294 | numBars := len(bars) |
| 329 | 327 | for _, b := range bars { |
| 330 | 328 | b.flushed() |
| 331 | 329 | } |
| 332 | case <-p.beforeStopCh: | |
| 330 | case <-p.stopReqCh: | |
| 333 | 331 | for _, b := range bars { |
| 334 | 332 | if b.GetStatistics().Total <= 0 { |
| 335 | fmt.Println("completing the bar: ", b) | |
| 336 | 333 | b.Completed() |
| 337 | 334 | } |
| 338 | 335 | } |
| 391 | 388 | return ch |
| 392 | 389 | } |
| 393 | 390 | |
| 394 | // isClosed check if ch closed | |
| 395 | // caution see: http://www.tapirgames.com/blog/golang-channel-closing | |
| 396 | func isClosed(ch <-chan struct{}) bool { | |
| 397 | select { | |
| 398 | case <-ch: | |
| 399 | return true | |
| 400 | default: | |
| 401 | return false | |
| 402 | } | |
| 403 | } | |
| 404 | ||
| 405 | 391 | func max(slice []int) int { |
| 406 | 392 | max := slice[0] |
| 407 | 393 | |