mirror of https://github.com/shunfei/cronsun
csctl: add sync command for node to sync node info to mongodb
parent
f34a3da05e
commit
42ddb23f00
|
@ -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()
|
||||||
|
|
2
csctl.go
2
csctl.go
|
@ -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",
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
8
node.go
8
node.go
|
@ -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())
|
||||||
}
|
}
|
||||||
|
|
|
@ -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())
|
||||||
|
|
Loading…
Reference in New Issue