-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
61 lines (53 loc) · 1.36 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"
"log/slog"
"os"
"os/signal"
"sync"
"syscall"
"github.com/libops/isle-event-bus/internal/config"
"github.com/libops/isle-event-bus/internal/stomp"
)
func main() {
config, err := config.ReadConfig("isle-event-bus.yaml")
if err != nil {
slog.Error("Could not read YML", "err", err)
os.Exit(1)
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
stopChan := make(chan os.Signal, 1)
signal.Notify(stopChan, os.Interrupt, syscall.SIGTERM)
var wg sync.WaitGroup
for _, q := range config.Queues {
if q.Disabled {
slog.Info("Skipping disabled subscriber", "queue", q.Name)
continue
}
numConsumers := q.Consumers
for i := range numConsumers {
wg.Add(1)
go func(ctx context.Context, q stomp.Queue, consumerID int) {
defer wg.Done()
slog.Info("Starting subscriber", "queue", q.Name, "consumer", consumerID)
for {
select {
case <-stopChan:
slog.Info("Stopping subscriber", "queue", q.Name, "consumer", consumerID)
return
default:
err := q.RecvAndProcessMessage(ctx)
if err != nil {
slog.Error("Error processing message", "queue", q.Name, "consumer", consumerID, "error", err)
}
}
}
}(ctx, q, i)
}
}
<-stopChan
slog.Info("All stomp subscribers are now running")
wg.Wait()
slog.Info("All stomp subscribers have stopped")
}