From 6fb5a9d09da5f67584afa04856d3161db7f25584 Mon Sep 17 00:00:00 2001 From: 1 <1@72wo.cn> Date: Mon, 22 Sep 2025 06:17:39 +0000 Subject: [PATCH] =?UTF-8?q?refactor(global):=20=E5=B0=86Cron.Start()?= =?UTF-8?q?=E6=94=B9=E4=B8=BAgo=20Cron.Run()=E5=B9=B6=E7=A7=BB=E9=99=A4cro?= =?UTF-8?q?nex.Start()=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common/cool/global.go | 4 ++-- common/utils/cronex/cronex.go | 6 ------ common/utils/cronex/cronex_test.go | 6 +++--- 3 files changed, 5 insertions(+), 11 deletions(-) diff --git a/common/cool/global.go b/common/cool/global.go index e1469b51..58cecf47 100644 --- a/common/cool/global.go +++ b/common/cool/global.go @@ -42,8 +42,8 @@ func init() { sensitive.StoreOption{Type: sensitive.StoreMemory}, // 基于内存 sensitive.FilterOption{Type: sensitive.FilterDfa}, // 基于DFA算法 ) - Cron.Start() - + go Cron.Run() + // if err != nil { // log.Fatalf("敏感词服务启动失败, err:%v", err) // return diff --git a/common/utils/cronex/cronex.go b/common/utils/cronex/cronex.go index dd1b2092..c0371160 100644 --- a/common/utils/cronex/cronex.go +++ b/common/utils/cronex/cronex.go @@ -40,12 +40,6 @@ func (c *Cronex) Run() { c.Run() } -// 异步运行消费者循环 -func (c *Cronex) Start() { - //ants.Submit(c.Run) - go c.Run() -} - // 关闭cronex的任务循环 func (c *Cronex) Stop() { c.Stop() diff --git a/common/utils/cronex/cronex_test.go b/common/utils/cronex/cronex_test.go index 86b91dca..60c11755 100644 --- a/common/utils/cronex/cronex_test.go +++ b/common/utils/cronex/cronex_test.go @@ -15,7 +15,7 @@ func Test_Cronex(t *testing.T) { } cron := New() - cron.Start() //运行事件循环 + go cron.Run() count := 2 durationChan := make(chan time.Duration, count) @@ -82,7 +82,7 @@ func Test_Cronex_ParseNext(t *testing.T) { left := first + time.Duration(0.8*float64(time.Second)) right := first + time.Duration(1.2*float64(time.Second)) - tv := now.Sub(time.Now()) + tv := time.Until(now) if tv < left || tv > right { t.Logf("tv(%v), tv < left(%v) || tv > right(%v)", tv, left, right) return @@ -95,7 +95,7 @@ func Test_Multiple(t *testing.T) { cron := New() count := int32(0) - cron.Start() + go cron.Run() max := int32(10) for i := int32(0); i < max; i++ { cron.AddFunc("* * * * * *", func() {