🎨调整定时任务执行时间,以及启动时间
Some checks reported errors
continuous-integration/drone/tag Build was killed

This commit is contained in:
coward 2024-12-05 11:49:29 +08:00
parent ca42c72e0f
commit f09d0d2994
3 changed files with 22 additions and 21 deletions

View File

@ -14,7 +14,6 @@ func Task() {
return return
} }
_, _ = sch.NewJob(gocron.DurationJob(3*time.Minute), gocron.NewTask(task.NetworkClient().ClientOfflineNotify())) // 每三分钟检测离线的客户端并且发送消息 _, _ = sch.NewJob(gocron.DurationJob(1*time.Minute), gocron.NewTask(task.NetworkClient().ClientOfflineNotify)) // 每分钟执行一次
sch.Start() sch.Start()
} }

View File

@ -15,7 +15,7 @@ import (
) )
type NetworkClientImpl interface { type NetworkClientImpl interface {
ClientOfflineNotify() error // 客户端离线通知 ClientOfflineNotify() // 客户端离线通知
} }
type networkClient struct{} type networkClient struct{}
@ -28,35 +28,36 @@ func NetworkClient() NetworkClientImpl {
// @description: 客户端离线通知 // @description: 客户端离线通知
// @receiver c // @receiver c
// @return error // @return error
func (c networkClient) ClientOfflineNotify() error { func (c networkClient) ClientOfflineNotify() {
// 查询出所有配置了离线通知的客户端 log.Debugf("开始执行离线通知任务")
var clients []model.Client
if err := client.DB.Where("offline_monitoring = ?", 1).Find(&clients).Error; err != nil {
return err
}
if len(clients) <= 0 {
return nil
}
// 开始扫描已经链接过的客户端 // 开始扫描已经链接过的客户端
connectedPeers, err := component.Wireguard().GetClients() connectedPeers, err := component.Wireguard().GetClients()
if err != nil { if err != nil {
log.Errorf("获取已连接客户端失败: %v", err.Error()) log.Errorf("获取已连接客户端失败: %v", err.Error())
return err return
} }
// 查询一下通知配置 // 查询一下通知配置
code, err := service.Setting().GetByCode("WECHAT_NOTIFY") code, err := service.Setting().GetByCode("WECHAT_NOTIFY")
if err != nil { if err != nil {
log.Errorf("获取微信通知配置失败: %v", err.Error()) log.Errorf("获取微信通知配置失败: %v", err.Error())
return err return
}
// 查询出所有配置了离线通知的客户端
var clients []model.Client
if err := client.DB.Where("offline_monitoring = ?", 1).Find(&clients).Error; err != nil {
return
}
if len(clients) <= 0 {
return
} }
for _, peer := range connectedPeers { for _, peer := range connectedPeers {
var clientName string var clientName string
if !slices.ContainsFunc(clients, func(cli model.Client) bool { if !slices.ContainsFunc(clients, func(cli model.Client) bool {
isExist := peer.PublicKey.String() == jsoniter.Get([]byte(cli.Keys), "publicKey").ToString() isExist := peer.PublicKey.String() == jsoniter.Get([]byte(cli.Keys), "PublicKey").ToString()
if isExist { if isExist {
clientName = cli.Name clientName = cli.Name
} }
@ -92,5 +93,5 @@ func (c networkClient) ClientOfflineNotify() error {
} }
return nil return
} }

View File

@ -22,8 +22,6 @@ func init() {
log.Errorf("执行脚本失败: %v", err.Error()) log.Errorf("执行脚本失败: %v", err.Error())
} }
// 启动定时任务
go cron.Task()
} }
func main() { func main() {
@ -35,6 +33,9 @@ func main() {
pprof.Register(handler, "/monitoring") pprof.Register(handler, "/monitoring")
} }
// 启动定时任务
cron.Task()
httpServe := http.Server{ httpServe := http.Server{
Addr: fmt.Sprintf(":%d", config.Config.Http.Port), Addr: fmt.Sprintf(":%d", config.Config.Http.Port),
Handler: handler, Handler: handler,