From 9b2042b0756c96791c31c0a667a05c10bce4e210 Mon Sep 17 00:00:00 2001 From: ZJY <764763903@qq.com> Date: Tue, 7 Sep 2021 01:19:40 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B5=8B=E8=AF=95=E5=AE=9A=E6=97=B6=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- models/init.go | 2 +- models/wskey.go | 28 +++++++++++----------------- 2 files changed, 12 insertions(+), 18 deletions(-) diff --git a/models/init.go b/models/init.go index a5e84f2..e9bd805 100644 --- a/models/init.go +++ b/models/init.go @@ -31,5 +31,5 @@ func init() { InitReplies() initTask() initRepos() - intiSky(pri) + intiSky() } diff --git a/models/wskey.go b/models/wskey.go index cdbe5ee..0b3a05e 100644 --- a/models/wskey.go +++ b/models/wskey.go @@ -1,25 +1,19 @@ package models import ( - "github.com/beego/beego/v2/core/logs" - "time" + "fmt" + "github.com/robfig/cron/v3" ) -func intiSky(f func()) { - go func() { - for { - f() - now := time.Now() - // 计算下一个零点 - next := now.Add(time.Hour * 24) - next = time.Date(next.Year(), next.Month(), next.Day(), 0, 0, 0, 0, next.Location()) - t := time.NewTimer(next.Sub(now)) - <-t.C - } - }() -} +func intiSky() { + c := cron.New(cron.WithSeconds()) //精确到秒 -func pri() { - logs.Info("测试启用") + //定时任务 + spec := "1 * * * * ?" //cron表达式,每秒一次 + c.AddFunc(spec, func() { + fmt.Println("11111") + }) + c.Start() + select {} //阻塞主线程停止 }