From a4b4afdffd95de81a012ed98bd74558150fd02ec Mon Sep 17 00:00:00 2001 From: huangsimin Date: Wed, 5 Dec 2018 14:51:36 +0800 Subject: [PATCH] =?UTF-8?q?crontab=20=E6=97=B6=E9=97=B4=E9=80=9A=E8=BF=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crontab.go | 8 ++++---- crontab_test.go | 47 +++++++---------------------------------------- trie_year.go | 2 +- 3 files changed, 12 insertions(+), 45 deletions(-) diff --git a/crontab.go b/crontab.go index 6d34924..29f9f26 100644 --- a/crontab.go +++ b/crontab.go @@ -72,7 +72,7 @@ func (cron *Crontab) FromString(crontab string) error { // CreateYearPlan 创建年度计划 func (cron *Crontab) CreateYearPlan() { - cron.YearPlan = newTrieYear(time.Now().Year()) + cron.YearPlan = newTrieYear() cron.YearPlan.FromCrontab(cron) } @@ -105,13 +105,13 @@ func (cron *Crontab) TimeUp(now time.Time) bool { istimeup = true } else { if istimeup { - if i > 0 { - cron.SkipPlans = append(cron.SkipPlans, cron.WillPlans[0:i]...) + if i-1 > 0 { + cron.SkipPlans = append(cron.SkipPlans, cron.WillPlans[0:i-1]...) if len(cron.SkipPlans) >= maxlen+200 { cron.SkipPlans = cron.SkipPlans[200:] } } - cron.WillPlans = cron.WillPlans[i+1:] + cron.WillPlans = cron.WillPlans[i:] return istimeup } diff --git a/crontab_test.go b/crontab_test.go index 9637c26..177dd22 100644 --- a/crontab_test.go +++ b/crontab_test.go @@ -2,7 +2,6 @@ package curl2info import ( "fmt" - "log" "runtime" "testing" "time" @@ -15,53 +14,21 @@ import ( func TestParseCrontab(t *testing.T) { // crontab := "0-5/2,7-30/3,30,35,40-^1 * * * *" //(秒) 分 时 号(每月的多少号, 要注意月可可能性) 星期几(每个星期的) /每 ,列表 -范围 crontab := "* * * * *" - t.Error("") - // t.Error(NewCrontab(crontab)) - var s []*TrieYear PrintMemUsage() - for i := 0; i < 100; i++ { - ty := newTrieYear(2018) - ty.FromCrontab(NewCrontab(crontab)) - s = append(s, ty) - } - - ty := newTrieYear(2018) + ty := newTrieYear() cron := NewCrontab(crontab) ty.FromCrontab(cron) - t1 := time.Now() - 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) + if len(ty.GetPlanTime(cron, time.Now(), 10)) != 10 { + t.Error("GetPlanTime error len != 10") } - // for { - - // i := ty.TimeUp() - // if i != nil { - // if i.GetStatus() { - // spew.Dump(i) - // log.Println(time.Now()) - // break - // } - // } - // time.Sleep(time.Millisecond * 200) - // } + cron.CreateYearPlan() + if !cron.TimeUp(time.Now()) { + t.Error("timeup error") + } PrintMemUsage() diff --git a/trie_year.go b/trie_year.go index 2018e4d..85088cb 100644 --- a/trie_year.go +++ b/trie_year.go @@ -98,7 +98,7 @@ func (ty *TrieYear) clearHour() { } -func newTrieYear(year int) *TrieYear { +func newTrieYear() *TrieYear { ty := TrieYear{} ty.Year = time.Now().Year() return &ty