JM-WechatMini/task/task.go

39 lines
1.1 KiB
Go
Raw Normal View History

2024-03-21 21:08:45 +08:00
package task
import (
"github.com/go-co-op/gocron"
2024-03-29 02:32:26 +08:00
"go.uber.org/zap"
"miniapp/global"
2024-03-21 21:08:45 +08:00
"time"
)
var WxTask *gocron.Scheduler
func InitTask() {
WxTask = gocron.NewScheduler(time.Local)
// 每天凌晨1点执行
2024-03-31 03:45:17 +08:00
//cjob, err := WxTask.Cron("0 1 * * *").Do(CheckUserSurgeryDate) // 检查用户是否已到手术日期
cjob, err := WxTask.Every(1).Day().At("04:00").Do(CheckUserSurgeryDate) // 检查用户是否已到手术日期
2024-03-30 22:58:42 +08:00
if err != nil {
global.GVA_LOG.Error("检查用户是否已到手术日期定时任务创建失败", zap.Error(err))
}
2024-03-31 03:45:17 +08:00
sjob, err := WxTask.Every(1).Day().At("04:00").Do(SendMsg2User)
2024-03-30 22:58:42 +08:00
if err != nil {
global.GVA_LOG.Error("发送消息定时任务创建失败", zap.Error(err))
}
2024-03-31 03:45:17 +08:00
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)
// }
//}()
2024-03-21 21:08:45 +08:00
// 开启定时任务
WxTask.StartAsync()
2024-03-30 22:58:42 +08:00
global.GVA_LOG.Info("定时任务已开启", zap.Any("定时任务状态:", WxTask.IsRunning()))
2024-03-29 02:32:26 +08:00
global.GVA_LOG.Info("定时任务已开启", zap.Any("任务数量:", WxTask.Len()))
2024-03-21 21:08:45 +08:00
}