timetable-mgr/vstp/subscribe.go

64 lines
1.2 KiB
Go
Raw Normal View History

2023-07-19 21:31:00 +01:00
package vstp
import (
"time"
"git.fjla.uk/owlboard/timetable-mgr/log"
"git.fjla.uk/owlboard/timetable-mgr/messaging"
2023-07-19 21:31:00 +01:00
"github.com/go-stomp/stomp/v3"
"go.uber.org/zap"
2023-07-19 21:31:00 +01:00
)
func Subscribe() {
var sub *stomp.Subscription
var err error
retryCount := 0
maxRetries := 5
for retryCount < maxRetries {
sub, err = messaging.Client.Subscribe("/topic/VSTP_ALL", stomp.AckAuto)
if err != nil {
log.Warn("Unable to start subscription", zap.Error(err))
time.Sleep(10 * time.Second)
retryCount++
continue
}
break
2023-07-19 21:31:00 +01:00
}
if sub == nil {
log.Fatal("Failed to subscribe to VSTP topic", zap.Int("attempts", maxRetries))
}
2024-04-14 19:03:13 +01:00
log.Info("Subscription to VSTP topic successful, listening")
2023-07-19 21:31:00 +01:00
go func() {
for {
func() {
defer func() {
if r := recover(); r != nil {
log.Warn("VSTP Message Handler Stopped, waiting for recovery")
time.Sleep(time.Second * 10)
}
}()
log.Info("VSTP Message handler started")
for {
msg := <-sub.C
if msg.Err != nil {
2024-04-26 20:48:17 +01:00
log.Error("VSTP Message Error", zap.Error(msg.Err))
} else {
if msg != nil {
2024-10-24 11:36:45 +01:00
log.Debug("VSTP Message Received")
handle(msg)
} else {
2024-04-26 20:48:17 +01:00
log.Info("VSTP Message Empty")
}
}
}
}()
2023-07-19 21:31:00 +01:00
}
}()
select {}
}