package main
import (
"fmt"
"github.com/SimonWang00/goeureka"
"github.com/gin-gonic/gin"
"github.com/robfig/cron/v3"
"log"
"net/http"
"time"
"wbGo/task"
)
func main() {
router := gin.Default()
num := 0 // 运行次数
// 设置时区
local, _ := time.LoadLocation("Asia/Shanghai")
interval := cron.New(cron.WithLocation(local), cron.WithSeconds()) // 设置时区并且精度按秒。
timeout := "*/2 * * * * *"
intervalId, err := interval.AddFunc(timeout, func() {
num++
log.Println("全局定时器已开启=num=", num)
})
if err != nil {
log.Println("全局定时器报错", "\n error=", err, "\n num=", num)
return
}
log.Println("全局定时器已开启,Id=", intervalId)
interval.Start()
router.Run("0.0.0.0:8000")
}
gin定时任务,2秒一次
于 2024-09-10 15:26:23 首次发布