Browse Source

csctl: add sync command for node to sync node info to mongodb

pull/86/head
miraclesu 7 years ago
parent
commit
42ddb23f00
  1. 1
      bin/csctl/cmd/node.go
  2. 2
      csctl.go
  3. 8
      node.go
  4. 2
      node/csctl.go

1
bin/csctl/cmd/node.go

@ -30,6 +30,7 @@ var NodeCmd = &cobra.Command{
Available Commands:
rmold: remove old version(< 0.3.0) node info from mongodb and etcd
sync: sync node info to mongodb
`,
Run: func(cmd *cobra.Command, args []string) {
ea := NewExitAction()

2
csctl.go

@ -12,6 +12,7 @@ import (
const (
NodeCmdUnknown NodeCmd = iota
NodeCmdRmOld
NodeCmdSync
NodeCmdMax
)
@ -21,6 +22,7 @@ var (
NodeCmds = []string{
"unknown",
"rmold",
"sync",
}
)

8
node.go

@ -142,14 +142,16 @@ func WatchNode() client.WatchChan {
// On 结点实例启动后,在 mongoDB 中记录存活信息
func (n *Node) On() {
n.Alived, n.Version, n.UpTime = true, Version, time.Now()
if err := mgoDB.Upsert(Coll_Node, bson.M{"_id": n.ID}, n); err != nil {
log.Errorf(err.Error())
}
n.SyncToMgo()
}
// On 结点实例停用后,在 mongoDB 中去掉存活信息
func (n *Node) Down() {
n.Alived, n.DownTime = false, time.Now()
n.SyncToMgo()
}
func (n *Node) SyncToMgo() {
if err := mgoDB.Upsert(Coll_Node, bson.M{"_id": n.ID}, n); err != nil {
log.Errorf(err.Error())
}

2
node/csctl.go

@ -23,6 +23,8 @@ func (n *Node) executCsctlCmd(key, value []byte) error {
switch cmd.Cmd {
case cronsun.NodeCmdRmOld:
n.Node.RmOldInfo()
case cronsun.NodeCmdSync:
n.Node.SyncToMgo()
}
log.Infof("%s execute csctl command[%s] success", n.String(), cmd.Cmd.String())

Loading…
Cancel
Save