Examples refactoring: startBlock to sbEta
Vladimir Bauer
8 years ago
| 31 | 31 | |
| 32 | 32 | for i := 0; i < numBars; i++ { |
| 33 | 33 | name := fmt.Sprintf("Bar#%d:", i) |
| 34 | startBlock := make(chan time.Time) | |
| 34 | sbEta := make(chan time.Time) | |
| 35 | 35 | bar := p.AddBar(int64(total), |
| 36 | 36 | mpb.PrependDecorators( |
| 37 | 37 | decor.Name(name), |
| 38 | decor.ETA(decor.ET_STYLE_GO, 0, startBlock, decor.WCSyncSpace), | |
| 38 | decor.ETA(decor.ET_STYLE_GO, 60, sbEta, decor.WCSyncSpace), | |
| 39 | 39 | ), |
| 40 | 40 | mpb.AppendDecorators( |
| 41 | 41 | decor.Percentage(decor.WC{W: 5}), |
| 46 | 46 | defer wg.Done() |
| 47 | 47 | max := 100 * time.Millisecond |
| 48 | 48 | for !bar.Completed() { |
| 49 | startBlock <- time.Now() | |
| 49 | sbEta <- time.Now() | |
| 50 | 50 | time.Sleep(time.Duration(rand.Intn(10)+1) * max / 10) |
| 51 | 51 | bar.Increment() |
| 52 | 52 | } |
| 43 | 43 | doneWg.Add(1) |
| 44 | 44 | i := i |
| 45 | 45 | go func() { |
| 46 | startBlock := make(chan time.Time) | |
| 46 | sbEta := make(chan time.Time) | |
| 47 | 47 | task := fmt.Sprintf("Task#%02d:", i) |
| 48 | 48 | job := "installing" |
| 49 | 49 | // preparing delayed bars |
| 53 | 53 | mpb.PrependDecorators( |
| 54 | 54 | decor.Name(task, decor.WC{W: len(task) + 1, C: decor.DidentRight}), |
| 55 | 55 | decor.OnComplete(decor.Name(job, decor.WCSyncSpaceR), "done!", decor.WCSyncSpaceR), |
| 56 | decor.OnComplete(decor.ETA(decor.ET_STYLE_MMSS, 60, startBlock, decor.WCSyncWidth), "", decor.WCSyncSpace), | |
| 56 | decor.OnComplete(decor.ETA(decor.ET_STYLE_MMSS, 60, sbEta, decor.WCSyncWidth), "", decor.WCSyncSpace), | |
| 57 | 57 | ), |
| 58 | 58 | mpb.AppendDecorators( |
| 59 | 59 | decor.OnComplete(decor.Percentage(decor.WC{W: 5}), ""), |
| 61 | 61 | ) |
| 62 | 62 | // waiting for download to complete, before starting install job |
| 63 | 63 | downloadWgg[i].Wait() |
| 64 | go newTask(doneWg, b, numBars-i, startBlock) | |
| 64 | go newTask(doneWg, b, numBars-i, sbEta) | |
| 65 | 65 | }() |
| 66 | 66 | } |
| 67 | 67 | |
| 68 | 68 | p.Wait() |
| 69 | 69 | } |
| 70 | 70 | |
| 71 | func newTask(wg *sync.WaitGroup, b *mpb.Bar, incrBy int, startBlock chan<- time.Time) { | |
| 71 | func newTask(wg *sync.WaitGroup, b *mpb.Bar, incrBy int, sbCh chan<- time.Time) { | |
| 72 | 72 | defer wg.Done() |
| 73 | 73 | max := 100 * time.Millisecond |
| 74 | 74 | for !b.Completed() { |
| 75 | if startBlock != nil { | |
| 76 | startBlock <- time.Now() | |
| 75 | if sbCh != nil { | |
| 76 | sbCh <- time.Now() | |
| 77 | 77 | } |
| 78 | 78 | time.Sleep(time.Duration(rand.Intn(10)+1) * max / 10) |
| 79 | 79 | b.IncrBy(incrBy) |
| 25 | 25 | if i != 1 { |
| 26 | 26 | name = fmt.Sprintf("Bar#%d:", i) |
| 27 | 27 | } |
| 28 | startBlock := make(chan time.Time) | |
| 28 | sbEta := make(chan time.Time) | |
| 29 | 29 | b := p.AddBar(int64(total), |
| 30 | 30 | mpb.PrependDecorators( |
| 31 | 31 | decor.Name(name, decor.WCSyncWidth), |
| 32 | 32 | decor.OnComplete( |
| 33 | decor.ETA(decor.ET_STYLE_MMSS, 60, startBlock, decor.WC{W: 6}), | |
| 33 | decor.ETA(decor.ET_STYLE_MMSS, 60, sbEta, decor.WC{W: 6}), | |
| 34 | 34 | "Done", |
| 35 | 35 | decor.WCSyncSpace, |
| 36 | 36 | ), |
| 43 | 43 | defer wg.Done() |
| 44 | 44 | max := 100 * time.Millisecond |
| 45 | 45 | for i := 0; i < total; i++ { |
| 46 | startBlock <- time.Now() | |
| 46 | sbEta <- time.Now() | |
| 47 | 47 | time.Sleep(time.Duration(rand.Intn(10)+1) * max / 10) |
| 48 | 48 | b.Increment() |
| 49 | 49 | } |
| 28 | 28 | bOption = mpb.BarRemoveOnComplete() |
| 29 | 29 | } |
| 30 | 30 | |
| 31 | startBlock := make(chan time.Time) | |
| 31 | sbEta := make(chan time.Time) | |
| 32 | 32 | b := p.AddBar(int64(total), mpb.BarID(i), |
| 33 | 33 | bOption, |
| 34 | 34 | mpb.PrependDecorators( |
| 35 | 35 | decor.Name(name), |
| 36 | decor.ETA(decor.ET_STYLE_GO, 0, startBlock, decor.WCSyncSpace), | |
| 36 | decor.ETA(decor.ET_STYLE_GO, 60, sbEta, decor.WCSyncSpace), | |
| 37 | 37 | ), |
| 38 | mpb.AppendDecorators( | |
| 39 | decor.Percentage(), | |
| 40 | ), | |
| 38 | mpb.AppendDecorators(decor.Percentage()), | |
| 41 | 39 | ) |
| 42 | 40 | go func() { |
| 43 | 41 | defer wg.Done() |
| 44 | 42 | max := 100 * time.Millisecond |
| 45 | 43 | for i := 0; i < total; i++ { |
| 46 | startBlock <- time.Now() | |
| 44 | sbEta <- time.Now() | |
| 47 | 45 | if b.ID() == 2 && i == 42 { |
| 48 | 46 | p.Abort(b) |
| 49 | 47 | return |
| 25 | 25 | if i != 1 { |
| 26 | 26 | name = fmt.Sprintf("Bar#%d:", i) |
| 27 | 27 | } |
| 28 | startBlock := make(chan time.Time) | |
| 28 | sbEta := make(chan time.Time) | |
| 29 | 29 | b := p.AddBar(int64(total), |
| 30 | 30 | mpb.PrependDecorators( |
| 31 | 31 | decor.Name(name, decor.WCSyncWidth), |
| 32 | 32 | decor.CountersNoUnit("%d / %d", decor.WCSyncSpace), |
| 33 | 33 | ), |
| 34 | 34 | mpb.AppendDecorators( |
| 35 | decor.ETA(decor.ET_STYLE_GO, 0, startBlock, decor.WC{W: 3}), | |
| 35 | decor.ETA(decor.ET_STYLE_GO, 60, sbEta, decor.WC{W: 3}), | |
| 36 | 36 | ), |
| 37 | 37 | ) |
| 38 | 38 | go func() { |
| 39 | 39 | defer wg.Done() |
| 40 | 40 | max := 100 * time.Millisecond |
| 41 | 41 | for i := 0; i < total; i++ { |
| 42 | startBlock <- time.Now() | |
| 42 | sbEta <- time.Now() | |
| 43 | 43 | time.Sleep(time.Duration(rand.Intn(10)+1) * max / 10) |
| 44 | 44 | if i&1 == 1 { |
| 45 | 45 | priority := total - int(b.Current()) |
| 26 | 26 | for i := 0; i < totalBars; i++ { |
| 27 | 27 | name := fmt.Sprintf("Bar#%02d: ", i) |
| 28 | 28 | total := rand.Intn(320) + 10 |
| 29 | startBlock := make(chan time.Time) | |
| 29 | sbEta := make(chan time.Time) | |
| 30 | 30 | bar := p.AddBar(int64(total), |
| 31 | 31 | mpb.PrependDecorators( |
| 32 | 32 | decor.Name(name), |
| 33 | decor.ETA(decor.ET_STYLE_GO, 60, startBlock, decor.WCSyncSpace), | |
| 33 | decor.ETA(decor.ET_STYLE_GO, 60, sbEta, decor.WCSyncSpace), | |
| 34 | 34 | ), |
| 35 | 35 | mpb.AppendDecorators( |
| 36 | 36 | decor.Percentage(decor.WC{W: 5}), |
| 41 | 41 | defer wg.Done() |
| 42 | 42 | max := 100 * time.Millisecond |
| 43 | 43 | for !bar.Completed() { |
| 44 | startBlock <- time.Now() | |
| 44 | sbEta <- time.Now() | |
| 45 | 45 | time.Sleep(time.Duration(rand.Intn(10)+1) * max / 10) |
| 46 | 46 | bar.Increment() |
| 47 | 47 | } |