diff --git a/bin/csctl/cmd/node.go b/bin/csctl/cmd/node.go index 6d00c9b..5f34199 100644 --- a/bin/csctl/cmd/node.go +++ b/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() diff --git a/csctl.go b/csctl.go index 599cb07..55f62fb 100644 --- a/csctl.go +++ b/csctl.go @@ -12,6 +12,7 @@ import ( const ( NodeCmdUnknown NodeCmd = iota NodeCmdRmOld + NodeCmdSync NodeCmdMax ) @@ -21,6 +22,7 @@ var ( NodeCmds = []string{ "unknown", "rmold", + "sync", } ) diff --git a/node.go b/node.go index 2916e5a..64e63bc 100644 --- a/node.go +++ b/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()) } diff --git a/node/csctl.go b/node/csctl.go index 31b8594..22eedd7 100644 --- a/node/csctl.go +++ b/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())