测试定时器
This commit is contained in:
parent
fbd3cf2830
commit
9b2042b075
@ -31,5 +31,5 @@ func init() {
|
||||
InitReplies()
|
||||
initTask()
|
||||
initRepos()
|
||||
intiSky(pri)
|
||||
intiSky()
|
||||
}
|
||||
|
||||
@ -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 {} //阻塞主线程停止
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user