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] }