计划表测试通过
This commit is contained in:
parent
4df88d8680
commit
f82f388867
52
crontab.go
52
crontab.go
@ -3,6 +3,7 @@ package curl2info
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
@ -75,6 +76,57 @@ func (cron *Crontab) CreateYearPlan() {
|
|||||||
cron.YearPlan.FromCrontab(cron)
|
cron.YearPlan.FromCrontab(cron)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TimeUp 是否时间快到
|
||||||
|
func (cron *Crontab) TimeUp(now time.Time) bool {
|
||||||
|
|
||||||
|
maxlen := 1000
|
||||||
|
createlen := 500
|
||||||
|
|
||||||
|
plen := len(cron.WillPlans)
|
||||||
|
if plen <= createlen {
|
||||||
|
var lastplan time.Time
|
||||||
|
if plen == 0 {
|
||||||
|
lastplan = now
|
||||||
|
} else {
|
||||||
|
lastplan = cron.WillPlans[plen-1].Add(time.Minute)
|
||||||
|
}
|
||||||
|
if !cron.YearPlan.CheckYear() {
|
||||||
|
cron.CreateYearPlan()
|
||||||
|
}
|
||||||
|
|
||||||
|
timeplans := cron.YearPlan.GetPlanTime(cron, lastplan, uint(maxlen-plen))
|
||||||
|
cron.WillPlans = append(cron.WillPlans, timeplans...)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(cron.WillPlans) > 0 {
|
||||||
|
istimeup := false
|
||||||
|
for i := 0; i < maxlen; i++ {
|
||||||
|
if now.Unix() >= cron.WillPlans[i].Unix() {
|
||||||
|
istimeup = true
|
||||||
|
} else {
|
||||||
|
if istimeup {
|
||||||
|
if i > 0 {
|
||||||
|
cron.SkipPlans = append(cron.SkipPlans, cron.WillPlans[0:i]...)
|
||||||
|
if len(cron.SkipPlans) >= maxlen+200 {
|
||||||
|
cron.SkipPlans = cron.SkipPlans[200:]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
cron.WillPlans = cron.WillPlans[i+1:]
|
||||||
|
return istimeup
|
||||||
|
}
|
||||||
|
|
||||||
|
return istimeup
|
||||||
|
}
|
||||||
|
}
|
||||||
|
cron.SkipPlans = cron.WillPlans
|
||||||
|
cron.WillPlans = nil
|
||||||
|
return istimeup
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Panicln("error willplans range")
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
func createTimePointer(min string, llimit, rlimit int, fixedLeftRight bool) []timePointer {
|
func createTimePointer(min string, llimit, rlimit int, fixedLeftRight bool) []timePointer {
|
||||||
|
|
||||||
var result []timePointer
|
var result []timePointer
|
||||||
|
@ -14,14 +14,14 @@ import (
|
|||||||
|
|
||||||
func TestParseCrontab(t *testing.T) {
|
func TestParseCrontab(t *testing.T) {
|
||||||
// crontab := "0-5/2,7-30/3,30,35,40-^1 * * * *" //(秒) 分 时 号(每月的多少号, 要注意月可可能性) 星期几(每个星期的) /每 ,列表 -范围
|
// crontab := "0-5/2,7-30/3,30,35,40-^1 * * * *" //(秒) 分 时 号(每月的多少号, 要注意月可可能性) 星期几(每个星期的) /每 ,列表 -范围
|
||||||
crontab := "* * * * 6-6"
|
crontab := "* * * * *"
|
||||||
t.Error("")
|
t.Error("")
|
||||||
// t.Error(NewCrontab(crontab))
|
// t.Error(NewCrontab(crontab))
|
||||||
|
|
||||||
var s []*TrieYear
|
var s []*TrieYear
|
||||||
PrintMemUsage()
|
PrintMemUsage()
|
||||||
|
|
||||||
for i := 0; i < 10000; i++ {
|
for i := 0; i < 100; i++ {
|
||||||
ty := newTrieYear(2018)
|
ty := newTrieYear(2018)
|
||||||
ty.FromCrontab(NewCrontab(crontab))
|
ty.FromCrontab(NewCrontab(crontab))
|
||||||
s = append(s, ty)
|
s = append(s, ty)
|
||||||
@ -30,7 +30,25 @@ func TestParseCrontab(t *testing.T) {
|
|||||||
ty := newTrieYear(2018)
|
ty := newTrieYear(2018)
|
||||||
cron := NewCrontab(crontab)
|
cron := NewCrontab(crontab)
|
||||||
ty.FromCrontab(cron)
|
ty.FromCrontab(cron)
|
||||||
|
|
||||||
|
t1 := time.Now()
|
||||||
log.Println(ty.GetPlanTime(cron, time.Now(), 10))
|
log.Println(ty.GetPlanTime(cron, time.Now(), 10))
|
||||||
|
log.Println(time.Since(t1))
|
||||||
|
|
||||||
|
cron.CreateYearPlan()
|
||||||
|
|
||||||
|
cur := time.Now().Add(time.Minute * 2)
|
||||||
|
for i := 0; i < 510; i++ {
|
||||||
|
|
||||||
|
if i >= 499 {
|
||||||
|
// log.Println("skip:", cron.SkipPlans, len(cron.SkipPlans))
|
||||||
|
log.Println(cur, "will:", cron.WillPlans[0:10], len(cron.WillPlans))
|
||||||
|
log.Println("skip:", cron.SkipPlans)
|
||||||
|
}
|
||||||
|
log.Println(cron.TimeUp(cur))
|
||||||
|
cur = cur.Add(time.Minute * 1) // 模拟出来的分绝对一分钟, 程序损耗会多几毫秒
|
||||||
|
// time.Sleep(time.Minute * 1)
|
||||||
|
}
|
||||||
|
|
||||||
// for {
|
// for {
|
||||||
|
|
||||||
|
56
trie_year.go
56
trie_year.go
@ -8,23 +8,6 @@ var defaultTime = time.Now()
|
|||||||
|
|
||||||
type SecondNode int
|
type SecondNode int
|
||||||
|
|
||||||
// type INode interface {
|
|
||||||
// Get() interface{}
|
|
||||||
// Set(value interface{})
|
|
||||||
// }
|
|
||||||
|
|
||||||
// type TYNode struct {
|
|
||||||
// Value interface{}
|
|
||||||
// }
|
|
||||||
|
|
||||||
// func (min *TYNode) Get() interface{} {
|
|
||||||
// return min.Value
|
|
||||||
// }
|
|
||||||
|
|
||||||
// func (min *TYNode) Set(value interface{}) {
|
|
||||||
// min.Value = value
|
|
||||||
// }
|
|
||||||
|
|
||||||
type MinuteNode struct {
|
type MinuteNode struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -169,7 +152,6 @@ func (ty *TrieYear) GetPlanTime(cron *Crontab, aftertime time.Time, count uint)
|
|||||||
if hour != nil {
|
if hour != nil {
|
||||||
|
|
||||||
for n := 0; n <= 59; n++ {
|
for n := 0; n <= 59; n++ {
|
||||||
|
|
||||||
if nmonth == i && nday == j && nhour == k {
|
if nmonth == i && nday == j && nhour == k {
|
||||||
if n < nminute {
|
if n < nminute {
|
||||||
continue
|
continue
|
||||||
@ -186,7 +168,6 @@ func (ty *TrieYear) GetPlanTime(cron *Crontab, aftertime time.Time, count uint)
|
|||||||
ty.clearHour()
|
ty.clearHour()
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -198,38 +179,10 @@ func (ty *TrieYear) GetPlanTime(cron *Crontab, aftertime time.Time, count uint)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ty.clearHour()
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
// Contain 包含
|
|
||||||
// func (ty *TrieYear) Contain(now *time.Time) INode {
|
|
||||||
|
|
||||||
// // 月
|
|
||||||
// month := ty.Month[int(now.Month())]
|
|
||||||
|
|
||||||
// if month != nil {
|
|
||||||
|
|
||||||
// // 天
|
|
||||||
// day := month.Day[now.Day()]
|
|
||||||
|
|
||||||
// if day != nil {
|
|
||||||
|
|
||||||
// var hour *HourNode
|
|
||||||
// hour = day.Hour[now.Hour()]
|
|
||||||
|
|
||||||
// var min *MinuteNode
|
|
||||||
// if hour != nil {
|
|
||||||
// min = hour.Minute[now.Minute()]
|
|
||||||
// }
|
|
||||||
|
|
||||||
// return min
|
|
||||||
// }
|
|
||||||
|
|
||||||
// }
|
|
||||||
|
|
||||||
// return nil
|
|
||||||
// }
|
|
||||||
|
|
||||||
// FromCrontab 从Crontab生成树
|
// FromCrontab 从Crontab生成树
|
||||||
func (ty *TrieYear) FromCrontab(cron *Crontab) {
|
func (ty *TrieYear) FromCrontab(cron *Crontab) {
|
||||||
// 月的填充
|
// 月的填充
|
||||||
@ -253,13 +206,14 @@ func filterDay(cron *Crontab, curday *DayNode) bool {
|
|||||||
if w.isAll {
|
if w.isAll {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
for n := w.left; n <= w.right; n += w.per {
|
for n := w.left; n <= w.right; n += w.per {
|
||||||
if n != int(curday.Week) {
|
if n == int(curday.Week) {
|
||||||
return true
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func insertDay(cron *Crontab, curMonth *MonthNode) {
|
func insertDay(cron *Crontab, curMonth *MonthNode) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user