WIP: 通知方式分组 支持将不同的报警|监控|计划任务的通知 发送到指定的通知分组

This commit is contained in:
Akkia
2022-04-14 21:06:42 +08:00
parent 8ab62858f9
commit 9c1d495eb0
11 changed files with 143 additions and 49 deletions

View File

@@ -11,7 +11,7 @@ import (
var (
Cron *cron.Cron
Crons map[uint64]*model.Cron
Crons map[uint64]*model.Cron // [CrondID] -> *model.Cron
CronLock sync.RWMutex
)
@@ -27,9 +27,12 @@ func LoadCronTasks() {
DB.Find(&crons)
var err error
errMsg := new(bytes.Buffer)
for i := 0; i < len(crons); i++ {
cr := crons[i]
var notificationTagList []string
for _, cr := range crons {
// 旧版本计划任务可能不存在通知组 为其添加默认通知组
if cr.NotificationTag == "" {
AddDefaultCronNotificationTag(&cr)
}
// 注册计划任务
cr.CronJobID, err = Cron.AddFunc(cr.Scheduler, CronTrigger(cr))
if err == nil {
@@ -39,15 +42,30 @@ func LoadCronTasks() {
errMsg.WriteString("调度失败的计划任务:[")
}
errMsg.WriteString(fmt.Sprintf("%d,", cr.ID))
notificationTagList = append(notificationTagList, cr.NotificationTag)
}
}
if errMsg.Len() > 0 {
msg := errMsg.String()
SendNotification(msg[:len(msg)-1]+"] 这些任务将无法正常执行,请进入后点重新修改保存。", false)
msg := errMsg.String() + "] 这些任务将无法正常执行,请进入后点重新修改保存。"
for _, tag := range notificationTagList {
// 向调度错误的计划任务所包含的所有通知组发送通知
SendNotification(tag, msg, false)
}
}
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)()
}
@@ -74,7 +92,7 @@ func CronTrigger(cr model.Cron) func() {
Type: model.TaskTypeCommand,
})
} else {
SendNotification(fmt.Sprintf("[任务失败] %s服务器 %s 离线,无法执行。", cr.Name, s.Name), false)
SendNotification(cr.NotificationTag, fmt.Sprintf("[任务失败] %s服务器 %s 离线,无法执行。", cr.Name, s.Name), false)
}
}
}