fix: 修复修改通知方式所在组后 不会移除其原有map映射关系的问题
This commit is contained in:
		
							parent
							
								
									2ccb8f3477
								
							
						
					
					
						commit
						26646bff1a
					
				@ -71,10 +71,11 @@ type Config struct {
 | 
				
			|||||||
	ProxyGRPCPort uint
 | 
						ProxyGRPCPort uint
 | 
				
			||||||
	TLS           bool
 | 
						TLS           bool
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						EnablePlainIPInNotification bool // 通知信息IP不打码
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// IP变更提醒
 | 
						// IP变更提醒
 | 
				
			||||||
	EnableIPChangeNotification bool
 | 
						EnableIPChangeNotification bool
 | 
				
			||||||
	IPChangeNotificationTag    string
 | 
						IPChangeNotificationTag    string
 | 
				
			||||||
	EnablePlainIPInNotification bool
 | 
					 | 
				
			||||||
	Cover                      uint8  // 覆盖范围(0:提醒未被 IgnoredIPNotification 包含的所有服务器; 1:仅提醒被 IgnoredIPNotification 包含的服务器;)
 | 
						Cover                      uint8  // 覆盖范围(0:提醒未被 IgnoredIPNotification 包含的所有服务器; 1:仅提醒被 IgnoredIPNotification 包含的服务器;)
 | 
				
			||||||
	IgnoredIPNotification      string // 特定服务器IP(多个服务器用逗号分隔)
 | 
						IgnoredIPNotification      string // 特定服务器IP(多个服务器用逗号分隔)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -30,10 +30,11 @@ func LoadCronTasks() {
 | 
				
			|||||||
	var err error
 | 
						var err error
 | 
				
			||||||
	var notificationTagList []string
 | 
						var notificationTagList []string
 | 
				
			||||||
	notificationMsgMap := make(map[string]*bytes.Buffer)
 | 
						notificationMsgMap := make(map[string]*bytes.Buffer)
 | 
				
			||||||
	for i := range crons {
 | 
						for i := 0; i < len(crons); i++ {
 | 
				
			||||||
		// 旧版本计划任务可能不存在通知组 为其添加默认通知组
 | 
							// 旧版本计划任务可能不存在通知组 为其添加默认通知组
 | 
				
			||||||
		if crons[i].NotificationTag == "" {
 | 
							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]))
 | 
							crons[i].CronJobID, err = Cron.AddFunc(crons[i].Scheduler, CronTrigger(crons[i]))
 | 
				
			||||||
@ -57,17 +58,6 @@ func LoadCronTasks() {
 | 
				
			|||||||
	Cron.Start()
 | 
						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) {
 | 
					func ManualTrigger(c model.Cron) {
 | 
				
			||||||
	CronTrigger(c)()
 | 
						CronTrigger(c)()
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -35,7 +35,7 @@ func LoadNotifications() {
 | 
				
			|||||||
	if err := DB.Find(¬ifications).Error; err != nil {
 | 
						if err := DB.Find(¬ifications).Error; err != nil {
 | 
				
			||||||
		panic(err)
 | 
							panic(err)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	for i := range notifications {
 | 
						for i := 0; i < len(notifications); i++ {
 | 
				
			||||||
		// 旧版本的Tag可能不存在 自动设置为默认值
 | 
							// 旧版本的Tag可能不存在 自动设置为默认值
 | 
				
			||||||
		if notifications[i].Tag == "" {
 | 
							if notifications[i].Tag == "" {
 | 
				
			||||||
			SetDefaultNotificationTagInDB(¬ifications[i])
 | 
								SetDefaultNotificationTagInDB(¬ifications[i])
 | 
				
			||||||
@ -58,7 +58,7 @@ func OnRefreshOrAddNotification(n *model.Notification) {
 | 
				
			|||||||
	defer notificationsLock.Unlock()
 | 
						defer notificationsLock.Unlock()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	var isEdit bool
 | 
						var isEdit bool
 | 
				
			||||||
	if _, ok := NotificationList[n.Tag][n.ID]; ok {
 | 
						if _, ok := NotificationIDToTag[n.ID]; ok {
 | 
				
			||||||
		isEdit = true
 | 
							isEdit = true
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	if !isEdit {
 | 
						if !isEdit {
 | 
				
			||||||
@ -80,8 +80,17 @@ func AddNotificationToList(n *model.Notification) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
// UpdateNotificationInList 在 map 中更新通知方式
 | 
					// UpdateNotificationInList 在 map 中更新通知方式
 | 
				
			||||||
func UpdateNotificationInList(n *model.Notification) {
 | 
					func UpdateNotificationInList(n *model.Notification) {
 | 
				
			||||||
 | 
						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
 | 
							NotificationList[n.Tag][n.ID] = n
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// OnDeleteNotification 在map中删除通知方式
 | 
					// OnDeleteNotification 在map中删除通知方式
 | 
				
			||||||
func OnDeleteNotification(id uint64) {
 | 
					func OnDeleteNotification(id uint64) {
 | 
				
			||||||
 | 
				
			|||||||
@ -149,7 +149,7 @@ func (ss *ServiceSentinel) loadMonitorHistory() {
 | 
				
			|||||||
	var err error
 | 
						var err error
 | 
				
			||||||
	ss.monitorsLock.Lock()
 | 
						ss.monitorsLock.Lock()
 | 
				
			||||||
	defer ss.monitorsLock.Unlock()
 | 
						defer ss.monitorsLock.Unlock()
 | 
				
			||||||
	for i := range monitors {
 | 
						for i := 0; i < len(monitors); i++ {
 | 
				
			||||||
		// 旧版本可能不存在通知组 为其设置默认组
 | 
							// 旧版本可能不存在通知组 为其设置默认组
 | 
				
			||||||
		if monitors[i].NotificationTag == "" {
 | 
							if monitors[i].NotificationTag == "" {
 | 
				
			||||||
			monitors[i].NotificationTag = "default"
 | 
								monitors[i].NotificationTag = "default"
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user