-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
61 lines (48 loc) · 1.41 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
package main
import (
"context"
"fmt"
"os"
"os/signal"
"syscall"
"time"
"github.com/bilal-bhatti/async-event-bus/concur"
"github.com/bilal-bhatti/async-event-bus/events"
"github.com/bilal-bhatti/async-event-bus/handlers"
)
func main() {
bus, err := concur.NewBus()
if err != nil {
panic(err)
}
bus.RegisterTopics("do.something", "do.print", "do.construct")
bus.RegisterHandler("do.something.worker", handlers.DoSomething, "do.something")
bus.RegisterHandler("do.print.worker", handlers.DoPrint, "do.print")
constructor := handlers.NewConstruct("dbstring", "awsstring")
bus.RegisterHandler("do.construct.worker", constructor.Handle(), "do.construct")
done := make(chan bool, 1)
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt, syscall.SIGTERM)
go gracefulShutdown(bus, quit, done)
ctx := context.Background()
for i := 0; i < 15; i++ {
e := events.NewSomething()
fmt.Printf("emit: %s\n", e.ToString())
err := bus.Emit(ctx, "do.something", e)
if err != nil {
panic(err)
}
}
<-done
fmt.Println("shutdown complete")
}
func gracefulShutdown(bus *concur.AsyncBus, quit <-chan os.Signal, done chan<- bool) {
<-quit
fmt.Println("shutdown signal received")
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := bus.Shutdown(ctx); err != nil {
fmt.Printf("could not gracefully shutdown the server: %v\n", err)
}
close(done)
}