mirror of https://github.com/statping/statping
notifier update - test fix
parent
bc163618d7
commit
c42ae4292d
|
@ -39,7 +39,7 @@ func TestStartServerCommand(t *testing.T) {
|
|||
os.Setenv("DB_CONN", "sqlite")
|
||||
cmd := helperCommand(nil, "")
|
||||
var got = make(chan string)
|
||||
commandAndSleep(cmd, time.Duration(8*time.Second), got)
|
||||
commandAndSleep(cmd, time.Duration(30*time.Second), got)
|
||||
os.Unsetenv("DB_CONN")
|
||||
gg, _ := <-got
|
||||
assert.Contains(t, gg, "DB_CONN environment variable was found")
|
||||
|
|
|
@ -68,7 +68,7 @@ type Notification struct {
|
|||
|
||||
// QueueData is the struct for the messaging queue with service
|
||||
type QueueData struct {
|
||||
Id int64
|
||||
Id string
|
||||
Data interface{}
|
||||
}
|
||||
|
||||
|
@ -100,7 +100,7 @@ func (n *Notification) AfterFind() (err error) {
|
|||
}
|
||||
|
||||
// AddQueue will add any type of interface (json, string, struct, etc) into the Notifiers queue
|
||||
func (n *Notification) AddQueue(uid int64, msg interface{}) {
|
||||
func (n *Notification) AddQueue(uid string, msg interface{}) {
|
||||
data := &QueueData{uid, msg}
|
||||
n.Queue = append(n.Queue, data)
|
||||
}
|
||||
|
@ -431,10 +431,10 @@ func (n *Notification) ResetQueue() {
|
|||
}
|
||||
|
||||
// ResetQueue will clear the notifiers Queue for a service
|
||||
func (n *Notification) ResetUniqueQueue(id int64) []*QueueData {
|
||||
func (n *Notification) ResetUniqueQueue(uid string) []*QueueData {
|
||||
var queue []*QueueData
|
||||
for _, v := range n.Queue {
|
||||
if v.Id != id {
|
||||
if v.Id != uid {
|
||||
queue = append(queue, v)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -378,7 +378,7 @@ func (s *Service) Update(restart bool) error {
|
|||
if !s.AllowNotifications.Bool {
|
||||
for _, n := range CoreApp.Notifications {
|
||||
notif := n.(notifier.Notifier).Select()
|
||||
notif.ResetUniqueQueue(s.Id)
|
||||
notif.ResetUniqueQueue(fmt.Sprintf("service_%v", s.Id))
|
||||
}
|
||||
}
|
||||
if restart {
|
||||
|
|
|
@ -16,6 +16,7 @@
|
|||
package notifiers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/hunterlong/statping/core/notifier"
|
||||
"github.com/hunterlong/statping/types"
|
||||
"github.com/hunterlong/statping/utils"
|
||||
|
@ -75,23 +76,23 @@ func (u *commandLine) Select() *notifier.Notification {
|
|||
|
||||
// OnFailure for commandLine will trigger failing service
|
||||
func (u *commandLine) OnFailure(s *types.Service, f *types.Failure) {
|
||||
u.AddQueue(s.Id, u.Var2)
|
||||
u.AddQueue(fmt.Sprintf("service_%v", s.Id), u.Var2)
|
||||
u.Online = false
|
||||
}
|
||||
|
||||
// OnSuccess for commandLine will trigger successful service
|
||||
func (u *commandLine) OnSuccess(s *types.Service) {
|
||||
if !u.Online {
|
||||
u.ResetUniqueQueue(s.Id)
|
||||
u.AddQueue(s.Id, u.Var1)
|
||||
u.ResetUniqueQueue(fmt.Sprintf("service_%v", s.Id))
|
||||
u.AddQueue(fmt.Sprintf("service_%v", s.Id), u.Var1)
|
||||
}
|
||||
u.Online = true
|
||||
}
|
||||
|
||||
// OnSave for commandLine triggers when this notifier has been saved
|
||||
func (u *commandLine) OnSave() error {
|
||||
u.AddQueue(0, u.Var1)
|
||||
u.AddQueue(0, u.Var2)
|
||||
u.AddQueue("saved", u.Var1)
|
||||
u.AddQueue("saved", u.Var2)
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
|
@ -70,16 +70,16 @@ func (u *discord) Select() *notifier.Notification {
|
|||
// OnFailure will trigger failing service
|
||||
func (u *discord) OnFailure(s *types.Service, f *types.Failure) {
|
||||
msg := fmt.Sprintf(`{"content": "Your service '%v' is currently failing! Reason: %v"}`, s.Name, f.Issue)
|
||||
u.AddQueue(s.Id, msg)
|
||||
u.AddQueue(fmt.Sprintf("service_%v", s.Id), msg)
|
||||
u.Online = false
|
||||
}
|
||||
|
||||
// OnSuccess will trigger successful service
|
||||
func (u *discord) OnSuccess(s *types.Service) {
|
||||
if !u.Online {
|
||||
u.ResetUniqueQueue(s.Id)
|
||||
u.ResetUniqueQueue(fmt.Sprintf("service_%v", s.Id))
|
||||
msg := fmt.Sprintf(`{"content": "Your service '%v' is back online!"}`, s.Name)
|
||||
u.AddQueue(s.Id, msg)
|
||||
u.AddQueue(fmt.Sprintf("service_%v", s.Id), msg)
|
||||
}
|
||||
u.Online = true
|
||||
}
|
||||
|
@ -87,7 +87,7 @@ func (u *discord) OnSuccess(s *types.Service) {
|
|||
// OnSave triggers when this notifier has been saved
|
||||
func (u *discord) OnSave() error {
|
||||
msg := fmt.Sprintf(`{"content": "The discord notifier on Statping was just updated."}`)
|
||||
u.AddQueue(0, msg)
|
||||
u.AddQueue("saved", msg)
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
|
@ -188,14 +188,14 @@ func (u *email) OnFailure(s *types.Service, f *types.Failure) {
|
|||
Data: interface{}(s),
|
||||
From: u.Var1,
|
||||
}
|
||||
u.AddQueue(s.Id, email)
|
||||
u.AddQueue(fmt.Sprintf("service_%v", s.Id), email)
|
||||
u.Online = false
|
||||
}
|
||||
|
||||
// OnSuccess will trigger successful service
|
||||
func (u *email) OnSuccess(s *types.Service) {
|
||||
if !u.Online {
|
||||
u.ResetUniqueQueue(s.Id)
|
||||
u.ResetUniqueQueue(fmt.Sprintf("service_%v", s.Id))
|
||||
email := &emailOutgoing{
|
||||
To: u.Var2,
|
||||
Subject: fmt.Sprintf("Service %v is Back Online", s.Name),
|
||||
|
@ -203,7 +203,7 @@ func (u *email) OnSuccess(s *types.Service) {
|
|||
Data: interface{}(s),
|
||||
From: u.Var1,
|
||||
}
|
||||
u.AddQueue(s.Id, email)
|
||||
u.AddQueue(fmt.Sprintf("service_%v", s.Id), email)
|
||||
}
|
||||
u.Online = true
|
||||
}
|
||||
|
|
|
@ -73,16 +73,16 @@ func (u *lineNotifier) Select() *notifier.Notification {
|
|||
// OnFailure will trigger failing service
|
||||
func (u *lineNotifier) OnFailure(s *types.Service, f *types.Failure) {
|
||||
msg := fmt.Sprintf("Your service '%v' is currently offline!", s.Name)
|
||||
u.AddQueue(s.Id, msg)
|
||||
u.AddQueue(fmt.Sprintf("service_%v", s.Id), msg)
|
||||
u.Online = false
|
||||
}
|
||||
|
||||
// OnSuccess will trigger successful service
|
||||
func (u *lineNotifier) OnSuccess(s *types.Service) {
|
||||
if !u.Online {
|
||||
u.ResetUniqueQueue(s.Id)
|
||||
u.ResetUniqueQueue(fmt.Sprintf("service_%v", s.Id))
|
||||
msg := fmt.Sprintf("Your service '%v' is back online!", s.Name)
|
||||
u.AddQueue(s.Id, msg)
|
||||
u.AddQueue(fmt.Sprintf("service_%v", s.Id), msg)
|
||||
}
|
||||
u.Online = true
|
||||
}
|
||||
|
|
|
@ -99,7 +99,7 @@ func (u *mobilePush) OnFailure(s *types.Service, f *types.Failure) {
|
|||
Topic: mobileIdentifier,
|
||||
Data: data,
|
||||
}
|
||||
u.AddQueue(s.Id, msg)
|
||||
u.AddQueue(fmt.Sprintf("service_%v", s.Id), msg)
|
||||
u.Online = false
|
||||
}
|
||||
|
||||
|
@ -107,14 +107,14 @@ func (u *mobilePush) OnFailure(s *types.Service, f *types.Failure) {
|
|||
func (u *mobilePush) OnSuccess(s *types.Service) {
|
||||
data := dataJson(s, nil)
|
||||
if !u.Online {
|
||||
u.ResetUniqueQueue(s.Id)
|
||||
u.ResetUniqueQueue(fmt.Sprintf("service_%v", s.Id))
|
||||
msg := &pushArray{
|
||||
Message: fmt.Sprintf("Your service '%v' is back online!", s.Name),
|
||||
Title: "Service Online",
|
||||
Topic: mobileIdentifier,
|
||||
Data: data,
|
||||
}
|
||||
u.AddQueue(s.Id, msg)
|
||||
u.AddQueue(fmt.Sprintf("service_%v", s.Id), msg)
|
||||
}
|
||||
u.Online = true
|
||||
}
|
||||
|
@ -126,7 +126,7 @@ func (u *mobilePush) OnSave() error {
|
|||
Title: "Notification Saved",
|
||||
Topic: mobileIdentifier,
|
||||
}
|
||||
u.AddQueue(0, msg)
|
||||
u.AddQueue("saved", msg)
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
|
@ -71,7 +71,7 @@ func parseSlackMessage(id int64, temp string, data interface{}) error {
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
slacker.AddQueue(id, buf.String())
|
||||
slacker.AddQueue(fmt.Sprintf("service_%v", id), buf.String())
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -114,7 +114,7 @@ func (u *slack) OnFailure(s *types.Service, f *types.Failure) {
|
|||
// OnSuccess will trigger successful service
|
||||
func (u *slack) OnSuccess(s *types.Service) {
|
||||
if !u.Online {
|
||||
u.ResetUniqueQueue(s.Id)
|
||||
u.ResetUniqueQueue(fmt.Sprintf("service_%v", s.Id))
|
||||
message := slackMessage{
|
||||
Service: s,
|
||||
Template: successTemplate,
|
||||
|
@ -128,6 +128,6 @@ func (u *slack) OnSuccess(s *types.Service) {
|
|||
// OnSave triggers when this notifier has been saved
|
||||
func (u *slack) OnSave() error {
|
||||
message := fmt.Sprintf("Notification %v is receiving updated information.", u.Method)
|
||||
u.AddQueue(0, message)
|
||||
u.AddQueue("saved", message)
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -92,16 +92,16 @@ func (u *telegram) Send(msg interface{}) error {
|
|||
// OnFailure will trigger failing service
|
||||
func (u *telegram) OnFailure(s *types.Service, f *types.Failure) {
|
||||
msg := fmt.Sprintf("Your service '%v' is currently offline!", s.Name)
|
||||
u.AddQueue(s.Id, msg)
|
||||
u.AddQueue(fmt.Sprintf("service_%v", s.Id), msg)
|
||||
u.Online = false
|
||||
}
|
||||
|
||||
// OnSuccess will trigger successful service
|
||||
func (u *telegram) OnSuccess(s *types.Service) {
|
||||
if !u.Online {
|
||||
u.ResetUniqueQueue(s.Id)
|
||||
u.ResetUniqueQueue(fmt.Sprintf("service_%v", s.Id))
|
||||
msg := fmt.Sprintf("Your service '%v' is back online!", s.Name)
|
||||
u.AddQueue(s.Id, msg)
|
||||
u.AddQueue(fmt.Sprintf("service_%v", s.Id), msg)
|
||||
}
|
||||
u.Online = true
|
||||
}
|
||||
|
|
|
@ -102,16 +102,16 @@ func (u *twilio) Send(msg interface{}) error {
|
|||
// OnFailure will trigger failing service
|
||||
func (u *twilio) OnFailure(s *types.Service, f *types.Failure) {
|
||||
msg := fmt.Sprintf("Your service '%v' is currently offline!", s.Name)
|
||||
u.AddQueue(s.Id, msg)
|
||||
u.AddQueue(fmt.Sprintf("service_%v", s.Id), msg)
|
||||
u.Online = false
|
||||
}
|
||||
|
||||
// OnSuccess will trigger successful service
|
||||
func (u *twilio) OnSuccess(s *types.Service) {
|
||||
if !u.Online {
|
||||
u.ResetUniqueQueue(s.Id)
|
||||
u.ResetUniqueQueue(fmt.Sprintf("service_%v", s.Id))
|
||||
msg := fmt.Sprintf("Your service '%v' is back online!", s.Name)
|
||||
u.AddQueue(s.Id, msg)
|
||||
u.AddQueue(fmt.Sprintf("service_%v", s.Id), msg)
|
||||
}
|
||||
u.Online = true
|
||||
}
|
||||
|
|
|
@ -171,16 +171,16 @@ func (w *webhooker) OnTest() error {
|
|||
// OnFailure will trigger failing service
|
||||
func (w *webhooker) OnFailure(s *types.Service, f *types.Failure) {
|
||||
msg := replaceBodyText(w.Var2, s, f)
|
||||
w.AddQueue(s.Id, msg)
|
||||
w.AddQueue(fmt.Sprintf("service_%v", s.Id), msg)
|
||||
w.Online = false
|
||||
}
|
||||
|
||||
// OnSuccess will trigger successful service
|
||||
func (w *webhooker) OnSuccess(s *types.Service) {
|
||||
if !w.Online {
|
||||
w.ResetUniqueQueue(s.Id)
|
||||
w.ResetUniqueQueue(fmt.Sprintf("service_%v", s.Id))
|
||||
msg := replaceBodyText(w.Var2, s, nil)
|
||||
w.AddQueue(s.Id, msg)
|
||||
w.AddQueue(fmt.Sprintf("service_%v", s.Id), msg)
|
||||
}
|
||||
w.Online = true
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue