default http behavior on is my self

This commit is contained in:
mr 2024-08-21 08:54:29 +02:00
parent c6ea2195ed
commit e31815f576
5 changed files with 50 additions and 46 deletions

View File

@ -21,7 +21,8 @@ func (ao *Peer) IsMySelf() bool {
}
func (p *Peer) LaunchPeerExecution(peerID string, dataID string, url string, dt utils.DataType, method tools.METHOD, body map[string]interface{}, caller *tools.HTTPCaller) (*PeerExecution, error) {
return (&PeerCache{}).LaunchPeerExecution(peerID, dataID, url, dt, method, body, caller)
p.UUID = peerID
return (&PeerCache{}).LaunchPeerExecution(peerID, p.IsMySelf(), dataID, url, dt, method, body, caller)
}
func (ao *Peer) GetID() string {

View File

@ -21,6 +21,7 @@ type PeerExecution struct {
Method tools.METHOD
Url string
Body map[string]interface{}
IsMySelf bool
Caller tools.HTTPCaller
PeerID string
DataType utils.DataType
@ -41,7 +42,15 @@ func (p *PeerCache) GetAccessor(caller *tools.HTTPCaller) utils.Accessor {
return data
}
func (p *PeerCache) LaunchPeerExecution(peerID string, dataID string, url string, dt utils.DataType, method tools.METHOD, body map[string]interface{}, caller *tools.HTTPCaller) (*PeerExecution, error) {
func (p *PeerCache) LaunchPeerExecution(peerID string, isMySelf bool, dataID string, url string,
dt utils.DataType, method tools.METHOD, body map[string]interface{}, caller *tools.HTTPCaller) (*PeerExecution, error) {
if strings.Contains(url, "localhost") || strings.Contains(url, "127.0.0.1") {
if isMySelf {
url = "http://" + dt.API() // default behavior on basic API container name
} else {
return nil, errors.New("Peer " + peerID + " is not reachable")
}
}
var err error
b := []byte{}
methods := caller.URLS[dt.String()]
@ -67,6 +76,7 @@ func (p *PeerCache) LaunchPeerExecution(peerID string, dataID string, url string
Method: method,
Url: url + methods[method],
Body: body,
IsMySelf: isMySelf,
Caller: *caller,
PeerID: peerID,
DataType: dt,
@ -88,7 +98,8 @@ func (p *PeerCache) LaunchPeerExecution(peerID string, dataID string, url string
func (p *PeerCache) retryPeerExecution() {
execs := []*PeerExecution{}
for _, v := range singleton.Executions {
d, err := p.LaunchPeerExecution(v.PeerID, v.DataID, v.Url, v.DataType, v.Method, v.Body, &v.Caller)
d, err := p.LaunchPeerExecution(v.PeerID, v.IsMySelf, v.DataID, v.Url, v.DataType, v.Method, v.Body, &v.Caller)
if err == nil {
execs = append(execs, d)
} else {

View File

@ -19,6 +19,23 @@ const (
BOOKING
)
var defaultAPI = [...]string{
"",
"oc-catalog",
"oc-catalog",
"oc-catalog",
"oc-catalog",
"oc-catalog",
"oc-workflow",
"",
"oc-workspace",
"",
"oc-peer",
"oc-shared",
"oc-shared",
"oc-datacenter",
}
var Str = [...]string{
"invalid",
"data_resource",
@ -40,6 +57,10 @@ func FromInt(i int) string {
return Str[i]
}
func (d DataType) API() string {
return defaultAPI[d]
}
func (d DataType) String() string {
return Str[d]
}

View File

@ -118,21 +118,16 @@ func (wfa *workflowMongoAccessor) book(id string, realData *Workflow, execs []*w
}
paccess := (&peer.Peer{})
p, code, _ := paccess.GetAccessor(nil).LoadOne(peerID)
fmt.Println("PEER", p, code)
if code != 200 {
continue
}
url := p.(*peer.Peer).Url
if p.(*peer.Peer).IsMySelf() {
url = "http://" + utils.BOOKING.String()
}
b, err := paccess.LaunchPeerExecution(p.GetID(), "", url, utils.BOOKING, tools.POST,
b, err := paccess.LaunchPeerExecution(p.GetID(), "", p.(*peer.Peer).Url, utils.BOOKING, tools.POST,
(&workflow_execution.WorkflowExecutions{
WorkflowID: id,
ResourceID: dc_id,
Executions: execs,
}).Serialize(), wfa.Caller)
fmt.Println("LaunchPeerExecution RES", b, err)
fmt.Println("LaunchPeerExecutio RES", b, err)
if err != nil && b == nil {
return err
}

View File

@ -45,13 +45,9 @@ func (wfa *sharedWorkspaceMongoAccessor) sharedWorkspace(shared *SharedWorkspace
for _, p := range shared.Peers {
pp, code, _ := paccess.GetAccessor(nil).LoadOne(p)
if code == 200 {
url := pp.(*peer.Peer).Url
if pp.(*peer.Peer).IsMySelf() {
url = "http://" + utils.WORKSPACE.String()
}
b, err := paccess.LaunchPeerExecution(p, v, url, utils.WORKSPACE, tools.DELETE, nil, wfa.Caller)
b, err := paccess.LaunchPeerExecution(p, v, pp.(*peer.Peer).Url, utils.WORKSPACE, tools.DELETE, nil, wfa.Caller)
if err != nil && b == nil {
wfa.Logger.Error().Msg("Could not send to peer " + url + ". Error: " + err.Error())
wfa.Logger.Error().Msg("Could not send to peer " + pp.(*peer.Peer).Url + ". Error: " + err.Error())
}
}
}
@ -71,13 +67,9 @@ func (wfa *sharedWorkspaceMongoAccessor) sharedWorkspace(shared *SharedWorkspace
paccess := (&peer.Peer{})
pp, code, _ := paccess.GetAccessor(nil).LoadOne(p)
if code == 200 {
url := pp.(*peer.Peer).Url
if pp.(*peer.Peer).IsMySelf() {
url = "http://" + utils.WORKSPACE.String()
}
b, err := paccess.LaunchPeerExecution(p, v, url, utils.WORKSPACE, tools.POST, workspace.Serialize(), wfa.Caller)
b, err := paccess.LaunchPeerExecution(p, v, pp.(*peer.Peer).Url, utils.WORKSPACE, tools.POST, workspace.Serialize(), wfa.Caller)
if err != nil && b == nil {
wfa.Logger.Error().Msg("Could not send to peer " + url + ". Error: " + err.Error())
wfa.Logger.Error().Msg("Could not send to peer " + pp.(*peer.Peer).Url + ". Error: " + err.Error())
}
}
}
@ -113,13 +105,9 @@ func (wfa *sharedWorkspaceMongoAccessor) sharedWorkflow(shared *SharedWorkspace,
if code != 200 {
continue
}
url := pp.(*peer.Peer).Url
if pp.(*peer.Peer).IsMySelf() {
url = "http://" + utils.WORKFLOW.String()
}
b, err := paccess.LaunchPeerExecution(p, v, url, utils.WORKFLOW, tools.DELETE, nil, wfa.Caller)
b, err := paccess.LaunchPeerExecution(p, v, pp.(*peer.Peer).Url, utils.WORKFLOW, tools.DELETE, nil, wfa.Caller)
if err != nil && b == nil {
wfa.Logger.Error().Msg("Could not send to peer " + url + ". Error: " + err.Error())
wfa.Logger.Error().Msg("Could not send to peer " + pp.(*peer.Peer).Url + ". Error: " + err.Error())
}
}
}
@ -144,13 +132,9 @@ func (wfa *sharedWorkspaceMongoAccessor) sharedWorkflow(shared *SharedWorkspace,
}
pp, code, _ := paccess.GetAccessor(nil).LoadOne(p)
if code == 200 {
url := pp.(*peer.Peer).Url
if pp.(*peer.Peer).IsMySelf() {
url = "http://" + utils.WORKFLOW.String()
}
b, err := paccess.LaunchPeerExecution(p, shared.UUID, url, utils.WORKFLOW, tools.POST, workflow.Serialize(), wfa.Caller)
b, err := paccess.LaunchPeerExecution(p, shared.UUID, pp.(*peer.Peer).Url, utils.WORKFLOW, tools.POST, workflow.Serialize(), wfa.Caller)
if err != nil && b == nil {
wfa.Logger.Error().Msg("Could not send to peer " + url + ". Error: " + err.Error())
wfa.Logger.Error().Msg("Could not send to peer " + pp.(*peer.Peer).Url + ". Error: " + err.Error())
}
}
}
@ -173,13 +157,9 @@ func (wfa *sharedWorkspaceMongoAccessor) deleteToPeer(shared *SharedWorkspace) {
if code != 200 {
continue
}
url := p.(*peer.Peer).Url
if p.(*peer.Peer).IsMySelf() {
url = "http://" + utils.SHARED_WORKSPACE.String()
}
b, err := paccess.LaunchPeerExecution(p.GetID(), shared.UUID, url, utils.SHARED_WORKSPACE, tools.DELETE, nil, wfa.Caller)
b, err := paccess.LaunchPeerExecution(p.GetID(), shared.UUID, p.(*peer.Peer).Url, utils.SHARED_WORKSPACE, tools.DELETE, nil, wfa.Caller)
if err != nil && b == nil {
wfa.Logger.Error().Msg("Could not send to peer " + url + ". Error: " + err.Error())
wfa.Logger.Error().Msg("Could not send to peer " + p.(*peer.Peer).Url + ". Error: " + err.Error())
}
}
}
@ -197,13 +177,9 @@ func (wfa *sharedWorkspaceMongoAccessor) sendToPeer(shared *SharedWorkspace) {
if code != 200 {
continue
}
url := p.(*peer.Peer).Url
if p.(*peer.Peer).IsMySelf() {
url = "http://" + utils.SHARED_WORKSPACE.String()
}
b, err := paccess.LaunchPeerExecution(p.GetID(), v, url, utils.SHARED_WORKSPACE, tools.POST, shared.Serialize(), wfa.Caller)
b, err := paccess.LaunchPeerExecution(p.GetID(), v, p.(*peer.Peer).Url, utils.SHARED_WORKSPACE, tools.POST, shared.Serialize(), wfa.Caller)
if err != nil && b == nil {
wfa.Logger.Error().Msg("Could not send to peer " + url + ". Error: " + err.Error())
wfa.Logger.Error().Msg("Could not send to peer " + p.(*peer.Peer).Url + ". Error: " + err.Error())
}
}
}