From 26646bff1ad40d9f219214736da30f78d1b68641 Mon Sep 17 00:00:00 2001 From: Akkia Date: Fri, 15 Apr 2022 23:23:51 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E9=80=9A=E7=9F=A5=E6=96=B9=E5=BC=8F=E6=89=80=E5=9C=A8=E7=BB=84?= =?UTF-8?q?=E5=90=8E=20=E4=B8=8D=E4=BC=9A=E7=A7=BB=E9=99=A4=E5=85=B6?= =?UTF-8?q?=E5=8E=9F=E6=9C=89map=E6=98=A0=E5=B0=84=E5=85=B3=E7=B3=BB?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- model/config.go | 11 ++++++----- service/singleton/crontask.go | 16 +++------------- service/singleton/notification.go | 15 ++++++++++++--- service/singleton/servicesentinel.go | 2 +- 4 files changed, 22 insertions(+), 22 deletions(-) diff --git a/model/config.go b/model/config.go index be32ef0..e509104 100644 --- a/model/config.go +++ b/model/config.go @@ -71,12 +71,13 @@ type Config struct { ProxyGRPCPort uint TLS bool + EnablePlainIPInNotification bool // 通知信息IP不打码 + // IP变更提醒 - EnableIPChangeNotification bool - IPChangeNotificationTag string - EnablePlainIPInNotification bool - Cover uint8 // 覆盖范围(0:提醒未被 IgnoredIPNotification 包含的所有服务器; 1:仅提醒被 IgnoredIPNotification 包含的服务器;) - IgnoredIPNotification string // 特定服务器IP(多个服务器用逗号分隔) + EnableIPChangeNotification bool + IPChangeNotificationTag string + Cover uint8 // 覆盖范围(0:提醒未被 IgnoredIPNotification 包含的所有服务器; 1:仅提醒被 IgnoredIPNotification 包含的服务器;) + IgnoredIPNotification string // 特定服务器IP(多个服务器用逗号分隔) v *viper.Viper IgnoredIPNotificationServerIDs map[uint64]bool // [ServerID] -> bool(值为true代表当前ServerID在特定服务器列表内) diff --git a/service/singleton/crontask.go b/service/singleton/crontask.go index 956456b..54aa65e 100644 --- a/service/singleton/crontask.go +++ b/service/singleton/crontask.go @@ -30,10 +30,11 @@ func LoadCronTasks() { var err error var notificationTagList []string notificationMsgMap := make(map[string]*bytes.Buffer) - for i := range crons { + for i := 0; i < len(crons); i++ { // 旧版本计划任务可能不存在通知组 为其添加默认通知组 if crons[i].NotificationTag == "" { - AddDefaultCronNotificationTag(&crons[i]) + crons[i].NotificationTag = "default" + DB.Save(crons[i]) } // 注册计划任务 crons[i].CronJobID, err = Cron.AddFunc(crons[i].Scheduler, CronTrigger(crons[i])) @@ -57,17 +58,6 @@ func LoadCronTasks() { Cron.Start() } -// AddDefaultCronNotificationTag 添加默认的计划任务通知组 -func AddDefaultCronNotificationTag(c *model.Cron) { - CronLock.Lock() - defer CronLock.Unlock() - - if c.NotificationTag == "" { - c.NotificationTag = "default" - } - DB.Save(c) -} - func ManualTrigger(c model.Cron) { CronTrigger(c)() } diff --git a/service/singleton/notification.go b/service/singleton/notification.go index 73efde3..48c0099 100644 --- a/service/singleton/notification.go +++ b/service/singleton/notification.go @@ -35,7 +35,7 @@ func LoadNotifications() { if err := DB.Find(¬ifications).Error; err != nil { panic(err) } - for i := range notifications { + for i := 0; i < len(notifications); i++ { // 旧版本的Tag可能不存在 自动设置为默认值 if notifications[i].Tag == "" { SetDefaultNotificationTagInDB(¬ifications[i]) @@ -58,7 +58,7 @@ func OnRefreshOrAddNotification(n *model.Notification) { defer notificationsLock.Unlock() var isEdit bool - if _, ok := NotificationList[n.Tag][n.ID]; ok { + if _, ok := NotificationIDToTag[n.ID]; ok { isEdit = true } if !isEdit { @@ -80,7 +80,16 @@ func AddNotificationToList(n *model.Notification) { // UpdateNotificationInList 在 map 中更新通知方式 func UpdateNotificationInList(n *model.Notification) { - NotificationList[n.Tag][n.ID] = n + if n.Tag != NotificationIDToTag[n.ID] { + // 如果 Tag 不一致,则需要先移除原有的映射关系 + delete(NotificationList[NotificationIDToTag[n.ID]], n.ID) + delete(NotificationIDToTag, n.ID) + // 将新的 Tag 中的通知方式添加到 map 中 + AddNotificationToList(n) + } else { + // 如果 Tag 一致,则直接更新 + NotificationList[n.Tag][n.ID] = n + } } // OnDeleteNotification 在map中删除通知方式 diff --git a/service/singleton/servicesentinel.go b/service/singleton/servicesentinel.go index 714091a..5049924 100644 --- a/service/singleton/servicesentinel.go +++ b/service/singleton/servicesentinel.go @@ -149,7 +149,7 @@ func (ss *ServiceSentinel) loadMonitorHistory() { var err error ss.monitorsLock.Lock() defer ss.monitorsLock.Unlock() - for i := range monitors { + for i := 0; i < len(monitors); i++ { // 旧版本可能不存在通知组 为其设置默认组 if monitors[i].NotificationTag == "" { monitors[i].NotificationTag = "default"