Structured NATS

This commit is contained in:
mr
2026-01-28 15:49:21 +01:00
parent 1c9d7b63c0
commit e3fe49c239
2 changed files with 18 additions and 14 deletions

View File

@@ -2,7 +2,6 @@ package tools
import (
"encoding/json"
"fmt"
"strings"
"sync"
"time"
@@ -67,7 +66,7 @@ func NewNATSCaller() *natsCaller {
// on workflows' scheduling. Messages must contain
// workflow execution ID, to allow retrieval of execution infos
func (s *natsCaller) ListenNats(execs map[NATSMethod]func(map[string]string)) {
func (s *natsCaller) ListenNats(execs map[NATSMethod]func(NATSResponse)) {
log := logs.GetLogger()
if config.GetConfig().NATSUrl == "" {
log.Error().Msg(" -> NATS_SERVER is not set")
@@ -121,7 +120,7 @@ func (o *natsCaller) SetNATSPub(method NATSMethod, data NATSResponse) string {
// on workflows' scheduling. Messages must contain
// workflow execution ID, to allow retrieval of execution infos
func (o *natsCaller) listenForChange(logger zerolog.Logger, nc *nats.Conn, natsTools NATSMethod,
function func(map[string]string), wg *sync.WaitGroup) {
function func(NATSResponse), wg *sync.WaitGroup) {
defer wg.Done()
ch := make(chan *nats.Msg, 64)
logger.Info().Msg("Listening to " + natsTools.GenerateKey())
@@ -132,9 +131,9 @@ func (o *natsCaller) listenForChange(logger zerolog.Logger, nc *nats.Conn, natsT
defer subs.Unsubscribe()
for msg := range ch {
map_mess := map[string]string{}
json.Unmarshal(msg.Data, &map_mess)
fmt.Println("Catching " + natsTools.String() + " workflow... " + map_mess["id"])
function(map_mess)
var resp NATSResponse
json.Unmarshal(msg.Data, &resp)
logger.Info().Msg("Catching " + natsTools.String() + "... " + resp.FromApp + " - " + resp.Datatype.String())
function(resp)
}
}