timetable-extension #1

Open
fred.boniface wants to merge 160 commits from timetable-extension into main
Showing only changes of commit 5b9c444ac5 - Show all commits

View File

@ -1,38 +1,64 @@
package vstp package vstp
import ( import (
"fmt"
"time" "time"
"git.fjla.uk/owlboard/timetable-mgr/log" "git.fjla.uk/owlboard/timetable-mgr/log"
"git.fjla.uk/owlboard/timetable-mgr/messaging" "git.fjla.uk/owlboard/timetable-mgr/messaging"
"github.com/go-stomp/stomp/v3" "github.com/go-stomp/stomp/v3"
"go.uber.org/zap"
) )
func Subscribe() { func Subscribe() {
sub, err := messaging.Client.Subscribe("/topic/VSTP_ALL", stomp.AckAuto) 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 { if err != nil {
log.Fatal("Unable to start subscription: " + err.Error()) log.Warn("Unable to start subscription", zap.Error(err))
time.Sleep(10 * time.Second)
retryCount++
continue
} }
break
}
if sub == nil {
log.Fatal("Failed to subscribe to VSTP topic", zap.Int("attempts", maxRetries))
}
log.Info("Subscription to VSTP topic successful, listening") log.Info("Subscription to VSTP topic successful, listening")
go func() { go func() {
log.Debug("GOROUTINE: VSTP Message Handler Started") for {
func() {
defer func() { defer func() {
if r := recover(); r != nil { if r := recover(); r != nil {
log.Warn("GOROUTINE: VSTP Message Handler Stopped") log.Warn("VSTP Message Handler Stopped, waiting for recovery")
time.Sleep(time.Second * 10) time.Sleep(time.Second * 10)
log.Fatal("GOROUTINE: VSTP Message Handler Failed")
} }
}() }()
log.Info("VSTP Message handler started")
for { for {
msg := <-sub.C msg := <-sub.C
if msg.Err != nil { if msg.Err != nil {
log.Error("STOMP Message Error: " + msg.Err.Error()) log.Error("STOMP Message Error", zap.Error(msg.Err))
} else { } else {
if msg != nil {
log.Info("STOMP Message Received") log.Info("STOMP Message Received")
fmt.Println(string(msg.Body))
handle(msg) handle(msg)
} else {
log.Info("STOMP Message Empty")
} }
} }
}
}()
}
}() }()
select {} select {}