添加Force强制Sleep, 和 重新覆盖crontab计划.

把原来SetLastStaus改为一个多元化的设置方式
This commit is contained in:
eson 2018-12-22 02:09:31 +08:00
parent 1140087c45
commit 74fddef18d
3 changed files with 83 additions and 307 deletions

86
base.go
View File

@ -1,86 +0,0 @@
package crontab
import (
"strconv"
"strings"
"474420502.top/eson/structure/priority_list"
randomdata "github.com/Pallinder/go-randomdata"
)
type randLR struct {
left, right int
}
// NodeCount 用于priority_list
type NodeCount struct {
plist.Node
randLR
}
// Compare NodeCount比较函数
func (rlr *NodeCount) Compare(v plist.INode) bool {
return rlr.GetValue().(int) > v.GetValue().(int)
}
func parseRandLR(lrvalue string) randLR {
vlen := len(lrvalue)
lastchar := lrvalue[vlen-1]
lr := strings.Split(lrvalue, "-")
switch len(lr) {
case 1:
lr := randLR{parseTimeValue(lr[0], lastchar), parseTimeValue(lr[0], lastchar)}
return lr
case 2:
lr := randLR{parseTimeValue(lr[0], lastchar), parseTimeValue(lr[1], lastchar)}
return lr
default:
panic("lr is error")
}
}
// intervalPriorityListISecond 获取优先链表比较的值
func intervalPriorityListISecond(planlist *plist.PriorityList, count int) int {
if planlist.Size() > 0 {
node := new(NodeCount)
node.SetValue(count)
iwantNode := planlist.GetCompare(node)
if iwantNode != nil {
wantNode := iwantNode.(*NodeCount)
lr := wantNode.randLR
return randomdata.Number(lr.left, lr.right+1)
}
}
return -1
}
func getInt(v string) int {
vint, err := strconv.Atoi(v)
if err != nil {
panic(err)
}
return vint
}
func parseTimeValue(v string, lastchar byte) int {
vlen := len(v)
switch lastchar {
case 's':
return getInt(v[:vlen-1])
case 'm':
return getInt(v[:vlen-1]) * 60
case 'h':
return getInt(v[:vlen-1]) * 3600
case 'd':
return getInt(v[:vlen-1]) * 3600 * 24
default:
return getInt(v)
}
}

View File

