From 043b543cfad32585aae5a95fba482371199b8acb Mon Sep 17 00:00:00 2001 From: fumiama Date: Tue, 26 Oct 2021 16:05:28 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20=E4=BF=AE=E5=A4=8D=20timer=20?= =?UTF-8?q?=E8=AE=A1=E6=97=B6=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plugin_manager/manager.go | 5 ++++- plugin_manager/timer/timer.go | 24 ++++++++++++++++++++---- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/plugin_manager/manager.go b/plugin_manager/manager.go index f6408e82..0f355155 100644 --- a/plugin_manager/manager.go +++ b/plugin_manager/manager.go @@ -57,7 +57,10 @@ var ( func init() { // 插件主体 loadConfig() - clock = timer.NewClock(timerfile) + go func() { + time.Sleep(time.Second + time.Millisecond*time.Duration(rand.Intn(1000))) + clock = timer.NewClock(timerfile) + }() // 菜单 zero.OnFullMatch("群管系统", zero.AdminPermission).SetBlock(true).FirstPriority(). Handle(func(ctx *zero.Ctx) { diff --git a/plugin_manager/timer/timer.go b/plugin_manager/timer/timer.go index 7899c000..3fe9be52 100644 --- a/plugin_manager/timer/timer.go +++ b/plugin_manager/timer/timer.go @@ -62,7 +62,7 @@ func (ts *Timer) nextDuration() time.Duration { isThisMinute := ts.Minute < 0 || ts.Minute == int32(time.Now().Minute()) if !isThisMinute { d, over := nextDistance(ts.Minute, time.Now().Minute(), time.Minute, time.Hour) - if !(ts.Hour > 0 && over) { + if !(ts.Hour >= 0 && over) { sleepdur = d } } @@ -92,15 +92,31 @@ func (c *Clock) RegisterTimer(ts *Timer, save bool) { } log.Printf("[群管]注册计时器[%t]%s", ts.Enable, key) for ts.Enable { - dur := time.Minute + dur := ts.nextDuration() isThisMonth := ts.Month < 0 || ts.Month == int32(time.Now().Month()) if isThisMonth { isThisDay := ts.Day < 0 || ts.Day == int32(time.Now().Day()) isThisWeek := ts.Week < 0 || ts.Week == int32(time.Now().Weekday()) - if isThisDay || isThisWeek { - dur = ts.nextDuration() + if !isThisDay && !isThisWeek { + if ts.Day == 0 { + d, over := nextDistance(ts.Week, int(time.Now().Weekday()), time.Hour*24*7, time.Hour*24*7) + if !(over && ts.Month > 0 && time.Now().Add(time.Hour*24*7).Month() != time.Now().Month()) { + dur += d + } + } else if ts.Day < int32(time.Now().Day()) { + if ts.Month < 0 { + dur += time.Until(time.Date(time.Now().Year(), time.Month(ts.Month)+1, int(ts.Day), time.Now().Hour(), time.Now().Minute(), time.Now().Second(), time.Now().Nanosecond(), time.Now().Location())) + } else { + dur += time.Until(time.Date(time.Now().Year()+1, time.Month(ts.Month), int(ts.Day), time.Now().Hour(), time.Now().Minute(), time.Now().Second(), time.Now().Nanosecond(), time.Now().Location())) + } + } } + } else if ts.Month < int32(time.Now().Month()) { + dur += time.Until(time.Date(time.Now().Year()+1, time.Month(ts.Month), int(ts.Day), time.Now().Hour(), time.Now().Minute(), time.Now().Second(), time.Now().Nanosecond(), time.Now().Location())) + } else { + dur += time.Until(time.Date(time.Now().Year(), time.Month(ts.Month), int(ts.Day), time.Now().Hour(), time.Now().Minute(), time.Now().Second(), time.Now().Nanosecond(), time.Now().Location())) } + log.Printf("[群管]计时器%s将睡眠%ds", key, dur/time.Second) time.Sleep(dur) if ts.Enable { zero.RangeBot(func(id int64, ctx *zero.Ctx) bool {