Compare commits

..

No commits in common. "2404ee3fce1f896a1a4375647c160e72186c741e" and "2ad4c0bb911bd43c4c4ced9c1daddd8d0f0d31ce" have entirely different histories.

5 changed files with 84 additions and 26 deletions

View File

@ -10,6 +10,7 @@ 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"
@ -45,6 +46,7 @@ const (
SHARED_WORKSPACE = utils.SHARED_WORKSPACE
RULE = utils.RULE
BOOKING = utils.BOOKING
DISCOVERY = utils.DISCOVERY
)
func (d LibDataEnum) API() string {
@ -94,6 +96,7 @@ 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()} {
@ -120,6 +123,23 @@ 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 {
@ -342,3 +362,10 @@ 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

@ -11,9 +11,9 @@ import (
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"`
Services map[string]int `json:"services,omitempty" bson:"services,omitempty"`
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"`
}
func (ao *Peer) IsMySelf() bool {

View File

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

View File

@ -21,10 +21,6 @@ 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 {
@ -61,45 +57,39 @@ func (a *API) GetState() (State, int, error) {
return ALIVE, 200, nil
}
func (a *API) CheckRemotePeer(url string) (State, map[string]int) {
func (a *API) CheckRemotePeer(url string) State {
// 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, map[string]int{}
return DEAD
}
json.Unmarshal(b, &resp)
if resp.Data == nil {
return DEAD, map[string]int{}
return DEAD
}
new := map[string]int{}
for k, v := range resp.Data.Services {
new[k] = ToState(v).EnumIndex()
}
return ToState(resp.Data.State), new
return ToState(resp.Data.State)
}
func (a *API) CheckRemoteAPIs(urls map[string]string) (State, map[string]int, error) {
func (a *API) CheckRemoteAPIs(urls []string) (State, int, error) {
// Check if the database is up
new := map[string]int{}
caller := NewHTTPCaller(map[string]map[METHOD]string{})
for appName, url := range urls {
for _, url := range urls {
var resp APIStatusResponse
b, err := caller.CallGet(url, "/version/status")
if err != nil {
return REDUCED_SERVICE, new, err
return REDUCED_SERVICE, 200, err
}
json.Unmarshal(b, &resp)
if resp.Data == nil {
return DEAD, new, errors.New(url + " -> is DEAD")
return DEAD, 200, errors.New(url + " -> is DEAD")
}
new[appName] = ToState(resp.Data.State).EnumIndex()
if resp.Data.Code != 0 {
return REDUCED_SERVICE, new, errors.New(url + " -> " + resp.Error)
return REDUCED_SERVICE, 200, errors.New(url + " -> " + resp.Error)
}
}
return ALIVE, new, nil
return ALIVE, 200, nil
}
type APIStatusResponse struct {
@ -108,7 +98,6 @@ type APIStatusResponse struct {
}
type APIStatus struct {
Code int `json:"code"`
State string `json:"state"`
Services map[string]string `json:"services"`
Code int `json:"code"`
State string `json:"state"`
}

View File

@ -2,8 +2,10 @@ package tools
import (
"encoding/json"
"errors"
"strings"
"cloud.o-forge.io/core/oc-lib/logs"
"github.com/nats-io/nats.go"
)
@ -12,6 +14,7 @@ type NATSMethod int
const (
REMOVE NATSMethod = iota
CREATE
DISCOVERY
)
func NameToMethod(name string) NATSMethod {
@ -56,3 +59,39 @@ 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)
}
}