test fail time
This commit is contained in:
parent
316d616419
commit
cc37d62a2d
38
crontab.go
38
crontab.go
|
@ -9,7 +9,7 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
clinked "474420502.top/eson/structure/circular_linked"
|
"474420502.top/eson/structure/circular_linked"
|
||||||
"github.com/Pallinder/go-randomdata"
|
"github.com/Pallinder/go-randomdata"
|
||||||
|
|
||||||
"github.com/davecgh/go-spew/spew"
|
"github.com/davecgh/go-spew/spew"
|
||||||
|
@ -57,6 +57,7 @@ type Crontab struct {
|
||||||
|
|
||||||
interval *clinked.CircularLinked
|
interval *clinked.CircularLinked
|
||||||
lastStatus bool
|
lastStatus bool
|
||||||
|
isCalculated bool
|
||||||
nextTime time.Time
|
nextTime time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -82,21 +83,18 @@ func (cron *Crontab) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetStatus 设置状态 接口定义
|
// SetStatus 设置上次状态 true false
|
||||||
func (cron *Crontab) SetStatus(status interface{}) {
|
func (cron *Crontab) SetStatus(status bool) {
|
||||||
|
|
||||||
if cron.interval != nil {
|
if cron.interval != nil {
|
||||||
cron.lastStatus = status.(bool)
|
cron.lastStatus = status
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetStatus 设置状态 接口定义
|
// GetStatus 获取上次状态 true false
|
||||||
func (cron *Crontab) GetStatus() (status interface{}) {
|
func (cron *Crontab) GetStatus() (status bool) {
|
||||||
if cron.interval != nil {
|
|
||||||
return cron.lastStatus
|
return cron.lastStatus
|
||||||
}
|
}
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// TimeUp 是否时间快到
|
// TimeUp 是否时间快到
|
||||||
func (cron *Crontab) TimeUp() bool {
|
func (cron *Crontab) TimeUp() bool {
|
||||||
|
@ -111,6 +109,10 @@ func (cron *Crontab) TimeUp() bool {
|
||||||
// NextTime 返回下次任务的时间
|
// NextTime 返回下次任务的时间
|
||||||
func (cron *Crontab) NextTime() time.Time {
|
func (cron *Crontab) NextTime() time.Time {
|
||||||
if cron.interval != nil {
|
if cron.interval != nil {
|
||||||
|
if !cron.isCalculated {
|
||||||
|
now := time.Now()
|
||||||
|
cron.intervalCalculateNextTime(now)
|
||||||
|
}
|
||||||
return cron.nextTime
|
return cron.nextTime
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -136,6 +138,7 @@ func (cron *Crontab) FromString(crontab string) error {
|
||||||
// "f1-2|5-10x5,f1|10m,10-15,f1"
|
// "f1-2|5-10x5,f1|10m,10-15,f1"
|
||||||
|
|
||||||
cron.lastStatus = true
|
cron.lastStatus = true
|
||||||
|
cron.isCalculated = false
|
||||||
cron.interval = clinked.NewCircularLinked()
|
cron.interval = clinked.NewCircularLinked()
|
||||||
var intervalList []interface{}
|
var intervalList []interface{}
|
||||||
intervalList = parseIntervalString(matches[0])
|
intervalList = parseIntervalString(matches[0])
|
||||||
|
@ -214,17 +217,15 @@ func (cron *Crontab) linuxTimeUp() bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cron *Crontab) intervalTimeUp() bool {
|
// IntervalCalculateNextTime 计算时间间隔的下次时间
|
||||||
|
func (cron *Crontab) intervalCalculateNextTime(now time.Time) {
|
||||||
now := time.Now()
|
|
||||||
if now.Unix() >= cron.nextTime.Unix() {
|
|
||||||
|
|
||||||
iv := cron.interval.Cursor().GetValue().(hInterval)
|
iv := cron.interval.Cursor().GetValue().(hInterval)
|
||||||
isecond := 0
|
isecond := 0
|
||||||
if cron.lastStatus == false && len(iv.PlanFail) > 0 {
|
if cron.lastStatus == false && len(iv.PlanFail) > 0 {
|
||||||
idx := randomdata.Number(len(iv.PlanFail))
|
idx := randomdata.Number(len(iv.PlanFail))
|
||||||
lr := iv.PlanFail[idx]
|
lr := iv.PlanFail[idx]
|
||||||
isecond = randomdata.Number(lr.left, lr.right+1)
|
isecond = randomdata.Number(lr.left, lr.right+1)
|
||||||
|
log.Println("fail time ", isecond)
|
||||||
} else {
|
} else {
|
||||||
idx := randomdata.Number(len(iv.PlanNormal))
|
idx := randomdata.Number(len(iv.PlanNormal))
|
||||||
lr := iv.PlanNormal[idx]
|
lr := iv.PlanNormal[idx]
|
||||||
|
@ -236,8 +237,15 @@ func (cron *Crontab) intervalTimeUp() bool {
|
||||||
iv.reset()
|
iv.reset()
|
||||||
cron.interval.MoveNext()
|
cron.interval.MoveNext()
|
||||||
}
|
}
|
||||||
|
|
||||||
cron.nextTime = now.Add(time.Duration(isecond) * time.Second)
|
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
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user