test fail time
This commit is contained in:
parent
316d616419
commit
cc37d62a2d
38
crontab.go
38
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"
|
||||
|
@ -57,6 +57,7 @@ type Crontab struct {
|
|||
|
||||
interval *clinked.CircularLinked
|
||||
lastStatus bool
|
||||
isCalculated bool
|
||||
nextTime time.Time
|
||||
}
|
||||
|
||||
|
@ -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 {
|
||||
// GetStatus 获取上次状态 true false
|
||||
func (cron *Crontab) GetStatus() (status bool) {
|
||||
return cron.lastStatus
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// 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,17 +217,15 @@ func (cron *Crontab) linuxTimeUp() bool {
|
|||
return false
|
||||
}
|
||||
|
||||
func (cron *Crontab) intervalTimeUp() bool {
|
||||
|
||||
now := time.Now()
|
||||
if now.Unix() >= cron.nextTime.Unix() {
|
||||
|
||||
// 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]
|
||||
|
@ -236,8 +237,15 @@ func (cron *Crontab) intervalTimeUp() bool {
|
|||
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() {
|
||||
cron.isCalculated = false
|
||||
return true
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user