Compare commits

..

2 Commits

Author SHA1 Message Date
mr
2404ee3fce No discovery needed 2024-08-23 09:03:51 +02:00
mr
e4ee33dcbe No discovery needed 2024-08-23 09:01:28 +02:00
5 changed files with 26 additions and 84 deletions

View File

@ -10,7 +10,6 @@ import (
"cloud.o-forge.io/core/oc-lib/dbs/mongo"
"cloud.o-forge.io/core/oc-lib/logs"
"cloud.o-forge.io/core/oc-lib/models"
"cloud.o-forge.io/core/oc-lib/models/discovery"
"cloud.o-forge.io/core/oc-lib/models/peer"
"cloud.o-forge.io/core/oc-lib/models/resource_model"
"cloud.o-forge.io/core/oc-lib/models/resources/data"
@ -46,7 +45,6 @@ const (
SHARED_WORKSPACE = utils.SHARED_WORKSPACE
RULE = utils.RULE
BOOKING = utils.BOOKING
DISCOVERY = utils.DISCOVERY
)
func (d LibDataEnum) API() string {
@ -96,7 +94,6 @@ func Init(appName string, hostname string, port string) {
}()
logs.SetAppName(appName)
logs.SetLogger(logs.CreateLogger("main", ""))
logger := logs.GetLogger()
mongo.MONGOService.Init(models.GetModelsNames(), tools.GetConfig())
accessor := (&resource_model.ResourceModel{}).GetAccessor(nil)
for _, model := range []string{utils.DATA_RESOURCE.String(), utils.PROCESSING_RESOURCE.String(), utils.STORAGE_RESOURCE.String(), utils.DATACENTER_RESOURCE.String(), utils.WORKFLOW_RESOURCE.String()} {
@ -123,23 +120,6 @@ func Init(appName string, hostname string, port string) {
})
}
}
discoveryAccess := (&discovery.Discovery{}).GetAccessor(nil)
res, code, _ := discoveryAccess.Search(nil, appName)
initial := &discovery.Discovery{
AbstractObject: utils.AbstractObject{
Name: appName,
},
Host: hostname,
Port: port,
State: 1,
}
if code == 200 && len(res) == 0 {
discoveryAccess.StoreOne(initial)
}
err := tools.NewNATSCaller().DiscoveryNATS(appName, initial)
if err != nil {
logger.Error().Msg(err.Error())
}
}
func GetLogger() zerolog.Logger {
@ -362,10 +342,3 @@ func (l *LibData) ToWorkflowExecution() *workflow_execution.WorkflowExecution {
}
return nil
}
func (l *LibData) ToDiscovery() *discovery.Discovery {
if l.Data.GetAccessor(nil).GetType() == utils.DISCOVERY.String() {
return l.Data.(*discovery.Discovery)
}
return nil
}

View File

@ -13,7 +13,7 @@ type Peer struct {
utils.AbstractObject
Url string `json:"url,omitempty" bson:"url,omitempty" validate:"required,base64url"`
PublicKey string `json:"public_key,omitempty" bson:"public_key,omitempty"`
State tools.State `json:"state,omitempty" bson:"state,omitempty"`
Services map[string]int `json:"services,omitempty" bson:"services,omitempty"`
}
func (ao *Peer) IsMySelf() bool {

View File

@ -17,7 +17,6 @@ const (
SHARED_WORKSPACE
RULE
BOOKING
DISCOVERY
)
var DefaultAPI = [...]string{
@ -35,7 +34,6 @@ var DefaultAPI = [...]string{
"oc-shared",
"oc-shared",
"oc-datacenter",
"oc-peers",
}
var Str = [...]string{
@ -53,7 +51,6 @@ var Str = [...]string{
"shared_workspace",
"rule",
"booking",
"discovery",
}
func FromInt(i int) string {

View File

@ -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,45 @@ 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 map[string]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 {
for appName, 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")
}
new[appName] = ToState(resp.Data.State).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 {
@ -100,4 +110,5 @@ type APIStatusResponse struct {
type APIStatus struct {
Code int `json:"code"`
State string `json:"state"`
Services map[string]string `json:"services"`
}

View File

@ -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)
}
}