Codebase list golang-github-vbauerster-mpb / 71720ed
BarParkTo always replacing its argument bar. Vladimir Bauer 7 years ago
4 changed file(s) with 29 addition(s) and 22 deletion(s). Raw diff Collapse all Expand all
2727 task := fmt.Sprintf("Task#%02d:", i)
2828 job := "downloading"
2929 b := p.AddBar(rand.Int63n(201)+100,
30 mpb.BarRemoveOnComplete(),
3130 mpb.PrependDecorators(
3231 decor.Name(task, decor.WC{W: len(task) + 1, C: decor.DidentRight}),
3332 decor.Name(job, decor.WCSyncSpaceR),
3434 priority int // used by heap
3535 index int // used by heap
3636
37 extendedLines int
38 toShutdown bool
39 dropOnComplete bool
40 operateState chan func(*bState)
41 frameCh chan io.Reader
42 syncTableCh chan [][]chan int
43 completed chan bool
37 extendedLines int
38 toShutdown bool
39 toDrop bool
40 operateState chan func(*bState)
41 frameCh chan io.Reader
42 syncTableCh chan [][]chan int
43 completed chan bool
4444
4545 // concel is called either by user or on complete event
4646 cancel func()
9999 logPrefix := fmt.Sprintf("%sbar#%02d ", container.dlogger.Prefix(), bs.id)
100100 ctx, cancel := context.WithCancel(container.ctx)
101101 bar := &Bar{
102 container: container,
103 priority: bs.priority,
104 dropOnComplete: bs.dropOnComplete,
105 operateState: make(chan func(*bState)),
106 frameCh: make(chan io.Reader, 1),
107 syncTableCh: make(chan [][]chan int),
108 completed: make(chan bool, 1),
109 done: make(chan struct{}),
110 cancel: cancel,
111 dlogger: log.New(bs.debugOut, logPrefix, log.Lshortfile),
102 container: container,
103 priority: bs.priority,
104 toDrop: bs.dropOnComplete,
105 operateState: make(chan func(*bState)),
106 frameCh: make(chan io.Reader, 1),
107 syncTableCh: make(chan [][]chan int),
108 completed: make(chan bool, 1),
109 done: make(chan struct{}),
110 cancel: cancel,
111 dlogger: log.New(bs.debugOut, logPrefix, log.Lshortfile),
112112 }
113113
114114 go bar.serve(ctx, bs)
315315 frame = io.MultiReader(frame, s.bufE)
316316 }
317317
318 b.toDrop = s.dropOnComplete
318319 b.toShutdown = s.toComplete && !s.completeFlushed
319320 s.completeFlushed = s.toComplete
320321
408409 }
409410 }
410411
412 func (b *Bar) dropOnComplete() {
413 select {
414 case b.operateState <- func(s *bState) { s.dropOnComplete = true }:
415 case <-b.done:
416 }
417 }
418
411419 func newStatistics(s *bState) *decor.Statistics {
412420 return &decor.Statistics{
413421 ID: s.id,
6666
6767 // BarParkTo parks constructed bar into the runningBar. In other words,
6868 // constructed bar will start only after runningBar has been completed.
69 // Parked bar will replace runningBar if BarRemoveOnComplete option
70 // is set on the runningBar. Parked bar inherits priority of the
71 // runningBar, if no BarPriority option is set.
69 // Parked bar inherits priority of the runningBar, if no BarPriority
70 // option is set. Parked bar will replace runningBar eventually.
7271 func BarParkTo(runningBar *Bar) BarOption {
72 runningBar.dropOnComplete()
7373 return func(s *bState) {
7474 s.runningBar = runningBar
7575 }
269269 s.heapUpdated = true
270270 delete(s.parkedBars, bar)
271271 }
272 if bar.dropOnComplete {
272 if bar.toDrop {
273273 s.heapUpdated = true
274274 return
275275 }