cronsun/node.go

143 lines
3.3 KiB
Go
Raw Normal View History

2017-05-12 06:48:24 +00:00
package cronsun
2017-01-09 02:32:14 +00:00
2017-01-09 10:51:52 +00:00
import (
2017-03-16 10:27:26 +00:00
"encoding/json"
"fmt"
2017-01-09 10:51:52 +00:00
"os"
"strconv"
"syscall"
2017-04-18 09:41:54 +00:00
"time"
2017-01-09 10:51:52 +00:00
client "github.com/coreos/etcd/clientv3"
2017-05-12 07:38:50 +00:00
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
2017-01-09 10:51:52 +00:00
2017-05-09 10:27:32 +00:00
"github.com/shunfei/cronsun/conf"
2017-05-12 07:38:50 +00:00
"github.com/shunfei/cronsun/log"
2017-01-09 10:51:52 +00:00
)
const (
Coll_Node = "node"
)
2017-01-09 09:13:56 +00:00
// 执行 cron cmd 的进程
// 注册到 /cronsun/node/<id>
2017-01-09 02:32:14 +00:00
type Node struct {
2017-02-14 07:15:00 +00:00
ID string `bson:"_id" json:"id"` // ip
PID string `bson:"pid" json:"pid"` // 进程 pid
2017-04-18 09:41:54 +00:00
Version string `bson:"version" json:"version"`
UpTime time.Time `bson:"up" json:"up"` // 启动时间
DownTime time.Time `bson:"down" json:"down"` // 上次关闭时间
Alived bool `bson:"alived" json:"alived"` // 是否可用
Connected bool `bson:"-" json:"connected"` // 当 Alived 为 true 时有效,表示心跳是否正常
2017-01-09 02:32:14 +00:00
}
2017-01-09 10:51:52 +00:00
func (n *Node) String() string {
return "node[" + n.ID + "] pid[" + n.PID + "]"
}
func (n *Node) Put(opts ...client.OpOption) (*client.PutResponse, error) {
return DefalutClient.Put(conf.Config.Node+n.ID, n.PID, opts...)
2017-01-09 10:51:52 +00:00
}
func (n *Node) Del() (*client.DeleteResponse, error) {
return DefalutClient.Delete(conf.Config.Node + n.ID)
2017-01-09 10:51:52 +00:00
}
// 判断 node 是否已注册到 etcd
// 存在则返回进行 pid不存在返回 -1
func (n *Node) Exist() (pid int, err error) {
resp, err := DefalutClient.Get(conf.Config.Node + n.ID)
2017-01-09 10:51:52 +00:00
if err != nil {
return
}
if len(resp.Kvs) == 0 {
return -1, nil
}
if pid, err = strconv.Atoi(string(resp.Kvs[0].Value)); err != nil {
if _, err = DefalutClient.Delete(conf.Config.Node + n.ID); err != nil {
2017-01-09 10:51:52 +00:00
return
}
return -1, nil
}
p, err := os.FindProcess(pid)
if err != nil {
return -1, nil
}
// TODO: 暂时不考虑 linux/unix 以外的系统
if p != nil && p.Signal(syscall.Signal(0)) == nil {
return
}
return -1, nil
}
2017-02-14 07:15:00 +00:00
func GetNodes() (nodes []*Node, err error) {
2017-04-14 07:33:14 +00:00
return GetNodesBy(nil)
}
func GetNodesBy(query interface{}) (nodes []*Node, err error) {
2017-02-14 07:15:00 +00:00
err = mgoDB.WithC(Coll_Node, func(c *mgo.Collection) error {
2017-04-14 07:33:14 +00:00
return c.Find(query).All(&nodes)
2017-02-14 07:15:00 +00:00
})
return
}
2017-04-14 07:33:14 +00:00
func ISNodeFault(id string) (bool, error) {
n := 0
err := mgoDB.WithC(Coll_Node, func(c *mgo.Collection) error {
var e error
n, e = c.Find(bson.M{"_id": id, "alived": true}).Count()
return e
})
return n > 0, err
}
2017-03-16 10:27:26 +00:00
func GetNodeGroups() (list []*Group, err error) {
resp, err := DefalutClient.Get(conf.Config.Group, client.WithPrefix(), client.WithSort(client.SortByKey, client.SortAscend))
if err != nil {
return
}
list = make([]*Group, 0, resp.Count)
for i := range resp.Kvs {
g := Group{}
err = json.Unmarshal(resp.Kvs[i].Value, &g)
if err != nil {
err = fmt.Errorf("node.GetGroups(key: %s) error: %s", string(resp.Kvs[i].Key), err.Error())
return
}
list = append(list, &g)
}
return
}
2017-04-14 07:33:14 +00:00
func WatchNode() client.WatchChan {
return DefalutClient.Watch(conf.Config.Node, client.WithPrefix())
}
// On 结点实例启动后,在 mongoDB 中记录存活信息
func (n *Node) On() {
2017-04-18 09:41:54 +00:00
n.Alived, n.Version, n.UpTime = true, Version, time.Now()
if err := mgoDB.Upsert(Coll_Node, bson.M{"_id": n.ID}, n); err != nil {
2017-05-12 07:38:50 +00:00
log.Errorf(err.Error())
}
}
// On 结点实例停用后,在 mongoDB 中去掉存活信息
func (n *Node) Down() {
2017-04-18 09:41:54 +00:00
n.Alived, n.DownTime = false, time.Now()
if err := mgoDB.Upsert(Coll_Node, bson.M{"_id": n.ID}, n); err != nil {
2017-05-12 07:38:50 +00:00
log.Errorf(err.Error())
}
}