@ -4,46 +4,46 @@ import (
"errors"
"fmt"
"log"
"reflect"
"regexp"
"strconv"
"strings"
"time"
"474420502.top/eson/structure/circular_linked"
"474420502.top/eson/structure/priority_list"
"github.com/Pallinder/go-randomdata"
"github.com/davecgh/go-spew/spew"
)
type hInterval struct {
PlanFailCount plist.PriorityList
PlanTrueCount plist.PriorityList
// StatusType 设置状态的类型
type StatusType int
PlanFail []randLR
PlanTrue []randLR
const (
_ StatusType = iota
// SExecuteOK 设置这个次成功或者失败的状态
SExecuteOK
// SExecuteSleep 设置下次Sleep时间, 只影响一次
SExecuteSleep
// SExecuteCrontab 设置改写Crontab, 覆盖以前的Crontab
SExecuteCrontab
)
Count int
ConstCount int
// Force 强制下次执行
type Force struct {
sleep time.Duration
}
func (interval *hInterval) reset() {
interval.Count = interval.ConstCount
}
type timePointer struct {
left, right int
leftlimit, rightlimit int
per int
isAll bool
}
func (tp *timePointer) String() string {
return fmt.Sprintf("left: %d, right: %d, leftlimit: %d, rightlimit: %d, per: %d", tp.left, tp.right, tp.leftlimit, tp.rightlimit, tp.per)
// NextTime 下次执行时间
func (force *Force) NextTime() time.Time {
return time.Now().Add(force.sleep)
}
// Crontab 的string解析
type Crontab struct {
crontab string
force *Force
min []timePointer
hour []timePointer
day []timePointer
@ -67,7 +67,8 @@ type Crontab struct {
// NewCrontab create 一个crontab
func NewCrontab(crontab string) *Crontab {
cron := &Crontab{}
cron.FromString(crontab)
cron.crontab = strings.TrimSpace(crontab)
cron.FromString(cron.crontab)
return cron
}
@ -87,17 +88,40 @@ func (cron *Crontab) UnmarshalYAML(unmarshal func(interface{}) error) error {
}
// SetStatus 设置上次状态 true false
func (cron *Crontab) SetStatus(status bool) {
func (cron *Crontab) SetStatus(statusType StatusType, statusValue ...interface{}) {
if cron.interval != nil {
cron.lastStatus = status
switch statusType {
case SExecuteOK:
if cron.interval != nil {
cron.lastStatus = statusValue[0].(bool)
}
case SExecuteSleep:
force := new(Force)
ivalue := statusValue[0]
switch value := ivalue.(type) {
case int:
force.sleep = time.Duration(value) * time.Second
case int64:
force.sleep = time.Duration(value) * time.Second
case time.Duration:
force.sleep = value
default:
panic(errors.New("statusValue type is error, the type is" + reflect.TypeOf(ivalue).String()))
}
case SExecuteCrontab:
crontab := statusValue[0].(string)
cron.crontab = strings.TrimSpace(crontab)
cron.FromString(cron.crontab)
default:
panic(errors.New("StatusType is unknown, the type " + reflect.TypeOf(statusType).String()))
}
}
// GetStatus 获取上次状态 true false
func (cron *Crontab) GetStatus() (status bool) {
return cron.lastStatus
}
// // GetStatus 获取上次状态 true false
// func (cron *Crontab) GetStatus() (status bool) {
// return cron.lastStatus
// }
// TimeUp 是否时间快到, 时间间隔调用完TimeUp后必须调用NextTime, 为了精准控制时间
func (cron *Crontab) TimeUp() bool {
@ -111,6 +135,13 @@ func (cron *Crontab) TimeUp() bool {
// NextTime 返回下次任务的时间
func (cron *Crontab) NextTime() time.Time {
if cron.force != nil {
nt := cron.force.NextTime()
cron.force = nil
return nt
}
if cron.interval != nil {
if !cron.isCalculated {
now := time.Now()
@ -132,7 +163,19 @@ func (cron *Crontab) String() string {
// FromString 解析crontab 的 表达式
func (cron *Crontab) FromString(crontab string) error {
crontab = strings.TrimSpace(crontab)
crontab = cron.crontab
cron.interval = nil
cron.min = nil
cron.hour = nil
cron.day = nil
cron.month = nil
cron.week = nil
cron.WillPlans = nil
cron.SkipPlans = nil
cron.YearPlan = nil
matches := regexp.MustCompile("[^ ]+").FindAllString(crontab, -1)
mlen := len(matches)
@ -146,7 +189,7 @@ func (cron *Crontab) FromString(crontab string) error {
var intervalList []interface{}
intervalList = parseIntervalString(matches[0])
cron.interval.Append(intervalList...)
cron.NextTime()
cron.TimeUp()
case 5:
cron.min = createTimePointer(matches[0], 0, 59, true)
cron.hour = createTimePointer(matches[1], 0, 23, true)
@ -155,7 +198,7 @@ func (cron *Crontab) FromString(crontab string) error {
cron.week = createTimePointer(matches[4], 0, 6, true)
cron.createYearPlan()
cron.NextTime()
cron.TimeUp()
default:
return errors.New("mathches len != want, check crontab string")
}
@ -290,184 +333,3 @@ func (cron *Crontab) intervalTimeUp() bool {
return false
}
func createTimePointer(min string, llimit, rlimit int, fixedLeftRight bool) []timePointer {
var result []timePointer
exelist := strings.Split(min, ",")
for _, exe := range exelist {
tp := timePointer{}
takeper := strings.Split(exe, "/") // per
var rangevalue, per string
if len(takeper) == 1 {
rangevalue = exe
per = "1"
} else {
rangevalue = takeper[0]
per = takeper[1]
}
// takeRange
be := strings.Split(rangevalue, "-")
var left, rigth string
switch len(be) {
case 1:
left = be[0]
rigth = be[0]
case 2:
left = be[0]
rigth = be[1]
default:
panic(errors.New("range value is > 2"))
}
if left == "*" {
tp.left = llimit
} else {
ileft, err := strconv.Atoi(strings.Replace(left, "^", "-", -1))
if err != nil {
panic(err)
}
tp.left = ileft
}
if rigth == "*" {
tp.right = rlimit
} else {
iright, err := strconv.Atoi(strings.Replace(rigth, "^", "-", -1))
if err != nil {
panic(err)
}
tp.right = iright
}
iper, err := strconv.Atoi(per)
if err != nil {
panic(err)
}
tp.per = iper
tp.leftlimit = llimit
tp.rightlimit = rlimit
// 修正左值
leftfixed := tp.left
if leftfixed < 0 {
leftfixed += tp.rightlimit + 1
if fixedLeftRight {
tp.left = leftfixed
}
}
rightfixed := tp.right
if rightfixed < 0 {
rightfixed += tp.rightlimit + 1
if fixedLeftRight {
tp.right = rightfixed
}
}
// 全部符合 当左等于左 且 右等于右最大 并且 per == 1
if leftfixed == tp.leftlimit && rightfixed == tp.rightlimit && tp.per == 1 {
tp.isAll = true
}
result = append(result, tp)
}
return result
}
func parseIntervalString(crontab string) []interface{} {
var result []interface{}
values := strings.Split(crontab, ",")
for _, value := range values {
interval := &hInterval{}
// 次数
valuesCounts := strings.Split(value, "x")
switch len(valuesCounts) {
case 1:
interval.ConstCount = 1
case 2:
count, err := strconv.Atoi(valuesCounts[1])
if err != nil {
panic(err)
}
interval.ConstCount = count
default:
panic("valuesCounts error, the len is not in range")
}
// 统计失败与普通间隔值的数组
failAndNormal := valuesCounts[0]
valuesFN := strings.Split(failAndNormal, "|")
for _, FN := range valuesFN {
if FN == "" {
continue
}
switch FN[0] {
case 'f', 'F':
scharIndex := strings.Index(FN, "?")
if scharIndex != -1 {
fc := FN[0:scharIndex]
flr := FN[scharIndex+1:]
node := new(NodeCount)
node.SetValue(getInt(fc[1:]))
node.randLR = parseRandLR(flr)
interval.PlanFailCount.Insert(node)
} else {
fvalue := FN[1:]
interval.PlanFail = append(interval.PlanFail, parseRandLR(fvalue))
}
case 't', 'T':
scharIndex := strings.Index(FN, "?")
if scharIndex != -1 {
tc := FN[0:scharIndex]
tlr := FN[scharIndex+1:]
node := new(NodeCount)
node.SetValue(getInt(tc[1:]))
node.randLR = parseRandLR(tlr)
interval.PlanTrueCount.Insert(node)
} else {
tvalue := FN[1:]
interval.PlanTrue = append(interval.PlanTrue, parseRandLR(tvalue))
}
default:
FN = "t" + FN
scharIndex := strings.Index(FN, "?")
if scharIndex != -1 {
tc := FN[0:scharIndex]
tlr := FN[scharIndex+1:]
node := new(NodeCount)
node.SetValue(getInt(tc[1:]))
node.randLR = parseRandLR(tlr)
interval.PlanTrueCount.Insert(node)
} else {
tvalue := FN[1:]
interval.PlanTrue = append(interval.PlanTrue, parseRandLR(tvalue))
}
}
}
interval.reset()
result = append(result, interval)
}
return result
}

View File

@ -101,31 +101,31 @@ func TestParseIntervalPlus(t *testing.T) {
case 1:
if err := isInRangeTime(sec, 2.0); err != nil {
t.Error(err.Error(), "status = ", cron.GetStatus())
t.Error(err.Error(), "status = ", cron.lastStatus)
}
case 2:
if err := isInRangeTime(sec, 3.0); err != nil {
t.Error(err.Error(), "status = ", cron.GetStatus())
t.Error(err.Error(), "status = ", cron.lastStatus)
}
case 3:
if err := isInRangeTime(sec, 4.0); err != nil {
t.Error(err.Error(), "status = ", cron.GetStatus())
t.Error(err.Error(), "status = ", cron.lastStatus)
}
case 5:
if err := isInRangeTime(sec, 1); err != nil {
t.Error(err.Error(), "status = ", cron.GetStatus())
t.Error(err.Error(), "status = ", cron.lastStatus)
}
default:
if i >= 10 {
if err := isInRangeTime(sec, 5.0); err != nil {
t.Error(err.Error(), "status = ", cron.GetStatus())
t.Error(err.Error(), "status = ", cron.lastStatus)
}
}
}
if i <= 3 {
cron.SetStatus(false)
cron.SetStatus(SExecuteOK, false)
} else {
cron.SetStatus(true)
cron.SetStatus(SExecuteOK, true)
}
now = time.Now()