mirror of https://github.com/shunfei/cronsun
proc: 执行中任务信息过期策略
parent
117d694b98
commit
7e12c60415
|
@ -206,7 +206,7 @@ func (j *Job) Run() {
|
||||||
NodeID: j.runOn,
|
NodeID: j.runOn,
|
||||||
Time: t,
|
Time: t,
|
||||||
}
|
}
|
||||||
go p.Start()
|
p.Start()
|
||||||
|
|
||||||
if err := cmd.Wait(); err != nil {
|
if err := cmd.Wait(); err != nil {
|
||||||
p.Stop()
|
p.Stop()
|
||||||
|
|
|
@ -6,9 +6,28 @@ import (
|
||||||
|
|
||||||
client "github.com/coreos/etcd/clientv3"
|
client "github.com/coreos/etcd/clientv3"
|
||||||
|
|
||||||
|
"sunteng/commons/log"
|
||||||
"sunteng/cronsun/conf"
|
"sunteng/cronsun/conf"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
leaseID client.LeaseID
|
||||||
|
)
|
||||||
|
|
||||||
|
func ProcessKeepAlive() error {
|
||||||
|
if conf.Config.ProcTtl == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, err := DefalutClient.Grant(context.TODO(), conf.Config.ProcTtl+5)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
leaseID = resp.ID
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// 当前执行中的任务信息
|
// 当前执行中的任务信息
|
||||||
// key: /cronsun/proc/node/job id/pid
|
// key: /cronsun/proc/node/job id/pid
|
||||||
// value: 开始执行时间
|
// value: 开始执行时间
|
||||||
|
@ -18,6 +37,8 @@ type Process struct {
|
||||||
JobID string `json:"job_id"`
|
JobID string `json:"job_id"`
|
||||||
NodeID string `json:"node_id"`
|
NodeID string `json:"node_id"`
|
||||||
Time time.Time `json:"name"` // 开始执行时间
|
Time time.Time `json:"name"` // 开始执行时间
|
||||||
|
|
||||||
|
running bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Process) Key() string {
|
func (p *Process) Key() string {
|
||||||
|
@ -39,24 +60,33 @@ func (p *Process) Count() (int64, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Process) Put() error {
|
func (p *Process) Put() error {
|
||||||
if conf.Config.ProcTtl == 0 {
|
if leaseID == 0 {
|
||||||
_, err := DefalutClient.Put(p.Key(), p.Val())
|
_, err := DefalutClient.Put(p.Key(), p.Val())
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, err := DefalutClient.Grant(context.TODO(), conf.Config.ProcTtl)
|
_, err := DefalutClient.Put(p.Key(), p.Val(), client.WithLease(leaseID))
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err = DefalutClient.Put(p.Key(), p.Val(), client.WithLease(resp.ID))
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Process) Start() error {
|
func (p *Process) Del() error {
|
||||||
return nil
|
_, err := DefalutClient.Delete(p.Key())
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Process) Start() {
|
||||||
|
if err := p.Put(); err != nil {
|
||||||
|
log.Warnf("proc put err: %s", err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
p.running = true
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Process) Stop() error {
|
func (p *Process) Stop() error {
|
||||||
return nil
|
if !p.running {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return p.Del()
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue