From f09d0d2994c78d54b27432f6efcec03c4e08be1a Mon Sep 17 00:00:00 2001 From: coward Date: Thu, 5 Dec 2024 11:49:29 +0800 Subject: [PATCH] =?UTF-8?q?:art:=E8=B0=83=E6=95=B4=E5=AE=9A=E6=97=B6?= =?UTF-8?q?=E4=BB=BB=E5=8A=A1=E6=89=A7=E8=A1=8C=E6=97=B6=E9=97=B4=EF=BC=8C?= =?UTF-8?q?=E4=BB=A5=E5=8F=8A=E5=90=AF=E5=8A=A8=E6=97=B6=E9=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cron/cron.go | 3 +-- cron/task/client.go | 35 ++++++++++++++++++----------------- main.go | 5 +++-- 3 files changed, 22 insertions(+), 21 deletions(-) diff --git a/cron/cron.go b/cron/cron.go index b477e63..87dc593 100644 --- a/cron/cron.go +++ b/cron/cron.go @@ -14,7 +14,6 @@ func Task() { 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() } diff --git a/cron/task/client.go b/cron/task/client.go index 4864641..c816e4c 100644 --- a/cron/task/client.go +++ b/cron/task/client.go @@ -15,7 +15,7 @@ import ( ) type NetworkClientImpl interface { - ClientOfflineNotify() error // 客户端离线通知 + ClientOfflineNotify() // 客户端离线通知 } type networkClient struct{} @@ -28,35 +28,36 @@ func NetworkClient() NetworkClientImpl { // @description: 客户端离线通知 // @receiver c // @return error -func (c networkClient) ClientOfflineNotify() error { - // 查询出所有配置了离线通知的客户端 - 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 - } - +func (c networkClient) ClientOfflineNotify() { + log.Debugf("开始执行离线通知任务") // 开始扫描已经链接过的客户端 connectedPeers, err := component.Wireguard().GetClients() if err != nil { log.Errorf("获取已连接客户端失败: %v", err.Error()) - return err + return } // 查询一下通知配置 code, err := service.Setting().GetByCode("WECHAT_NOTIFY") if err != nil { 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 { var clientName string 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 { clientName = cli.Name } @@ -78,7 +79,7 @@ func (c networkClient) ClientOfflineNotify() error { // 已经离线,发送通知 msg := fmt.Sprintf(` - [离线通知] + [离线通知] 客户端名称 : %v 客户端IP : %v 最后在线时间 : %v @@ -92,5 +93,5 @@ func (c networkClient) ClientOfflineNotify() error { } - return nil + return } diff --git a/main.go b/main.go index c40d4d3..d69968c 100644 --- a/main.go +++ b/main.go @@ -22,8 +22,6 @@ func init() { log.Errorf("执行脚本失败: %v", err.Error()) } - // 启动定时任务 - go cron.Task() } func main() { @@ -35,6 +33,9 @@ func main() { pprof.Register(handler, "/monitoring") } + // 启动定时任务 + cron.Task() + httpServe := http.Server{ Addr: fmt.Sprintf(":%d", config.Config.Http.Port), Handler: handler,