Codebase list golang-github-vbauerster-mpb / 607ab1d
Request totalUnknownBarStopReqCh before stop Vladimir Bauer 9 years ago
1 changed file(s) with 24 addition(s) and 19 deletion(s). Raw diff Collapse all Expand all
33 "fmt"
44 "io"
55 "os"
6 "runtime"
76 "sync"
87 "time"
98 "unicode/utf8"
6261 // WaitGroup for internal rendering sync
6362 wg *sync.WaitGroup
6463
65 done chan struct{}
66 userConfCh chan userConf
67 bCommandCh chan *bCommandData
68 barCountCh chan int
69 stopReqCh chan struct{}
64 done chan struct{}
65 userConfCh chan userConf
66 bCommandCh chan *bCommandData
67 barCountCh chan int
68 stopReqCh chan struct{}
69 totalUnknownBarStopReqCh chan struct{}
7070
7171 // follawing is used after (*Progress.done) is closed
7272 conf userConf
7777 // If you don't plan to cancel, it is safe to feed with nil
7878 func New() *Progress {
7979 p := &Progress{
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{}),
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{}),
86 totalUnknownBarStopReqCh: make(chan struct{}),
8687 }
8788 go p.server(userConf{
8889 width: pwidth,
211212 case <-p.done:
212213 return
213214 default:
214 p.wg.Wait() // wait for all bars to quit
215 // complet Total unknown bars
216 p.totalUnknownBarStopReqCh <- struct{}{}
217 // wait for all bars to quit
218 p.wg.Wait()
219 // stop request
215220 p.stopReqCh <- struct{}{}
216 <-p.done // wait for p.server to quit
221 // wait for p.server to quit
222 <-p.done
217223 }
218224 }
219225
240246 func (p *Progress) server(conf userConf) {
241247
242248 defer func() {
249 conf.ticker.Stop()
250 conf.cw.Flush()
243251 p.conf = conf
244 conf.ticker.Stop()
245252 if conf.shutdownNotifier != nil {
246253 close(conf.shutdownNotifier)
247254 }
250257
251258 recoverFn := func(ch chan []byte) {
252259 if p := recover(); p != nil {
253 var buf [4096]byte
254 n := runtime.Stack(buf[:], false)
255 os.Stderr.Write(buf[:n])
256260 ch <- []byte(fmt.Sprintln(p))
257261 }
258262 close(ch)
328332 for _, b := range bars {
329333 b.flushed()
330334 }
331 case <-p.stopReqCh:
335 case <-p.totalUnknownBarStopReqCh:
332336 for _, b := range bars {
333337 if b.GetStatistics().Total <= 0 {
334338 b.Completed()
335339 }
336340 }
341 case <-p.stopReqCh:
337342 return
338343 }
339344 }