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

pull/86/head
miraclesu 2018-05-26 21:31:33 +08:00
parent f34a3da05e
commit 42ddb23f00
4 changed files with 10 additions and 3 deletions

View File

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

View File

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

View File

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

View File

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