No discovery needed
This commit is contained in:
35
tools/api.go
35
tools/api.go
@@ -21,6 +21,10 @@ const (
|
||||
WAITING
|
||||
)
|
||||
|
||||
func (s State) EnumIndex() int {
|
||||
return int(s)
|
||||
}
|
||||
|
||||
func ToState(str string) State {
|
||||
for _, s := range []State{ALIVE, REDUCED_SERVICE, UNPROCESSABLE_ENTITY, DB_FALLOUT, TEAPOT, DEAD, WAITING} {
|
||||
if s.String() == str {
|
||||
@@ -57,39 +61,47 @@ func (a *API) GetState() (State, int, error) {
|
||||
return ALIVE, 200, nil
|
||||
}
|
||||
|
||||
func (a *API) CheckRemotePeer(url string) State {
|
||||
func (a *API) CheckRemotePeer(url string) (State, map[string]int) {
|
||||
// Check if the database is up
|
||||
caller := NewHTTPCaller(map[string]map[METHOD]string{})
|
||||
var resp APIStatusResponse
|
||||
b, err := caller.CallPost(url, "/status", []string{})
|
||||
if err != nil {
|
||||
return DEAD
|
||||
return DEAD, map[string]int{}
|
||||
}
|
||||
json.Unmarshal(b, &resp)
|
||||
if resp.Data == nil {
|
||||
return DEAD
|
||||
return DEAD, map[string]int{}
|
||||
}
|
||||
return ToState(resp.Data.State)
|
||||
new := map[string]int{}
|
||||
for k, v := range resp.Data.Services {
|
||||
new[k] = ToState(v).EnumIndex()
|
||||
}
|
||||
return ToState(resp.Data.State), new
|
||||
}
|
||||
|
||||
func (a *API) CheckRemoteAPIs(urls []string) (State, int, error) {
|
||||
func (a *API) CheckRemoteAPIs(urls []string) (State, map[string]int, error) {
|
||||
// Check if the database is up
|
||||
new := map[string]int{}
|
||||
caller := NewHTTPCaller(map[string]map[METHOD]string{})
|
||||
for _, url := range urls {
|
||||
var resp APIStatusResponse
|
||||
b, err := caller.CallGet(url, "/version/status")
|
||||
if err != nil {
|
||||
return REDUCED_SERVICE, 200, err
|
||||
return REDUCED_SERVICE, new, err
|
||||
}
|
||||
json.Unmarshal(b, &resp)
|
||||
if resp.Data == nil {
|
||||
return DEAD, 200, errors.New(url + " -> is DEAD")
|
||||
return DEAD, new, errors.New(url + " -> is DEAD")
|
||||
}
|
||||
for k, v := range resp.Data.Services {
|
||||
new[k] = ToState(v).EnumIndex()
|
||||
}
|
||||
if resp.Data.Code != 0 {
|
||||
return REDUCED_SERVICE, 200, errors.New(url + " -> " + resp.Error)
|
||||
return REDUCED_SERVICE, new, errors.New(url + " -> " + resp.Error)
|
||||
}
|
||||
}
|
||||
return ALIVE, 200, nil
|
||||
return ALIVE, new, nil
|
||||
}
|
||||
|
||||
type APIStatusResponse struct {
|
||||
@@ -98,6 +110,7 @@ type APIStatusResponse struct {
|
||||
}
|
||||
|
||||
type APIStatus struct {
|
||||
Code int `json:"code"`
|
||||
State string `json:"state"`
|
||||
Code int `json:"code"`
|
||||
State string `json:"state"`
|
||||
Services map[string]string `json:"services"`
|
||||
}
|
||||
|
@@ -2,10 +2,8 @@ package tools
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"strings"
|
||||
|
||||
"cloud.o-forge.io/core/oc-lib/logs"
|
||||
"github.com/nats-io/nats.go"
|
||||
)
|
||||
|
||||
@@ -14,7 +12,6 @@ type NATSMethod int
|
||||
const (
|
||||
REMOVE NATSMethod = iota
|
||||
CREATE
|
||||
DISCOVERY
|
||||
)
|
||||
|
||||
func NameToMethod(name string) NATSMethod {
|
||||
@@ -59,39 +56,3 @@ func (o *natsCaller) SetNATSPub(dataName string, method NATSMethod, data interfa
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
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")
|
||||
}
|
||||
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) {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user