oc-lib/tools/nats_caller.go

105 lines
2.3 KiB
Go
Raw Normal View History

2024-08-21 10:04:09 +02:00
package tools
import (
"encoding/json"
2024-08-22 13:11:21 +02:00
"errors"
2024-08-21 10:04:09 +02:00
"strings"
2024-08-22 13:11:21 +02:00
"sync"
2024-08-21 10:04:09 +02:00
2024-08-22 13:11:21 +02:00
"cloud.o-forge.io/core/oc-lib/logs"
2024-08-21 10:04:09 +02:00
"github.com/nats-io/nats.go"
)
type NATSMethod int
const (
REMOVE NATSMethod = iota
CREATE
2024-08-22 13:11:21 +02:00
DISCOVERY
2024-08-21 10:04:09 +02:00
)
func NameToMethod(name string) NATSMethod {
for _, v := range [...]NATSMethod{REMOVE, CREATE} {
if strings.Contains(strings.ToLower(v.String()), strings.ToLower(name)) {
return v
}
}
return -1
}
func (d NATSMethod) GenerateKey(name string) string {
return name + "_" + d.String()
}
func (d NATSMethod) String() string {
2024-08-22 15:04:01 +02:00
return [...]string{"remove", "create", "discovery"}[d]
2024-08-21 10:04:09 +02:00
}
2024-08-21 10:21:17 +02:00
type natsCaller struct{}
2024-08-21 10:04:09 +02:00
2024-08-21 10:21:17 +02:00
func NewNATSCaller() *natsCaller {
return &natsCaller{}
2024-08-21 10:04:09 +02:00
}
2024-08-21 10:21:17 +02:00
func (o *natsCaller) SetNATSPub(dataName string, method NATSMethod, data interface{}) string {
if GetConfig().NATSUrl == "" {
2024-08-21 10:04:09 +02:00
return " -> NATS_SERVER is not set"
}
2024-08-21 10:21:17 +02:00
nc, err := nats.Connect(GetConfig().NATSUrl)
2024-08-21 10:04:09 +02:00
if err != nil {
return " -> Could not reach NATS server : " + err.Error()
}
defer nc.Close()
js, err := json.Marshal(data)
if err != nil {
return " -> " + err.Error()
}
err = nc.Publish(method.GenerateKey(dataName), js)
if err != nil {
return " -> " + err.Error()
}
return ""
}
2024-08-22 13:11:21 +02:00
type NATSObjectInterface interface {
Serialize() map[string]interface{}
}
func (o *natsCaller) DiscoveryNATS(name string, model NATSObjectInterface) error {
if GetConfig().NATSUrl == "" {
return errors.New("NATS_SERVER is not set")
}
nc, err := nats.Connect(GetConfig().NATSUrl)
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()
return nil
}
func (o *natsCaller) listenForChange(nc *nats.Conn, model NATSObjectInterface, chanName string, wg *sync.WaitGroup) {
defer wg.Done()
api := API{}
logger := logs.GetLogger()
ch := make(chan *nats.Msg, 64)
subs, err := nc.ChanSubscribe(chanName, ch)
if err != nil {
logger.Error().Msg("Error listening to NATS : " + err.Error())
return
}
defer subs.Unsubscribe()
for msg := range ch {
logger.Info().Msg("Received message from NATS : " + string(msg.Data))
m := model.Serialize()
s, _, _ := api.GetState()
m["state"] = s
o.SetNATSPub("answer", DISCOVERY, m)
}
}