OSDN Git Service

add
authorHAOYUatHZ <haoyu@protonmail.com>
Fri, 2 Aug 2019 12:30:49 +0000 (20:30 +0800)
committerHAOYUatHZ <haoyu@protonmail.com>
Fri, 2 Aug 2019 12:30:49 +0000 (20:30 +0800)
toolbar/precog/common/const.go
toolbar/precog/database/orm/node.go
toolbar/precog/monitor/monitor.go

index 3962b77..d22f93b 100644 (file)
@@ -4,7 +4,7 @@ const (
        NodeUnknownStatus uint8 = iota
        NodeHealthyStatus
        NodeCongestedStatus
-       NodeBusyStatus
+       NodeOrphanStatus
        NodeOfflineStatus
 )
 
@@ -12,6 +12,6 @@ var StatusMap = map[uint8]string{
        NodeUnknownStatus:   "unknown",
        NodeHealthyStatus:   "healthy",
        NodeCongestedStatus: "congested",
-       NodeBusyStatus:      "busy",
+       NodeOrphanStatus:    "orphan",
        NodeOfflineStatus:   "offline",
 }
index 7e29c59..a470f26 100644 (file)
@@ -10,7 +10,7 @@ import (
 
 type Node struct {
        Alias           string
-       PubKey          chainkd.XPub
+       PubKey          string
        Host            string
        Port            uint16
        BestHeight      uint64
@@ -23,22 +23,23 @@ type Node struct {
 
 func (n *Node) MarshalJSON() ([]byte, error) {
        status := common.StatusMap[n.Status]
-       var lantency uint64
+       var lantency int64
        var activeMinutes uint64
-       if n.Status != common.NodeOfflineStatus {
-               lantency = n.LantencyMS
+       switch n.Status {
+       case common.NodeHealthyStatus, common.NodeCongestedStatus, common.NodeOrphanStatus:
+               lantency = n.LantencyMS.Int64
                activeMinutes = uint64(time.Since(n.ActiveBeginTime).Minutes())
        }
 
        return json.Marshal(&struct {
-               Alias         string       `json:"alias"`
-               PubKey        chainkd.XPub `json:"pubkey"`
-               Host          string       `json:"host"`
-               Port          uint16       `json:"port"`
-               BestHeight    uint64       `json:"best_height"`
-               LantencyMS    uint64       `json:"lantency_ms,omitempty"`
-               ActiveMinutes uint64       `json:"active_minutes,omitempty"`
-               Status        string       `json:"status"`
+               Alias         string `json:"alias"`
+               PubKey        string `json:"pubkey"`
+               Host          string `json:"host"`
+               Port          uint16 `json:"port"`
+               BestHeight    uint64 `json:"best_height"`
+               LantencyMS    int64  `json:"lantency_ms,omitempty"`
+               ActiveMinutes uint64 `json:"active_minutes,omitempty"`
+               Status        string `json:"status"`
        }{
                Alias:         n.Alias,
                PubKey:        n.PubKey,
index d5a3df4..15dffc1 100644 (file)
@@ -7,6 +7,7 @@ import (
        log "github.com/sirupsen/logrus"
 
        "github.com/vapor/toolbar/precog/config"
+       "github.com/vapor/toolbar/precog/database/orm"
 )
 
 type monitor struct {
@@ -23,9 +24,10 @@ func NewMonitor(cfg *config.Config, db *gorm.DB) *monitor {
 
 func (m *monitor) Run() {
        if err := m.updateBootstrapNodes(); err != nil {
-               log.Fatal(err)
+               log.Error(err)
        }
 
+       go m.discovery()
        ticker := time.NewTicker(time.Duration(m.cfg.CheckFreqSeconds) * time.Second)
        for ; true; <-ticker.C {
                // TODO: lock?
@@ -33,12 +35,32 @@ func (m *monitor) Run() {
        }
 }
 
+// create or update: https://github.com/jinzhu/gorm/issues/1307
 func (m *monitor) updateBootstrapNodes() error {
-       // TODO: updated existed nodes
-       // TODO: add new nodes
+       for _, node := range m.cfg.Nodes {
+               ormNode := &orm.Node{
+                       PubKey: node.PubKey.String(),
+                       Alias:  node.Alias,
+                       Host:   node.Host,
+                       Port:   node.Port,
+               }
+               if err := m.db.Where(&orm.Node{PubKey: ormNode.PubKey}).
+                       Assign(&orm.Node{
+                               Alias: node.Alias,
+                               Host:  node.Host,
+                               Port:  node.Port,
+                       }).FirstOrCreate(ormNode).Error; err != nil {
+                       return err
+               }
+       }
+
        return nil
 }
 
+// TODO:
+func (m *monitor) discovery() {
+}
+
 func (m *monitor) monitorRountine() error {
        // TODO: dail, get lantency & best_height
        // TODO: decide check_height("best best_height" - "confirmations")