From cc37d62a2d7be73ac85980cd68a457a641108d70 Mon Sep 17 00:00:00 2001 From: huangsimin Date: Thu, 20 Dec 2018 14:37:37 +0800 Subject: [PATCH] test fail time --- crontab.go | 74 ++++++++++++++++++++++++++++++------------------------ 1 file changed, 41 insertions(+), 33 deletions(-) diff --git a/crontab.go b/crontab.go index 842b6cf..e471def 100644 --- a/crontab.go +++ b/crontab.go @@ -9,7 +9,7 @@ import ( "strings" "time" - clinked "474420502.top/eson/structure/circular_linked" + "474420502.top/eson/structure/circular_linked" "github.com/Pallinder/go-randomdata" "github.com/davecgh/go-spew/spew" @@ -55,9 +55,10 @@ type Crontab struct { YearPlan *trieYear - interval *clinked.CircularLinked - lastStatus bool - nextTime time.Time + interval *clinked.CircularLinked + lastStatus bool + isCalculated bool + nextTime time.Time } // NewCrontab create 一个crontab @@ -82,20 +83,17 @@ func (cron *Crontab) UnmarshalYAML(unmarshal func(interface{}) error) error { return nil } -// SetStatus 设置状态 接口定义 -func (cron *Crontab) SetStatus(status interface{}) { +// SetStatus 设置上次状态 true false +func (cron *Crontab) SetStatus(status bool) { if cron.interval != nil { - cron.lastStatus = status.(bool) + cron.lastStatus = status } } -// GetStatus 设置状态 接口定义 -func (cron *Crontab) GetStatus() (status interface{}) { - if cron.interval != nil { - return cron.lastStatus - } - return nil +// GetStatus 获取上次状态 true false +func (cron *Crontab) GetStatus() (status bool) { + return cron.lastStatus } // TimeUp 是否时间快到 @@ -111,6 +109,10 @@ func (cron *Crontab) TimeUp() bool { // NextTime 返回下次任务的时间 func (cron *Crontab) NextTime() time.Time { if cron.interval != nil { + if !cron.isCalculated { + now := time.Now() + cron.intervalCalculateNextTime(now) + } return cron.nextTime } @@ -136,6 +138,7 @@ func (cron *Crontab) FromString(crontab string) error { // "f1-2|5-10x5,f1|10m,10-15,f1" cron.lastStatus = true + cron.isCalculated = false cron.interval = clinked.NewCircularLinked() var intervalList []interface{} intervalList = parseIntervalString(matches[0]) @@ -214,30 +217,35 @@ func (cron *Crontab) linuxTimeUp() bool { return false } +// IntervalCalculateNextTime 计算时间间隔的下次时间 +func (cron *Crontab) intervalCalculateNextTime(now time.Time) { + iv := cron.interval.Cursor().GetValue().(hInterval) + isecond := 0 + if cron.lastStatus == false && len(iv.PlanFail) > 0 { + idx := randomdata.Number(len(iv.PlanFail)) + lr := iv.PlanFail[idx] + isecond = randomdata.Number(lr.left, lr.right+1) + log.Println("fail time ", isecond) + } else { + idx := randomdata.Number(len(iv.PlanNormal)) + lr := iv.PlanNormal[idx] + isecond = randomdata.Number(lr.left, lr.right+1) + } + + iv.Count-- + if iv.Count <= 0 { + iv.reset() + cron.interval.MoveNext() + } + cron.nextTime = now.Add(time.Duration(isecond) * time.Second) +} + func (cron *Crontab) intervalTimeUp() bool { now := time.Now() + if now.Unix() >= cron.nextTime.Unix() { - - iv := cron.interval.Cursor().GetValue().(hInterval) - isecond := 0 - if cron.lastStatus == false && len(iv.PlanFail) > 0 { - idx := randomdata.Number(len(iv.PlanFail)) - lr := iv.PlanFail[idx] - isecond = randomdata.Number(lr.left, lr.right+1) - } else { - idx := randomdata.Number(len(iv.PlanNormal)) - lr := iv.PlanNormal[idx] - isecond = randomdata.Number(lr.left, lr.right+1) - } - - iv.Count-- - if iv.Count <= 0 { - iv.reset() - cron.interval.MoveNext() - } - - cron.nextTime = now.Add(time.Duration(isecond) * time.Second) + cron.isCalculated = false return true }