optimize: 在查找到Server对象后移除ServerID标识符

This commit is contained in:
Akkia 2022-04-17 18:08:30 +08:00
parent f6a5362a97
commit b5b521e8cf
No known key found for this signature in database
GPG Key ID: DABE9A4AB2DD7EF3
2 changed files with 10 additions and 5 deletions

View File

@ -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)
}
}
}

View File

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