Skip to content

Commit

Permalink
notifier update - test fix
Browse files Browse the repository at this point in the history
  • Loading branch information
hunterlong committed Mar 4, 2019
1 parent bc16361 commit c42ae42
Show file tree
Hide file tree
Showing 12 changed files with 38 additions and 37 deletions.
2 changes: 1 addition & 1 deletion cmd/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
8 changes: 4 additions & 4 deletions core/notifier/notifiers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
}

Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
}
Expand Down
2 changes: 1 addition & 1 deletion core/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
11 changes: 6 additions & 5 deletions notifiers/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package notifiers

import (
"fmt"
"github.com/hunterlong/statping/core/notifier"
"github.com/hunterlong/statping/types"
"github.com/hunterlong/statping/utils"
Expand Down Expand Up @@ -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
}

Expand Down
8 changes: 4 additions & 4 deletions notifiers/discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,24 +70,24 @@ 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
}

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

Expand Down
6 changes: 3 additions & 3 deletions notifiers/email.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,22 +188,22 @@ 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),
Template: mainEmailTemplate,
Data: interface{}(s),
From: u.Var1,
}
u.AddQueue(s.Id, email)
u.AddQueue(fmt.Sprintf("service_%v", s.Id), email)
}
u.Online = true
}
Expand Down
6 changes: 3 additions & 3 deletions notifiers/line_notify.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
8 changes: 4 additions & 4 deletions notifiers/mobile.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,22 +99,22 @@ 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
}

// OnSuccess will trigger successful service
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
}
Expand All @@ -126,7 +126,7 @@ func (u *mobilePush) OnSave() error {
Title: "Notification Saved",
Topic: mobileIdentifier,
}
u.AddQueue(0, msg)
u.AddQueue("saved", msg)
return nil
}

Expand Down
6 changes: 3 additions & 3 deletions notifiers/slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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,
Expand All @@ -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
}
6 changes: 3 additions & 3 deletions notifiers/telegram.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
6 changes: 3 additions & 3 deletions notifiers/twilio.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
6 changes: 3 additions & 3 deletions notifiers/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit c42ae42

Please sign in to comment.