diff --git a/bin/csctl/cmd/upgrade.go b/bin/csctl/cmd/upgrade.go index e7a0874..32e8893 100644 --- a/bin/csctl/cmd/upgrade.go +++ b/bin/csctl/cmd/upgrade.go @@ -31,7 +31,7 @@ var UpgradeCmd = &cobra.Command{ ea.Exit("invalid version number") } - nodesById := getIPMapper(ea) + nodesById := getIPMapper(ea, prever) if prever < "0.3.0" { fmt.Println("upgrading data to version 0.3.0") if to_0_3_0(ea, nodesById) { @@ -48,7 +48,7 @@ var UpgradeCmd = &cobra.Command{ }, } -func getIPMapper(ea *ExitAction) map[string]*cronsun.Node { +func getIPMapper(ea *ExitAction, prever string) map[string]*cronsun.Node { nodes, err := cronsun.GetNodes() if err != nil { ea.Exit("failed to fetch nodes from MongoDB: %s", err.Error()) @@ -61,6 +61,9 @@ func getIPMapper(ea *ExitAction) map[string]*cronsun.Node { continue } + if prever < "0.3.0" { + n.RmOldInfo() + } ipMapper[n.IP] = n } diff --git a/node.go b/node.go index 8a9533e..2916e5a 100644 --- a/node.go +++ b/node.go @@ -102,7 +102,6 @@ func RemoveNode(query interface{}) error { return mgoDB.WithC(Coll_Node, func(c *mgo.Collection) error { return c.Remove(query) }) - } func ISNodeAlive(id string) (bool, error) { @@ -155,3 +154,9 @@ func (n *Node) Down() { log.Errorf(err.Error()) } } + +// RmOldInfo remove old version(< 0.3.0) node info +func (n *Node) RmOldInfo() { + RemoveNode(bson.M{"_id": n.IP}) + DefalutClient.Delete(conf.Config.Node + n.IP) +} diff --git a/node/csctl.go b/node/csctl.go index f61a779..31b8594 100644 --- a/node/csctl.go +++ b/node/csctl.go @@ -3,10 +3,7 @@ package node import ( "encoding/json" - "gopkg.in/mgo.v2/bson" - "github.com/shunfei/cronsun" - "github.com/shunfei/cronsun/conf" "github.com/shunfei/cronsun/log" ) @@ -25,15 +22,9 @@ func (n *Node) executCsctlCmd(key, value []byte) error { switch cmd.Cmd { case cronsun.NodeCmdRmOld: - n.rmOld() + n.Node.RmOldInfo() } log.Infof("%s execute csctl command[%s] success", n.String(), cmd.Cmd.String()) return nil } - -func (n *Node) rmOld() { - // remove old version(< 0.3.0) node info - cronsun.RemoveNode(bson.M{"_id": n.IP}) - cronsun.DefalutClient.Delete(conf.Config.Node + n.IP) -}