Compare commits
No commits in common. "2404ee3fce1f896a1a4375647c160e72186c741e" and "2ad4c0bb911bd43c4c4ced9c1daddd8d0f0d31ce" have entirely different histories.
2404ee3fce
...
2ad4c0bb91
@ -10,6 +10,7 @@ import (
|
|||||||
"cloud.o-forge.io/core/oc-lib/dbs/mongo"
|
"cloud.o-forge.io/core/oc-lib/dbs/mongo"
|
||||||
"cloud.o-forge.io/core/oc-lib/logs"
|
"cloud.o-forge.io/core/oc-lib/logs"
|
||||||
"cloud.o-forge.io/core/oc-lib/models"
|
"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/peer"
|
||||||
"cloud.o-forge.io/core/oc-lib/models/resource_model"
|
"cloud.o-forge.io/core/oc-lib/models/resource_model"
|
||||||
"cloud.o-forge.io/core/oc-lib/models/resources/data"
|
"cloud.o-forge.io/core/oc-lib/models/resources/data"
|
||||||
@ -45,6 +46,7 @@ const (
|
|||||||
SHARED_WORKSPACE = utils.SHARED_WORKSPACE
|
SHARED_WORKSPACE = utils.SHARED_WORKSPACE
|
||||||
RULE = utils.RULE
|
RULE = utils.RULE
|
||||||
BOOKING = utils.BOOKING
|
BOOKING = utils.BOOKING
|
||||||
|
DISCOVERY = utils.DISCOVERY
|
||||||
)
|
)
|
||||||
|
|
||||||
func (d LibDataEnum) API() string {
|
func (d LibDataEnum) API() string {
|
||||||
@ -94,6 +96,7 @@ func Init(appName string, hostname string, port string) {
|
|||||||
}()
|
}()
|
||||||
logs.SetAppName(appName)
|
logs.SetAppName(appName)
|
||||||
logs.SetLogger(logs.CreateLogger("main", ""))
|
logs.SetLogger(logs.CreateLogger("main", ""))
|
||||||
|
logger := logs.GetLogger()
|
||||||
mongo.MONGOService.Init(models.GetModelsNames(), tools.GetConfig())
|
mongo.MONGOService.Init(models.GetModelsNames(), tools.GetConfig())
|
||||||
accessor := (&resource_model.ResourceModel{}).GetAccessor(nil)
|
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()} {
|
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 {
|
func GetLogger() zerolog.Logger {
|
||||||
@ -342,3 +362,10 @@ func (l *LibData) ToWorkflowExecution() *workflow_execution.WorkflowExecution {
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (l *LibData) ToDiscovery() *discovery.Discovery {
|
||||||
|
if l.Data.GetAccessor(nil).GetType() == utils.DISCOVERY.String() {
|
||||||
|
return l.Data.(*discovery.Discovery)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
@ -13,7 +13,7 @@ type Peer struct {
|
|||||||
utils.AbstractObject
|
utils.AbstractObject
|
||||||
Url string `json:"url,omitempty" bson:"url,omitempty" validate:"required,base64url"`
|
Url string `json:"url,omitempty" bson:"url,omitempty" validate:"required,base64url"`
|
||||||
PublicKey string `json:"public_key,omitempty" bson:"public_key,omitempty"`
|
PublicKey string `json:"public_key,omitempty" bson:"public_key,omitempty"`
|
||||||
Services map[string]int `json:"services,omitempty" bson:"services,omitempty"`
|
State tools.State `json:"state,omitempty" bson:"state,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ao *Peer) IsMySelf() bool {
|
func (ao *Peer) IsMySelf() bool {
|
||||||
|
@ -17,6 +17,7 @@ const (
|
|||||||
SHARED_WORKSPACE
|
SHARED_WORKSPACE
|
||||||
RULE
|
RULE
|
||||||
BOOKING
|
BOOKING
|
||||||
|
DISCOVERY
|
||||||
)
|
)
|
||||||
|
|
||||||
var DefaultAPI = [...]string{
|
var DefaultAPI = [...]string{
|
||||||
@ -34,6 +35,7 @@ var DefaultAPI = [...]string{
|
|||||||
"oc-shared",
|
"oc-shared",
|
||||||
"oc-shared",
|
"oc-shared",
|
||||||
"oc-datacenter",
|
"oc-datacenter",
|
||||||
|
"oc-peers",
|
||||||
}
|
}
|
||||||
|
|
||||||
var Str = [...]string{
|
var Str = [...]string{
|
||||||
@ -51,6 +53,7 @@ var Str = [...]string{
|
|||||||
"shared_workspace",
|
"shared_workspace",
|
||||||
"rule",
|
"rule",
|
||||||
"booking",
|
"booking",
|
||||||
|
"discovery",
|
||||||
}
|
}
|
||||||
|
|
||||||
func FromInt(i int) string {
|
func FromInt(i int) string {
|
||||||
|
31
tools/api.go
31
tools/api.go
@ -21,10 +21,6 @@ const (
|
|||||||
WAITING
|
WAITING
|
||||||
)
|
)
|
||||||
|
|
||||||
func (s State) EnumIndex() int {
|
|
||||||
return int(s)
|
|
||||||
}
|
|
||||||
|
|
||||||
func ToState(str string) State {
|
func ToState(str string) State {
|
||||||
for _, s := range []State{ALIVE, REDUCED_SERVICE, UNPROCESSABLE_ENTITY, DB_FALLOUT, TEAPOT, DEAD, WAITING} {
|
for _, s := range []State{ALIVE, REDUCED_SERVICE, UNPROCESSABLE_ENTITY, DB_FALLOUT, TEAPOT, DEAD, WAITING} {
|
||||||
if s.String() == str {
|
if s.String() == str {
|
||||||
@ -61,45 +57,39 @@ func (a *API) GetState() (State, int, error) {
|
|||||||
return ALIVE, 200, nil
|
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
|
// Check if the database is up
|
||||||
caller := NewHTTPCaller(map[string]map[METHOD]string{})
|
caller := NewHTTPCaller(map[string]map[METHOD]string{})
|
||||||
var resp APIStatusResponse
|
var resp APIStatusResponse
|
||||||
b, err := caller.CallPost(url, "/status", []string{})
|
b, err := caller.CallPost(url, "/status", []string{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return DEAD, map[string]int{}
|
return DEAD
|
||||||
}
|
}
|
||||||
json.Unmarshal(b, &resp)
|
json.Unmarshal(b, &resp)
|
||||||
if resp.Data == nil {
|
if resp.Data == nil {
|
||||||
return DEAD, map[string]int{}
|
return DEAD
|
||||||
}
|
}
|
||||||
new := map[string]int{}
|
return ToState(resp.Data.State)
|
||||||
for k, v := range resp.Data.Services {
|
|
||||||
new[k] = ToState(v).EnumIndex()
|
|
||||||
}
|
|
||||||
return ToState(resp.Data.State), new
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
// Check if the database is up
|
||||||
new := map[string]int{}
|
|
||||||
caller := NewHTTPCaller(map[string]map[METHOD]string{})
|
caller := NewHTTPCaller(map[string]map[METHOD]string{})
|
||||||
for appName, url := range urls {
|
for _, url := range urls {
|
||||||
var resp APIStatusResponse
|
var resp APIStatusResponse
|
||||||
b, err := caller.CallGet(url, "/version/status")
|
b, err := caller.CallGet(url, "/version/status")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return REDUCED_SERVICE, new, err
|
return REDUCED_SERVICE, 200, err
|
||||||
}
|
}
|
||||||
json.Unmarshal(b, &resp)
|
json.Unmarshal(b, &resp)
|
||||||
if resp.Data == nil {
|
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 {
|
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 {
|
type APIStatusResponse struct {
|
||||||
@ -110,5 +100,4 @@ type APIStatusResponse struct {
|
|||||||
type APIStatus struct {
|
type APIStatus struct {
|
||||||
Code int `json:"code"`
|
Code int `json:"code"`
|
||||||
State string `json:"state"`
|
State string `json:"state"`
|
||||||
Services map[string]string `json:"services"`
|
|
||||||
}
|
}
|
||||||
|
@ -2,8 +2,10 @@ package tools
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"cloud.o-forge.io/core/oc-lib/logs"
|
||||||
"github.com/nats-io/nats.go"
|
"github.com/nats-io/nats.go"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -12,6 +14,7 @@ type NATSMethod int
|
|||||||
const (
|
const (
|
||||||
REMOVE NATSMethod = iota
|
REMOVE NATSMethod = iota
|
||||||
CREATE
|
CREATE
|
||||||
|
DISCOVERY
|
||||||
)
|
)
|
||||||
|
|
||||||
func NameToMethod(name string) NATSMethod {
|
func NameToMethod(name string) NATSMethod {
|
||||||
@ -56,3 +59,39 @@ func (o *natsCaller) SetNATSPub(dataName string, method NATSMethod, data interfa
|
|||||||
}
|
}
|
||||||
return ""
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user