23 lines
449 B
Go
23 lines
449 B
Go
package test
|
|
|
|
import (
|
|
"github.com/robfig/cron/v3"
|
|
"log"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestCron(t *testing.T) {
|
|
// 创建定时任务调度器.秒级定时器
|
|
cr := cron.New(cron.WithSeconds())
|
|
// 添加定时任务,每分钟执行一次
|
|
cr.AddFunc("*/2 * * * * *", func() {
|
|
log.Println("定时任务执行:", time.Now().Format("2006-01-02 15:04:05"))
|
|
})
|
|
// 启动定时任务调度器
|
|
cr.Start()
|
|
for {
|
|
time.Sleep(1 * time.Second)
|
|
}
|
|
}
|