From b5b521e8cfb9de614be86ad31fd304117e72e05c Mon Sep 17 00:00:00 2001 From: Akkia Date: Sun, 17 Apr 2022 18:08:30 +0800 Subject: [PATCH] =?UTF-8?q?optimize:=20=E5=9C=A8=E6=9F=A5=E6=89=BE?= =?UTF-8?q?=E5=88=B0Server=E5=AF=B9=E8=B1=A1=E5=90=8E=E7=A7=BB=E9=99=A4Ser?= =?UTF-8?q?verID=E6=A0=87=E8=AF=86=E7=AC=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- service/singleton/crontask.go | 2 +- service/singleton/notification.go | 13 +++++++++---- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/service/singleton/crontask.go b/service/singleton/crontask.go index 54aa65e..83f8674 100644 --- a/service/singleton/crontask.go +++ b/service/singleton/crontask.go @@ -84,7 +84,7 @@ func CronTrigger(cr model.Cron) func() { Type: model.TaskTypeCommand, }) } else { - SendNotification(cr.NotificationTag, fmt.Sprintf("[任务失败] %s,服务器 %s 离线,无法执行。", cr.Name, s.Name), false) + SendNotification(cr.NotificationTag, fmt.Sprintf("$%d$[任务失败] %s,服务器 %s 离线,无法执行。", s.ID, cr.Name, s.Name), false) } } } diff --git a/service/singleton/notification.go b/service/singleton/notification.go index ea80c49..e734eb3 100644 --- a/service/singleton/notification.go +++ b/service/singleton/notification.go @@ -6,6 +6,7 @@ import ( "log" "regexp" "strconv" + "strings" "sync" "time" @@ -145,7 +146,7 @@ func SendNotification(notificationTag string, desc string, mutable bool) { log.Println("尝试通知", n.Name) } for _, n := range NotificationList[notificationTag] { - server := findServerInMsg(desc) + desc, server := findServerInMsg(desc) ns := model.NotificationServerBundle{ Notification: n, Server: server, @@ -158,11 +159,15 @@ func SendNotification(notificationTag string, desc string, mutable bool) { } } -// findServerInMsg 通过msg字符串中的$ServerID$ 返回Server对象 未找到会返回nil -func findServerInMsg(msg string) *model.Server { +// findServerInMsg 通过msg字符串中的$ServerID$ 返回修改后的字符串与Server对象 +func findServerInMsg(msg string) (string, *model.Server) { reg1 := regexp.MustCompile(`^\$\d+`) reg2 := regexp.MustCompile(`[^$]+`) ServerIDStr := reg2.FindString(reg1.FindString(msg)) ServerID, _ := strconv.ParseUint(ServerIDStr, 10, 64) - return ServerList[ServerID] + // 将原字符串的ServerID标识去除 + if ServerIDStr != "" { + msg = strings.Replace(msg, "$"+ServerIDStr+"$", "", 1) + } + return msg, ServerList[ServerID] }