2023-07-19 21:31:00 +01:00
|
|
|
package vstp
|
|
|
|
|
|
|
|
import (
|
2023-07-21 12:12:16 +01:00
|
|
|
"time"
|
|
|
|
|
2024-03-25 11:26:07 +00:00
|
|
|
"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"
|
2024-04-24 23:09:13 +01:00
|
|
|
"go.uber.org/zap"
|
2023-07-19 21:31:00 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func Subscribe() {
|
2024-04-24 23:09:13 +01:00
|
|
|
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
|
|
|
}
|
2024-04-24 23:09:13 +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 {
|
2024-04-24 23:09:13 +01:00
|
|
|
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))
|
2024-04-24 23:09:13 +01:00
|
|
|
} else {
|
|
|
|
if msg != nil {
|
2024-10-24 11:36:45 +01:00
|
|
|
log.Debug("VSTP Message Received")
|
2024-04-24 23:09:13 +01:00
|
|
|
handle(msg)
|
|
|
|
} else {
|
2024-04-26 20:48:17 +01:00
|
|
|
log.Info("VSTP Message Empty")
|
2024-04-24 23:09:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2023-07-19 21:31:00 +01:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {}
|
|
|
|
}
|