mirror of https://github.com/statping/statping
88 lines
1.8 KiB
Go
88 lines
1.8 KiB
Go
package services
|
|
|
|
import (
|
|
"github.com/statping/statping/types/failures"
|
|
"github.com/statping/statping/types/notifications"
|
|
"github.com/statping/statping/utils"
|
|
)
|
|
|
|
func sendSuccess(s *Service) {
|
|
if !s.AllowNotifications.Bool {
|
|
return
|
|
}
|
|
|
|
if s.prevOnline == s.Online {
|
|
return
|
|
}
|
|
|
|
for _, n := range allNotifiers {
|
|
notif := n.Select()
|
|
if notif.CanSend() {
|
|
log.Infof("Sending notification to: %s!", notif.Method)
|
|
out, err := n.OnSuccess(*s)
|
|
if err != nil {
|
|
notif.Logger().Errorln(err)
|
|
logMessage(notif.Method, "", err, false, s.Id)
|
|
return
|
|
}
|
|
logMessage(notif.Method, out, nil, true, s.Id)
|
|
notif.LastSentCount++
|
|
notif.LastSent = utils.Now()
|
|
}
|
|
}
|
|
|
|
s.prevOnline = true
|
|
s.notifyAfterCount++
|
|
}
|
|
|
|
func sendFailure(s *Service, f *failures.Failure) {
|
|
if !s.AllowNotifications.Bool {
|
|
return
|
|
}
|
|
|
|
if s.prevOnline == s.Online && !s.UpdateNotify.Bool {
|
|
return
|
|
}
|
|
|
|
if s.NotifyAfter != 0 {
|
|
if s.NotifyAfter > s.notifyAfterCount {
|
|
s.notifyAfterCount++
|
|
return
|
|
}
|
|
}
|
|
|
|
for _, n := range allNotifiers {
|
|
notif := n.Select()
|
|
if notif.CanSend() {
|
|
log.Infof("Sending Failure notification to: %s!", notif.Method)
|
|
out, err := n.OnFailure(*s, *f)
|
|
if err != nil {
|
|
notif.Logger().WithField("failure", f.Issue).Errorln(err)
|
|
logMessage(notif.Method, "", err, false, s.Id)
|
|
}
|
|
logMessage(notif.Method, out, nil, false, s.Id)
|
|
|
|
notif.LastSentCount++
|
|
notif.LastSent = utils.Now()
|
|
}
|
|
}
|
|
|
|
s.prevOnline = false
|
|
s.notifyAfterCount++
|
|
}
|
|
|
|
func logMessage(method string, msg string, error error, onSuccesss bool, serviceId int64) {
|
|
notif := FindNotifier(method)
|
|
l := ¬ifications.NotificationLog{
|
|
Message: msg,
|
|
Error: error,
|
|
Success: onSuccesss,
|
|
Service: serviceId,
|
|
CreatedAt: utils.Now(),
|
|
}
|
|
notif.Logs = append(notif.Logs, l)
|
|
if len(notif.Logs) > 32 {
|
|
notif.Logs = notif.Logs[1:]
|
|
}
|
|
}
|