Discovery Nats automate

This commit is contained in:
mr 2024-08-22 15:38:27 +02:00
parent 8c9a983216
commit d89826db44

View File

@ -4,7 +4,6 @@ import (
"encoding/json"
"errors"
"strings"
"sync"
"cloud.o-forge.io/core/oc-lib/logs"
"github.com/nats-io/nats.go"
@ -73,17 +72,12 @@ func (o *natsCaller) DiscoveryNATS(name string, model NATSObjectInterface) error
if err != nil {
return errors.New("Could not connect to NATS")
}
defer nc.Close()
wg := &sync.WaitGroup{}
wg.Add(2)
go o.listenForChange(nc, model, DISCOVERY.GenerateKey("ask"), wg)
go o.listenForChange(nc, model, DISCOVERY.GenerateKey(name), wg)
wg.Wait()
go o.listenForChange(nc, model, DISCOVERY.GenerateKey("ask"))
go o.listenForChange(nc, model, DISCOVERY.GenerateKey(name))
return nil
}
func (o *natsCaller) listenForChange(nc *nats.Conn, model NATSObjectInterface, chanName string, wg *sync.WaitGroup) {
defer wg.Done()
func (o *natsCaller) listenForChange(nc *nats.Conn, model NATSObjectInterface, chanName string) {
api := API{}
logger := logs.GetLogger()
ch := make(chan *nats.Msg, 64)
@ -93,7 +87,6 @@ func (o *natsCaller) listenForChange(nc *nats.Conn, model NATSObjectInterface, c
return
}
defer subs.Unsubscribe()
for msg := range ch {
logger.Info().Msg("Received message from NATS : " + string(msg.Data))
m := model.Serialize()