-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
68 lines (56 loc) · 1.34 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package main
import (
"bus-sample-project/calculator"
"bus-sample-project/config"
"bus-sample-project/counter"
"bus-sample-project/models"
"bus-sample-project/printer"
"context"
"fmt"
"math/rand"
"sync"
"github.com/mustafaturan/bus/v3"
)
func main() {
config.Init()
var wg sync.WaitGroup
defer wg.Wait()
// register the event printer handler (synchronous handler)
printer.Start()
defer printer.Stop()
// register the event counter handler (asynchronous handler)
counter.Start(&wg)
defer counter.Stop()
// register the event calculator handler (asynchronous handler)
calculator.Start(&wg)
defer calculator.Stop()
txID := config.Monoton.Next()
ctx := context.Background()
ctx = context.WithValue(ctx, bus.CtxKeyTxID, txID)
b := config.Bus
for i := 0; i < 3; i++ {
err := b.Emit(
ctx,
"order.created",
models.Order{Name: fmt.Sprintf("Product #%d", i), Amount: randomAmount()},
)
if err != nil {
fmt.Println("ERROR >>>>", err)
}
}
// if the txID is not available on the context and bus package sets it
ctx = context.Background()
err := b.Emit(
ctx, // context
"order.canceled", // topic
models.Order{Name: "Product #N", Amount: randomAmount()}, // data
)
if err != nil {
fmt.Println("ERROR >>>>", err)
}
}
func randomAmount() int {
max := 100
min := 10
return rand.Intn(max-min) + min
}