light modification

This commit is contained in:
mr 2025-01-23 09:06:22 +01:00
parent df04133551
commit f8ac3154e1

View File

@ -57,28 +57,22 @@ func (p *PeerCache) urlFormat(url string, dt tools.DataType) string {
func (p *PeerCache) checkPeerStatus(peerID string, appName string, caller *tools.HTTPCaller) (*Peer, bool) {
api := tools.API{}
access := NewShallowAccessor()
res, code, err := access.LoadOne(peerID) // Load the peer from db
fmt.Println("Checking peer status", res, code, err)
res, code, _ := access.LoadOne(peerID) // Load the peer from db
if code != 200 { // no peer no party
return nil, false
}
methods := caller.URLS[tools.PEER] // Get the methods url of the peer
fmt.Println("Checking peer status 2", methods)
if methods == nil {
return res.(*Peer), false
}
meth := methods[tools.POST] // Get the POST method to check status
fmt.Println("Checking peer status 3", meth)
if meth == "" {
return res.(*Peer), false
}
url := p.urlFormat(res.(*Peer).Url, tools.PEER) + meth // Format the URL
fmt.Println("Checking peer status on", url, "...")
state, services := api.CheckRemotePeer(url)
fmt.Println("Checking peer status on", url, state, services) // Check the status of the peer
res.(*Peer).ServicesState = services // Update the services states of the peer
access.UpdateOne(res, peerID) // Update the peer in the db
fmt.Println("Checking peer status 4", state, state != tools.DEAD, services[appName])
return res.(*Peer), state != tools.DEAD && services[appName] == 0 // Return the peer and its status
}
@ -97,7 +91,6 @@ func (p *PeerCache) LaunchPeerExecution(peerID string, dataID string,
// Check the status of the peer
if mypeer, ok := p.checkPeerStatus(peerID, dt.API(), caller); !ok && mypeer != nil {
// If the peer is not reachable, add the execution to the failed executions list
fmt.Println("Peer is not reachable")
pexec := &PeerExecution{
Method: method.String(),
Url: p.urlFormat((mypeer.Url)+meth, dt),
@ -109,13 +102,11 @@ func (p *PeerCache) LaunchPeerExecution(peerID string, dataID string,
NewShallowAccessor().UpdateOne(mypeer, peerID) // Update the peer in the db
return nil, errors.New("peer is not reachable")
} else {
fmt.Println("Peer is reachable", mypeer)
if mypeer == nil {
return nil, errors.New("peer not found")
}
// If the peer is reachable, launch the execution
url = p.urlFormat((mypeer.Url)+meth, dt) // Format the URL
fmt.Println("Peer is reachable 2", url)
tmp := mypeer.FailedExecution // Get the failed executions list
mypeer.FailedExecution = []PeerExecution{} // Reset the failed executions list
NewShallowAccessor().UpdateOne(mypeer, peerID) // Update the peer in the db
@ -123,7 +114,6 @@ func (p *PeerCache) LaunchPeerExecution(peerID string, dataID string,
go p.exec(v.Url, tools.ToMethod(v.Method), v.Body, caller)
}
}
fmt.Println("URL exec", url)
return nil, p.exec(url, method, body, caller) // Execute the method
}
@ -131,7 +121,6 @@ func (p *PeerCache) LaunchPeerExecution(peerID string, dataID string,
func (p *PeerCache) exec(url string, method tools.METHOD, body interface{}, caller *tools.HTTPCaller) error {
var b []byte
var err error
fmt.Println("Executing", method, "on", url, "with", body)
if method == tools.POST { // Execute the POST method if it's a POST method
b, err = caller.CallPost(url, "", body)
}