2017-01-04 09:27:36 +00:00
|
|
|
|
package node
|
|
|
|
|
|
|
|
|
|
import (
|
2017-01-05 12:35:41 +00:00
|
|
|
|
"fmt"
|
2017-01-05 02:37:48 +00:00
|
|
|
|
"os"
|
2017-01-05 12:35:41 +00:00
|
|
|
|
"strconv"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"golang.org/x/net/context"
|
2017-01-05 02:37:48 +00:00
|
|
|
|
|
2017-01-04 09:27:36 +00:00
|
|
|
|
client "github.com/coreos/etcd/clientv3"
|
2017-01-05 02:37:48 +00:00
|
|
|
|
|
2017-01-06 08:44:42 +00:00
|
|
|
|
"sunteng/commons/log"
|
2017-01-05 02:37:48 +00:00
|
|
|
|
"sunteng/commons/util"
|
2017-01-05 12:35:41 +00:00
|
|
|
|
"sunteng/cronsun/conf"
|
2017-01-09 10:51:52 +00:00
|
|
|
|
"sunteng/cronsun/models"
|
2017-01-12 07:34:13 +00:00
|
|
|
|
"sunteng/cronsun/node/cron"
|
2017-01-05 12:35:41 +00:00
|
|
|
|
)
|
|
|
|
|
|
2017-01-04 09:27:36 +00:00
|
|
|
|
// Node 执行 cron 命令服务的结构体
|
|
|
|
|
type Node struct {
|
2017-01-09 10:51:52 +00:00
|
|
|
|
*models.Client
|
|
|
|
|
*models.Node
|
2017-01-12 07:34:13 +00:00
|
|
|
|
*cron.Cron
|
|
|
|
|
|
2017-02-17 09:09:46 +00:00
|
|
|
|
jobs Jobs
|
2017-02-17 10:55:40 +00:00
|
|
|
|
groups Groups
|
2017-02-17 09:09:46 +00:00
|
|
|
|
cmds map[string]*models.Cmd
|
2017-02-17 13:29:04 +00:00
|
|
|
|
// map[group id]map[job id](job group)
|
2017-01-20 09:11:26 +00:00
|
|
|
|
// 用于 group 发生变化的时候修改相应的 job
|
2017-02-17 13:29:04 +00:00
|
|
|
|
link map[string]map[string]string
|
|
|
|
|
// 删除的 job id,用于 group 更新
|
|
|
|
|
delIDs map[string]bool
|
2017-01-05 02:37:48 +00:00
|
|
|
|
|
2017-01-09 10:51:52 +00:00
|
|
|
|
ttl int64
|
2017-01-05 12:35:41 +00:00
|
|
|
|
|
|
|
|
|
lID client.LeaseID // lease id
|
2017-01-06 08:44:42 +00:00
|
|
|
|
lch <-chan *client.LeaseKeepAliveResponse
|
|
|
|
|
|
|
|
|
|
done chan struct{}
|
2017-01-04 09:27:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-01-06 08:44:42 +00:00
|
|
|
|
func NewNode(cfg *conf.Conf) (n *Node, err error) {
|
2017-01-05 02:37:48 +00:00
|
|
|
|
ip, err := util.GetLocalIP()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
n = &Node{
|
2017-01-09 10:51:52 +00:00
|
|
|
|
Client: models.DefalutClient,
|
|
|
|
|
Node: &models.Node{
|
|
|
|
|
ID: ip.String(),
|
|
|
|
|
PID: strconv.Itoa(os.Getpid()),
|
|
|
|
|
},
|
2017-01-12 07:34:13 +00:00
|
|
|
|
Cron: cron.New(),
|
2017-01-06 08:44:42 +00:00
|
|
|
|
|
2017-02-17 09:09:46 +00:00
|
|
|
|
cmds: make(map[string]*models.Cmd),
|
|
|
|
|
|
2017-01-09 10:51:52 +00:00
|
|
|
|
ttl: cfg.Ttl,
|
2017-01-06 08:44:42 +00:00
|
|
|
|
done: make(chan struct{}),
|
2017-01-05 02:37:48 +00:00
|
|
|
|
}
|
|
|
|
|
return
|
2017-01-04 09:27:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 注册到 /cronsun/proc/xx
|
2017-01-05 12:35:41 +00:00
|
|
|
|
func (n *Node) Register() (err error) {
|
2017-01-09 10:51:52 +00:00
|
|
|
|
pid, err := n.Node.Exist()
|
2017-01-05 12:35:41 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if pid != -1 {
|
2017-01-09 10:51:52 +00:00
|
|
|
|
return fmt.Errorf("node[%s] pid[%d] exist", n.Node.ID, pid)
|
2017-01-05 12:35:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-01-06 08:59:31 +00:00
|
|
|
|
resp, err := n.Client.Grant(context.TODO(), n.ttl)
|
2017-01-05 12:35:41 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-09 10:51:52 +00:00
|
|
|
|
if _, err = n.Node.Put(client.WithLease(resp.ID)); err != nil {
|
2017-01-05 12:35:41 +00:00
|
|
|
|
return
|
|
|
|
|
}
|
2017-01-06 08:44:42 +00:00
|
|
|
|
|
|
|
|
|
ch, err := n.Client.KeepAlive(context.TODO(), resp.ID)
|
|
|
|
|
if err != nil {
|
2017-01-05 12:35:41 +00:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-06 08:44:42 +00:00
|
|
|
|
n.lID, n.lch = resp.ID, ch
|
2017-01-05 12:35:41 +00:00
|
|
|
|
return
|
2017-01-04 09:27:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-02-17 09:09:46 +00:00
|
|
|
|
func (n *Node) loadJobs() (err error) {
|
2017-01-20 09:11:26 +00:00
|
|
|
|
if n.groups, err = models.GetGroups(n.ID); err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
2017-02-19 02:37:13 +00:00
|
|
|
|
|
2017-02-17 13:29:04 +00:00
|
|
|
|
jobs, err := models.GetJobs()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
2017-02-17 09:09:46 +00:00
|
|
|
|
|
2017-02-17 13:29:04 +00:00
|
|
|
|
n.jobs, n.link = make(Jobs, len(jobs)), make(map[string]map[string]string, len(n.groups))
|
|
|
|
|
if len(jobs) == 0 {
|
2017-02-17 10:16:47 +00:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-17 13:29:04 +00:00
|
|
|
|
for _, job := range jobs {
|
2017-02-17 10:16:47 +00:00
|
|
|
|
n.addJob(job, false)
|
|
|
|
|
}
|
2017-02-17 13:29:04 +00:00
|
|
|
|
|
2017-02-17 09:09:46 +00:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-17 10:16:47 +00:00
|
|
|
|
func (n *Node) addJob(job *models.Job, notice bool) {
|
2017-02-17 13:29:04 +00:00
|
|
|
|
for _, r := range job.Rules {
|
|
|
|
|
for _, gid := range r.GroupIDs {
|
|
|
|
|
n.addLink(gid, job.ID, job.Group)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-17 10:16:47 +00:00
|
|
|
|
cmds := job.Cmds(n.ID, n.groups)
|
|
|
|
|
if len(cmds) == 0 {
|
2017-01-20 09:11:26 +00:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-17 10:55:40 +00:00
|
|
|
|
n.jobs[job.ID] = job
|
2017-02-17 10:16:47 +00:00
|
|
|
|
for _, cmd := range cmds {
|
|
|
|
|
n.addCmd(cmd, notice)
|
2017-02-17 09:09:46 +00:00
|
|
|
|
}
|
2017-02-17 10:16:47 +00:00
|
|
|
|
return
|
2017-02-17 09:09:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-02-17 10:55:40 +00:00
|
|
|
|
func (n *Node) delJob(id string) {
|
|
|
|
|
job, ok := n.jobs[id]
|
|
|
|
|
// 之前此任务没有在当前结点执行
|
|
|
|
|
if !ok {
|
2017-02-17 13:29:04 +00:00
|
|
|
|
n.delIDs[id] = true
|
2017-02-17 10:55:40 +00:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-17 13:29:04 +00:00
|
|
|
|
delete(n.jobs, id)
|
|
|
|
|
for _, r := range job.Rules {
|
|
|
|
|
for _, gid := range r.GroupIDs {
|
|
|
|
|
n.delLink(gid, job.ID)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-17 10:16:47 +00:00
|
|
|
|
cmds := job.Cmds(n.ID, n.groups)
|
|
|
|
|
if len(cmds) == 0 {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, cmd := range cmds {
|
|
|
|
|
n.delCmd(cmd)
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-17 10:55:40 +00:00
|
|
|
|
func (n *Node) modJob(job *models.Job) {
|
|
|
|
|
oJob, ok := n.jobs[job.ID]
|
|
|
|
|
// 之前此任务没有在当前结点执行,直接增加任务
|
|
|
|
|
if !ok {
|
|
|
|
|
n.addJob(job, true)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-17 13:29:04 +00:00
|
|
|
|
for _, r := range oJob.Rules {
|
|
|
|
|
for _, gid := range r.GroupIDs {
|
|
|
|
|
n.delLink(gid, oJob.ID)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-17 10:55:40 +00:00
|
|
|
|
prevCmds := oJob.Cmds(n.ID, n.groups)
|
|
|
|
|
*oJob = *job
|
|
|
|
|
cmds := oJob.Cmds(n.ID, n.groups)
|
2017-02-17 10:16:47 +00:00
|
|
|
|
|
|
|
|
|
for id, cmd := range cmds {
|
|
|
|
|
n.addCmd(cmd, true)
|
|
|
|
|
delete(prevCmds, id)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, cmd := range prevCmds {
|
|
|
|
|
n.delCmd(cmd)
|
|
|
|
|
}
|
2017-02-17 13:29:04 +00:00
|
|
|
|
|
|
|
|
|
for _, r := range oJob.Rules {
|
|
|
|
|
for _, gid := range r.GroupIDs {
|
|
|
|
|
n.addLink(gid, oJob.ID, oJob.Group)
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-02-17 10:16:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (n *Node) addCmd(cmd *models.Cmd, notice bool) {
|
2017-02-17 09:09:46 +00:00
|
|
|
|
c, ok := n.cmds[cmd.GetID()]
|
|
|
|
|
if ok {
|
|
|
|
|
sch := c.Schedule
|
|
|
|
|
*c = *cmd
|
|
|
|
|
|
|
|
|
|
// 节点执行时间不变,不用更新 cron
|
|
|
|
|
if c.Schedule == sch {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
c = cmd
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := n.Cron.AddJob(c.Schedule, c); err != nil {
|
|
|
|
|
msg := fmt.Sprintf("job[%s] rule[%s] timer[%s] parse err: %s", c.Job.ID, c.JobRule.ID, c.Schedule, err.Error())
|
|
|
|
|
log.Warn(msg)
|
|
|
|
|
c.Fail(time.Now(), msg)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
|
n.cmds[c.GetID()] = c
|
2017-01-19 11:03:18 +00:00
|
|
|
|
}
|
2017-02-17 10:16:47 +00:00
|
|
|
|
|
|
|
|
|
if notice {
|
|
|
|
|
log.Noticef("job[%s] rule[%s] timer[%s] has added", c.Job.ID, c.JobRule.ID, c.Schedule)
|
|
|
|
|
}
|
2017-01-20 09:11:26 +00:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-17 10:16:47 +00:00
|
|
|
|
func (n *Node) delCmd(cmd *models.Cmd) {
|
|
|
|
|
delete(n.cmds, cmd.GetID())
|
|
|
|
|
n.Cron.DelJob(cmd)
|
|
|
|
|
log.Noticef("job[%s] rule[%s] timer[%s] has deleted", cmd.Job.ID, cmd.JobRule.ID, cmd.Schedule)
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-17 13:29:04 +00:00
|
|
|
|
func (n *Node) addLink(gid, jid, gname string) {
|
2017-01-20 09:11:26 +00:00
|
|
|
|
if len(gid) == 0 {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
js, ok := n.link[gid]
|
|
|
|
|
if !ok {
|
2017-02-17 13:29:04 +00:00
|
|
|
|
js = make(map[string]string, 4)
|
2017-01-20 09:11:26 +00:00
|
|
|
|
n.link[gid] = js
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-17 13:29:04 +00:00
|
|
|
|
js[jid] = gname
|
2017-01-20 09:11:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (n *Node) delLink(gid, jid string) {
|
|
|
|
|
if len(gid) == 0 {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
js, ok := n.link[gid]
|
|
|
|
|
if !ok {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
delete(js, jid)
|
2017-01-19 11:03:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-02-17 13:29:04 +00:00
|
|
|
|
func (n *Node) addGroup(g *models.Group) {
|
|
|
|
|
n.groups[g.ID] = g
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (n *Node) delGroup(id string) {
|
|
|
|
|
job, ok := n.jobs[id]
|
|
|
|
|
// 之前此任务没有在当前结点执行
|
|
|
|
|
if !ok {
|
|
|
|
|
return
|
2017-01-20 09:11:26 +00:00
|
|
|
|
}
|
2017-01-20 09:42:50 +00:00
|
|
|
|
|
2017-02-17 13:29:04 +00:00
|
|
|
|
cmds := job.Cmds(n.ID, n.groups)
|
|
|
|
|
if len(cmds) == 0 {
|
|
|
|
|
return
|
2017-01-20 09:42:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-02-17 13:29:04 +00:00
|
|
|
|
for _, cmd := range cmds {
|
|
|
|
|
n.delCmd(cmd)
|
|
|
|
|
}
|
|
|
|
|
return
|
2017-01-20 09:11:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-02-17 13:29:04 +00:00
|
|
|
|
func (n *Node) modGroup(g *models.Group) {
|
|
|
|
|
oGroup, ok := n.groups[g.ID]
|
|
|
|
|
if !ok {
|
|
|
|
|
n.addGroup(g)
|
2017-01-20 09:42:50 +00:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-17 13:29:04 +00:00
|
|
|
|
// 都包含/都不包含当前节点,对当前节点任务无影响
|
|
|
|
|
if (oGroup.Included(n.ID) && g.Included(n.ID)) || (!oGroup.Included(n.ID) && !g.Included(n.ID)) {
|
|
|
|
|
*oGroup = *g
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 增加当前节点
|
|
|
|
|
if !oGroup.Included(n.ID) && g.Included(n.ID) {
|
|
|
|
|
n.groups[g.ID] = g
|
|
|
|
|
jids := n.link[g.ID]
|
|
|
|
|
if len(jids) == 0 {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var err error
|
|
|
|
|
for jid, gname := range jids {
|
|
|
|
|
job, ok := n.jobs[jid]
|
|
|
|
|
// job 之前已运行,无需改动
|
|
|
|
|
if ok {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// job 已删除
|
|
|
|
|
if n.delIDs[jid] {
|
|
|
|
|
n.delLink(g.ID, jid)
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if job, err = models.GetJob(gname, jid); err != nil {
|
|
|
|
|
log.Warnf("get job[%s][%s] err: %s", gname, jid, err.Error())
|
|
|
|
|
n.delLink(g.ID, jid)
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cmds := job.Cmds(n.ID, n.groups)
|
|
|
|
|
for _, cmd := range cmds {
|
|
|
|
|
n.addCmd(cmd, true)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 移除当前节点
|
|
|
|
|
jids := n.link[g.ID]
|
|
|
|
|
if len(jids) == 0 {
|
|
|
|
|
n.groups[g.ID] = g
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for jid, _ := range jids {
|
|
|
|
|
job, ok := n.jobs[jid]
|
|
|
|
|
if !ok {
|
|
|
|
|
// 数据出错
|
|
|
|
|
log.Warnf("WTF! group[%s] job[%s]", g.ID, jid)
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
n.groups[oGroup.ID] = oGroup
|
|
|
|
|
prevCmds := job.Cmds(n.ID, n.groups)
|
|
|
|
|
n.groups[g.ID] = g
|
|
|
|
|
cmds := job.Cmds(n.ID, n.groups)
|
|
|
|
|
|
|
|
|
|
for id, cmd := range cmds {
|
|
|
|
|
n.addCmd(cmd, true)
|
|
|
|
|
delete(prevCmds, id)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, cmd := range prevCmds {
|
|
|
|
|
n.delCmd(cmd)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
n.groups[g.ID] = g
|
|
|
|
|
return
|
2017-01-20 09:11:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-01-19 11:03:18 +00:00
|
|
|
|
func (n *Node) watchJobs() {
|
|
|
|
|
rch := models.WatchJobs()
|
|
|
|
|
for wresp := range rch {
|
|
|
|
|
for _, ev := range wresp.Events {
|
|
|
|
|
switch {
|
|
|
|
|
case ev.IsCreate():
|
2017-01-20 09:11:26 +00:00
|
|
|
|
job, err := models.GetJobFromKv(ev.Kv)
|
2017-01-19 11:03:18 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
log.Warnf(err.Error())
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-17 10:16:47 +00:00
|
|
|
|
n.addJob(job, true)
|
2017-01-19 11:03:18 +00:00
|
|
|
|
case ev.IsModify():
|
2017-01-20 09:11:26 +00:00
|
|
|
|
job, err := models.GetJobFromKv(ev.Kv)
|
2017-01-19 11:03:18 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
log.Warnf(err.Error())
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-17 10:55:40 +00:00
|
|
|
|
n.modJob(job)
|
2017-01-19 11:03:18 +00:00
|
|
|
|
case ev.Type == client.EventTypeDelete:
|
2017-02-17 13:29:04 +00:00
|
|
|
|
n.delJob(models.GetIDFromKey(string(ev.Kv.Key)))
|
2017-01-19 11:03:18 +00:00
|
|
|
|
default:
|
|
|
|
|
log.Warnf("unknown event type[%v] from job[%s]", ev.Type, string(ev.Kv.Key))
|
|
|
|
|
}
|
2017-01-12 07:34:13 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-20 04:26:02 +00:00
|
|
|
|
func (n *Node) watchGroups() {
|
2017-02-11 09:59:19 +00:00
|
|
|
|
rch := models.WatchGroups()
|
2017-01-20 04:26:02 +00:00
|
|
|
|
for wresp := range rch {
|
|
|
|
|
for _, ev := range wresp.Events {
|
2017-01-20 09:11:26 +00:00
|
|
|
|
switch {
|
|
|
|
|
case ev.IsCreate():
|
|
|
|
|
g, err := models.GetGroupFromKv(ev.Kv)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Warnf(err.Error())
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
n.addGroup(g)
|
|
|
|
|
case ev.IsModify():
|
2017-01-20 09:42:50 +00:00
|
|
|
|
g, err := models.GetGroupFromKv(ev.Kv)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Warnf(err.Error())
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-17 13:29:04 +00:00
|
|
|
|
n.modGroup(g)
|
2017-01-20 09:11:26 +00:00
|
|
|
|
case ev.Type == client.EventTypeDelete:
|
2017-02-17 13:29:04 +00:00
|
|
|
|
n.delGroup(models.GetIDFromKey(string(ev.Kv.Key)))
|
2017-01-20 09:11:26 +00:00
|
|
|
|
default:
|
|
|
|
|
log.Warnf("unknown event type[%v] from group[%s]", ev.Type, string(ev.Kv.Key))
|
|
|
|
|
}
|
2017-01-20 04:26:02 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-01-19 11:03:18 +00:00
|
|
|
|
|
2017-01-05 02:37:48 +00:00
|
|
|
|
// 启动服务
|
2017-01-12 07:34:13 +00:00
|
|
|
|
func (n *Node) Run() (err error) {
|
2017-01-06 08:44:42 +00:00
|
|
|
|
go n.keepAlive()
|
2017-01-12 07:34:13 +00:00
|
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
|
if err != nil {
|
|
|
|
|
n.Stop(nil)
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
|
2017-02-17 09:09:46 +00:00
|
|
|
|
if err = n.loadJobs(); err != nil {
|
2017-01-12 07:34:13 +00:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
n.Cron.Start()
|
2017-02-17 10:16:47 +00:00
|
|
|
|
go n.watchJobs()
|
2017-02-17 13:29:04 +00:00
|
|
|
|
go n.watchGroups()
|
2017-02-14 06:39:34 +00:00
|
|
|
|
n.Node.On()
|
2017-01-12 07:34:13 +00:00
|
|
|
|
return
|
2017-01-06 08:44:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 断网掉线重新注册
|
|
|
|
|
func (n *Node) keepAlive() {
|
|
|
|
|
for {
|
|
|
|
|
for _ = range n.lch {
|
|
|
|
|
}
|
2017-01-05 02:37:48 +00:00
|
|
|
|
|
2017-01-06 08:44:42 +00:00
|
|
|
|
select {
|
|
|
|
|
case <-n.done:
|
|
|
|
|
return
|
|
|
|
|
default:
|
|
|
|
|
}
|
|
|
|
|
time.Sleep(time.Duration(n.ttl+1) * time.Second)
|
|
|
|
|
|
|
|
|
|
log.Noticef("%s has dropped, try to reconnect...", n.String())
|
|
|
|
|
if err := n.Register(); err != nil {
|
|
|
|
|
log.Warn(err.Error())
|
|
|
|
|
} else {
|
|
|
|
|
log.Noticef("%s reconnected", n.String())
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-01-05 02:37:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-01-18 08:27:35 +00:00
|
|
|
|
// 停止服务
|
2017-01-05 02:37:48 +00:00
|
|
|
|
func (n *Node) Stop(i interface{}) {
|
2017-02-14 06:39:34 +00:00
|
|
|
|
n.Node.Down()
|
2017-01-06 08:44:42 +00:00
|
|
|
|
close(n.done)
|
2017-01-09 10:51:52 +00:00
|
|
|
|
n.Node.Del()
|
2017-01-05 02:37:48 +00:00
|
|
|
|
n.Client.Close()
|
2017-01-12 07:34:13 +00:00
|
|
|
|
n.Cron.Stop()
|
2017-01-05 02:37:48 +00:00
|
|
|
|
}
|