测试定时器

This commit is contained in:
ZJY 2021-09-07 01:19:40 +08:00
parent fbd3cf2830
commit 9b2042b075
2 changed files with 12 additions and 18 deletions

View File

@ -31,5 +31,5 @@ func init() {
InitReplies()
initTask()
initRepos()
intiSky(pri)
intiSky()
}

View File

@ -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 {} //阻塞主线程停止
}