simplify call to peer

This commit is contained in:
mr 2024-08-23 23:00:57 +02:00
parent 762cc19c14
commit 8e80fa88be
2 changed files with 19 additions and 14 deletions

View File

@ -15,7 +15,7 @@ type Peer struct {
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"`
FailedExecution []PeerExecution `json:"failed_execution,omitempty" bson:"failed_execution,omitempty"`
FailedExecution []PeerExecution `json:"failed_execution" bson:"failed_execution"`
}
func (ao *Peer) AddExecution(exec PeerExecution) {

View File

@ -71,8 +71,6 @@ func (p *PeerCache) GetAccessor(caller *tools.HTTPCaller) utils.Accessor {
func (p *PeerCache) LaunchPeerExecution(peerID string, dataID string,
dt utils.DataType, method tools.METHOD, body map[string]interface{}, caller *tools.HTTPCaller) (*PeerExecution, error) {
var err error
b := []byte{}
methods := caller.URLS[dt.String()]
if _, ok := methods[method]; !ok {
return nil, errors.New("no path found")
@ -84,14 +82,15 @@ func (p *PeerCache) LaunchPeerExecution(peerID string, dataID string,
meth = strings.ReplaceAll(meth, ":id", dataID)
}
url := ""
pexec := &PeerExecution{
Method: method.String(),
Url: url + methods[method],
Body: body,
DataType: dt.EnumIndex(),
DataID: dataID,
}
if mypeer, ok := p.checkPeerStatus(peerID, dt.API(), caller); !ok {
pexec := &PeerExecution{
Method: method.String(),
Url: p.urlFormat((mypeer.Url)+meth, dt),
Body: body,
DataType: dt.EnumIndex(),
DataID: dataID,
}
mypeer.AddExecution(*pexec)
mypeer.GetAccessor(nil).UpdateOne(mypeer, peerID)
return nil, errors.New("peer is not reachable")
@ -102,9 +101,15 @@ func (p *PeerCache) LaunchPeerExecution(peerID string, dataID string,
mypeer.FailedExecution = []PeerExecution{}
mypeer.GetAccessor(nil).UpdateOne(mypeer, peerID)
for _, v := range tmp {
go p.LaunchPeerExecution(peerID, v.DataID, utils.DataType(v.DataType), tools.ToMethod(v.Method), v.Body, caller)
go p.exec(v.Url, tools.ToMethod(v.Method), v.Body, caller)
}
}
return nil, p.exec(url, method, body, caller)
}
func (p *PeerCache) exec(url string, method tools.METHOD, body map[string]interface{}, caller *tools.HTTPCaller) error {
var b []byte
var err error
if method == tools.POST {
b, err = caller.CallPost(url, "", body)
}
@ -117,12 +122,12 @@ func (p *PeerCache) LaunchPeerExecution(peerID string, dataID string,
var m map[string]interface{}
json.Unmarshal(b, &m)
if err != nil {
return nil, err
return err
}
fmt.Println("LaunchPeerExecution AFT 3", m, url)
if e, ok := m["error"]; !ok && e != "" {
return nil, errors.New(fmt.Sprintf("%v", m["error"]))
return errors.New(fmt.Sprintf("%v", m["error"]))
}
return nil, err
return err
}