remove panic example
Vladimir Bauer
3 years ago
| 0 | |
module github.com/vbauerster/mpb/_examples/panic
|
| 1 | |
|
| 2 | |
go 1.17
|
| 3 | |
|
| 4 | |
require github.com/vbauerster/mpb/v8 v8.0.2
|
| 5 | |
|
| 6 | |
require (
|
| 7 | |
github.com/VividCortex/ewma v1.2.0 // indirect
|
| 8 | |
github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d // indirect
|
| 9 | |
github.com/mattn/go-runewidth v0.0.13 // indirect
|
| 10 | |
github.com/rivo/uniseg v0.2.0 // indirect
|
| 11 | |
golang.org/x/sys v0.0.0-20220829200755-d48e67d00261 // indirect
|
| 12 | |
)
|
| 0 | |
package main
|
| 1 | |
|
| 2 | |
import (
|
| 3 | |
"fmt"
|
| 4 | |
"os"
|
| 5 | |
"strings"
|
| 6 | |
"sync"
|
| 7 | |
"time"
|
| 8 | |
|
| 9 | |
"github.com/vbauerster/mpb/v8"
|
| 10 | |
"github.com/vbauerster/mpb/v8/decor"
|
| 11 | |
)
|
| 12 | |
|
| 13 | |
func main() {
|
| 14 | |
var wg sync.WaitGroup
|
| 15 | |
// passed wg will be accounted at p.Wait() call
|
| 16 | |
p := mpb.New(
|
| 17 | |
mpb.WithWaitGroup(&wg),
|
| 18 | |
mpb.WithDebugOutput(os.Stderr),
|
| 19 | |
)
|
| 20 | |
|
| 21 | |
wantPanic := strings.Repeat("Panic ", 64)
|
| 22 | |
numBars := 3
|
| 23 | |
wg.Add(numBars)
|
| 24 | |
|
| 25 | |
for i := 0; i < numBars; i++ {
|
| 26 | |
name := fmt.Sprintf("b#%02d:", i)
|
| 27 | |
bar := p.AddBar(100, mpb.BarID(i), mpb.PrependDecorators(panicDecorator(name, wantPanic)))
|
| 28 | |
|
| 29 | |
go func() {
|
| 30 | |
defer wg.Done()
|
| 31 | |
for i := 0; i < 100; i++ {
|
| 32 | |
time.Sleep(50 * time.Millisecond)
|
| 33 | |
bar.Increment()
|
| 34 | |
}
|
| 35 | |
}()
|
| 36 | |
}
|
| 37 | |
// wait for passed wg and for all bars to complete and flush
|
| 38 | |
p.Wait()
|
| 39 | |
}
|
| 40 | |
|
| 41 | |
func panicDecorator(name, panicMsg string) decor.Decorator {
|
| 42 | |
return decor.Any(func(st decor.Statistics) string {
|
| 43 | |
if st.ID == 1 && st.Current >= 42 {
|
| 44 | |
panic(panicMsg)
|
| 45 | |
}
|
| 46 | |
return name
|
| 47 | |
})
|
| 48 | |
}
|