1
0
forked from lxh/go-wxhelper
go-wxhelper/tasks/tasks.go

47 lines
1.3 KiB
Go
Raw Permalink Normal View History

2023-09-21 17:33:59 +08:00
package tasks
import (
"github.com/go-co-op/gocron"
2023-10-11 09:21:07 +08:00
"go-wechat/config"
"go-wechat/tasks/friends"
"go-wechat/tasks/watergroup"
2023-09-21 17:33:59 +08:00
"log"
"time"
)
// InitTasks
// @description: 初始化定时任务
func InitTasks() {
2023-10-11 09:21:07 +08:00
// 没启用直接返回
if !config.Conf.Task.Enable {
log.Println("未启用定时任务")
return
}
2023-09-21 17:33:59 +08:00
// 定时任务发送消息
s := gocron.NewScheduler(time.Local)
2023-10-11 09:21:07 +08:00
// 水群排行
if config.Conf.Task.WaterGroup.Enable {
log.Printf("水群排行任务已启用,执行表达式: %+v", config.Conf.Task.WaterGroup.Cron)
if config.Conf.Task.WaterGroup.Cron.Yesterday != "" {
_, _ = s.Cron(config.Conf.Task.WaterGroup.Cron.Yesterday).Do(watergroup.Yesterday)
}
if config.Conf.Task.WaterGroup.Cron.Week != "" {
_, _ = s.Cron(config.Conf.Task.WaterGroup.Cron.Week).Do(watergroup.Week)
}
if config.Conf.Task.WaterGroup.Cron.Month != "" {
_, _ = s.Cron(config.Conf.Task.WaterGroup.Cron.Month).Do(watergroup.Month)
}
2023-10-11 09:21:07 +08:00
}
2023-09-21 17:33:59 +08:00
2023-10-11 09:21:07 +08:00
// 更新好友列表
if config.Conf.Task.SyncFriends.Enable {
log.Printf("更新好友列表任务已启用,执行表达式: %s", config.Conf.Task.SyncFriends.Cron)
_, _ = s.Cron(config.Conf.Task.SyncFriends.Cron).Do(friends.Sync)
2023-10-11 09:21:07 +08:00
}
2023-09-21 17:33:59 +08:00
// 开启定时任务
s.StartAsync()
log.Println("定时任务初始化成功")
}