diff --git a/initialize/router.go b/initialize/router.go index 2620dc3..ba59cd0 100644 --- a/initialize/router.go +++ b/initialize/router.go @@ -4,6 +4,8 @@ import ( "github.com/gin-gonic/gin" swaggerFiles "github.com/swaggo/files" ginSwagger "github.com/swaggo/gin-swagger" + r "miniapp/model/common/response" + "miniapp/task" "net/http" "miniapp/global" @@ -44,6 +46,19 @@ func Routers() *gin.Engine { PublicGroup.GET("/health", func(c *gin.Context) { c.JSON(http.StatusOK, "ok") }) + PublicGroup.GET("/task/msg", func(c *gin.Context) { + tag, _ := task.WxTask.FindJobsByTag("msg") + r.OkWithData(tag[0].NextRun(), c) + }) + PublicGroup.GET("/task/jobs", func(c *gin.Context) { + jobs := task.WxTask.Jobs() + r.OkWithData(len(jobs), c) + }) + PublicGroup.GET("/task/send", func(c *gin.Context) { + tag, _ := task.WxTask.FindJobsByTag("sendMsg") + r.OkWithData(tag[0].NextRun(), c) + }) + } { systemRouter.InitBaseRouter(PublicGroup) // 注册基础功能路由 不做鉴权 diff --git a/task/task.go b/task/task.go index 2645bc7..cca036f 100644 --- a/task/task.go +++ b/task/task.go @@ -13,15 +13,24 @@ func InitTask() { WxTask = gocron.NewScheduler(time.Local) // 每天凌晨1点执行 - _, err := WxTask.Every(1).Day().At("01:00").Do(CheckUserSurgeryDate) // 检查用户是否已到手术日期 + //cjob, err := WxTask.Cron("0 1 * * *").Do(CheckUserSurgeryDate) // 检查用户是否已到手术日期 + cjob, err := WxTask.Every(1).Day().At("04:00").Do(CheckUserSurgeryDate) // 检查用户是否已到手术日期 if err != nil { global.GVA_LOG.Error("检查用户是否已到手术日期定时任务创建失败", zap.Error(err)) } - _, err = WxTask.Every(1).Day().At("01:00").Do(SendMsg2User) + sjob, err := WxTask.Every(1).Day().At("04:00").Do(SendMsg2User) if err != nil { global.GVA_LOG.Error("发送消息定时任务创建失败", zap.Error(err)) } - + cjob.Tag("cjbo") + sjob.Tag("msg") + //go func() { + // for { + // fmt.Println("cjob Next run:", cjob.NextRun()) + // fmt.Println("sjob Next run:", sjob.NextRun()) + // time.Sleep(time.Second) + // } + //}() // 开启定时任务 WxTask.StartAsync() global.GVA_LOG.Info("定时任务已开启", zap.Any("定时任务状态:", WxTask.IsRunning())) diff --git a/task/user.go b/task/user.go index ccfcc67..dada241 100644 --- a/task/user.go +++ b/task/user.go @@ -52,11 +52,12 @@ func SendMsg2User() { job, err := WxTask.Every(1).Day().At(todo.RemindTime).LimitRunsTo(1).Do(MiniappSendMsg, strconv.Itoa(int(user.ID)), strconv.Itoa(int(todo.ID))) //job, err := WxTask.Cron("0 30 23 30 3 2024").Do(MiniappSendMsg, strconv.Itoa(int(user.ID)), strconv.Itoa(int(todo.ID))) //job, err := WxTask.Every(1).Day().At("23:51").LimitRunsTo(1).Do(MiniappSendMsg, strconv.Itoa(int(user.ID)), strconv.Itoa(int(todo.ID))) + job.Tag("sendMsg") if err != nil { global.GVA_LOG.Error("定时任务创建失败:%s", zap.Error(err)) return } - global.GVA_LOG.Info("定时任务创建成功", zap.Any("job", job.LastRun())) + global.GVA_LOG.Info("定时发送消息任务创建成功") } } }