peers logic
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"encoding/json"
|
||||
|
||||
"cloud.o-forge.io/core/oc-lib/models/utils"
|
||||
"cloud.o-forge.io/core/oc-lib/static"
|
||||
"cloud.o-forge.io/core/oc-lib/tools"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
@@ -14,6 +15,15 @@ type Peer struct {
|
||||
PublicKey string `json:"public_key,omitempty" bson:"public_key,omitempty"`
|
||||
}
|
||||
|
||||
func (ao *Peer) IsMySelf() bool {
|
||||
id, _ := static.GetMyLocalJsonPeer()
|
||||
return ao.UUID == id
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
func (ao *Peer) GetID() string {
|
||||
return ao.UUID
|
||||
}
|
||||
|
||||
105
models/peer/peer_cache.go
Normal file
105
models/peer/peer_cache.go
Normal file
@@ -0,0 +1,105 @@
|
||||
package peer
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"cloud.o-forge.io/core/oc-lib/logs"
|
||||
"cloud.o-forge.io/core/oc-lib/models/utils"
|
||||
"cloud.o-forge.io/core/oc-lib/tools"
|
||||
)
|
||||
|
||||
var currentRountine = 0
|
||||
var singleton = &PeerCache{
|
||||
Executions: []*PeerExecution{},
|
||||
}
|
||||
|
||||
type PeerExecution struct {
|
||||
Method tools.METHOD
|
||||
Url string
|
||||
Body map[string]interface{}
|
||||
Caller tools.HTTPCaller
|
||||
PeerID string
|
||||
DataType utils.DataType
|
||||
DataID string
|
||||
}
|
||||
|
||||
type PeerCache struct {
|
||||
Executions []*PeerExecution
|
||||
}
|
||||
|
||||
func (p *PeerCache) checkPeerStatus(peerID string) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (p *PeerCache) GetAccessor(caller *tools.HTTPCaller) utils.Accessor {
|
||||
data := New()
|
||||
data.Init(utils.PEER, caller)
|
||||
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) {
|
||||
var err error
|
||||
b := []byte{}
|
||||
methods := caller.URLS[dt.String()]
|
||||
if _, ok := methods[method]; !ok {
|
||||
return nil, errors.New("no path found")
|
||||
}
|
||||
if !p.checkPeerStatus(peerID) {
|
||||
return nil, err
|
||||
}
|
||||
if method == tools.POST {
|
||||
b, err = caller.CallPost(url, methods[method], body)
|
||||
}
|
||||
if method == tools.GET {
|
||||
b, err = caller.CallGet(url, strings.ReplaceAll(methods[method], ":id", dataID))
|
||||
}
|
||||
if method == tools.DELETE {
|
||||
b, err = caller.CallDelete(url, strings.ReplaceAll(methods[method], ":id", dataID))
|
||||
}
|
||||
var m map[string]interface{}
|
||||
json.Unmarshal(b, &m)
|
||||
if err != nil {
|
||||
pexec := &PeerExecution{
|
||||
Method: method,
|
||||
Url: url + methods[method],
|
||||
Body: body,
|
||||
Caller: *caller,
|
||||
PeerID: peerID,
|
||||
DataType: dt,
|
||||
DataID: dataID,
|
||||
}
|
||||
singleton.Executions = append(singleton.Executions, pexec)
|
||||
if currentRountine == 0 {
|
||||
currentRountine++
|
||||
go p.retryPeerExecution()
|
||||
}
|
||||
return pexec, err
|
||||
}
|
||||
if _, ok := m["error"]; !ok {
|
||||
return nil, errors.New(fmt.Sprintf("%v", m["error"]))
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
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)
|
||||
if err == nil {
|
||||
execs = append(execs, d)
|
||||
} else {
|
||||
logs.GetLogger().With().Err(err)
|
||||
}
|
||||
}
|
||||
singleton.Executions = execs
|
||||
if len(singleton.Executions) > 0 {
|
||||
time.Sleep(60 * time.Second)
|
||||
p.retryPeerExecution()
|
||||
} else {
|
||||
currentRountine = 0
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user