rag/internal/cmd/schedule.go
2024-06-16 00:55:25 +08:00

60 lines
1.2 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package cmd
import (
"fmt"
"framework_v2/internal/app/config"
"framework_v2/internal/app/ent"
"framework_v2/internal/app/jobs"
"framework_v2/internal/app/logger"
"framework_v2/internal/app/redis"
"github.com/spf13/cobra"
"time"
)
var scheduleCommand = &cobra.Command{
Use: "schedule",
Run: func(cmd *cobra.Command, args []string) {
config.InitConfig()
logger.InitLogger()
ent.InitEnt()
redis.InitRedis()
jobs.InitAsynQClient()
runSchedule()
},
}
func runSchedule() {
if config.Config.DebugMode.Enable {
fmt.Println("调试模式开启,直接触发。")
// RUN JOB
return
}
// 获取下一个整点时间
nextHour := time.Now().Truncate(time.Hour).Add(time.Hour)
// 计算当前时间到下一个整点的时间差
duration := nextHour.Sub(time.Now())
// 设置定时器,在下一个整点触发
timer := time.NewTimer(duration)
// 用无限循环,在每个小时整点执行任务
for {
select {
case <-timer.C:
// 每小时整点执行的代码
fmt.Printf("执行任务,当前时间:%v\n", time.Now().Format("2006-01-02 15:04:05"))
// DO JOB.
// 重置定时器设置为1小时后再次触发
timer.Reset(time.Hour)
}
}
}