mirror of https://github.com/statping/statping
notifier clear queue when back online
parent
70064ecbb9
commit
16b076aadd
|
@ -245,7 +245,7 @@ func Init(n Notifier) (*Notification, error) {
|
||||||
if err == nil {
|
if err == nil {
|
||||||
notify, _ = SelectNotification(n)
|
notify, _ = SelectNotification(n)
|
||||||
if notify.Delay.Seconds() == 0 {
|
if notify.Delay.Seconds() == 0 {
|
||||||
notify.Delay = time.Duration(60 * time.Second)
|
notify.Delay = time.Duration(1 * time.Second)
|
||||||
}
|
}
|
||||||
notify.testable = isType(n, new(Tester))
|
notify.testable = isType(n, new(Tester))
|
||||||
notify.Form = n.Select().Form
|
notify.Form = n.Select().Form
|
||||||
|
@ -400,9 +400,6 @@ func (n *Notification) WithinLimits() (bool, error) {
|
||||||
if n.SentLastMinute() >= n.Limits {
|
if n.SentLastMinute() >= n.Limits {
|
||||||
return false, fmt.Errorf("notifier sent %v out of %v in last minute", n.SentLastMinute(), n.Limits)
|
return false, fmt.Errorf("notifier sent %v out of %v in last minute", n.SentLastMinute(), n.Limits)
|
||||||
}
|
}
|
||||||
if n.Delay.Seconds() == 0 {
|
|
||||||
n.Delay = time.Duration(500 * time.Millisecond)
|
|
||||||
}
|
|
||||||
if n.LastSent().Seconds() == 0 {
|
if n.LastSent().Seconds() == 0 {
|
||||||
return true, nil
|
return true, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -227,14 +227,14 @@ func Dbtimestamp(group string, column string) string {
|
||||||
// Downtime returns the amount of time of a offline service
|
// Downtime returns the amount of time of a offline service
|
||||||
func (s *Service) Downtime() time.Duration {
|
func (s *Service) Downtime() time.Duration {
|
||||||
hits, _ := s.Hits()
|
hits, _ := s.Hits()
|
||||||
fails := s.LimitedFailures()
|
fail := s.lastFailure()
|
||||||
if len(fails) == 0 {
|
if fail == nil {
|
||||||
return time.Duration(0)
|
return time.Duration(0)
|
||||||
}
|
}
|
||||||
if len(hits) == 0 {
|
if len(hits) == 0 {
|
||||||
return time.Now().UTC().Sub(fails[len(fails)-1].CreatedAt.UTC())
|
return time.Now().UTC().Sub(fail.CreatedAt.UTC())
|
||||||
}
|
}
|
||||||
since := fails[0].CreatedAt.UTC().Sub(hits[0].CreatedAt.UTC())
|
since := fail.CreatedAt.UTC().Sub(fail.CreatedAt.UTC())
|
||||||
return since
|
return since
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -202,7 +202,7 @@ func servicesUpdateHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
service.Order = order
|
service.Order = order
|
||||||
|
|
||||||
service.Update(true)
|
service.Update(true)
|
||||||
service.Check(true)
|
go service.Check(true)
|
||||||
executeResponse(w, r, "service.html", service, "/services")
|
executeResponse(w, r, "service.html", service, "/services")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -83,6 +83,7 @@ func (u *discord) OnFailure(s *types.Service, f *types.Failure) {
|
||||||
// OnSuccess will trigger successful service
|
// OnSuccess will trigger successful service
|
||||||
func (u *discord) OnSuccess(s *types.Service) {
|
func (u *discord) OnSuccess(s *types.Service) {
|
||||||
if !u.Online {
|
if !u.Online {
|
||||||
|
u.ResetQueue()
|
||||||
msg := fmt.Sprintf(`{"content": "Your service '%v' is back online!"}`, s.Name)
|
msg := fmt.Sprintf(`{"content": "Your service '%v' is back online!"}`, s.Name)
|
||||||
u.AddQueue(msg)
|
u.AddQueue(msg)
|
||||||
}
|
}
|
||||||
|
|
|
@ -66,7 +66,7 @@ func TestDiscordNotifier(t *testing.T) {
|
||||||
|
|
||||||
t.Run("discord OnFailure", func(t *testing.T) {
|
t.Run("discord OnFailure", func(t *testing.T) {
|
||||||
discorder.OnFailure(TestService, TestFailure)
|
discorder.OnFailure(TestService, TestFailure)
|
||||||
assert.Len(t, discorder.Queue, 1)
|
assert.Equal(t, 1, len(discorder.Queue))
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("discord Check Offline", func(t *testing.T) {
|
t.Run("discord Check Offline", func(t *testing.T) {
|
||||||
|
@ -75,7 +75,7 @@ func TestDiscordNotifier(t *testing.T) {
|
||||||
|
|
||||||
t.Run("discord OnSuccess", func(t *testing.T) {
|
t.Run("discord OnSuccess", func(t *testing.T) {
|
||||||
discorder.OnSuccess(TestService)
|
discorder.OnSuccess(TestService)
|
||||||
assert.Len(t, discorder.Queue, 2)
|
assert.Equal(t, 1, len(discorder.Queue))
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("discord Check Back Online", func(t *testing.T) {
|
t.Run("discord Check Back Online", func(t *testing.T) {
|
||||||
|
@ -84,7 +84,7 @@ func TestDiscordNotifier(t *testing.T) {
|
||||||
|
|
||||||
t.Run("discord OnSuccess Again", func(t *testing.T) {
|
t.Run("discord OnSuccess Again", func(t *testing.T) {
|
||||||
discorder.OnSuccess(TestService)
|
discorder.OnSuccess(TestService)
|
||||||
assert.Len(t, discorder.Queue, 2)
|
assert.Equal(t, 1, len(discorder.Queue))
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("discord Send", func(t *testing.T) {
|
t.Run("discord Send", func(t *testing.T) {
|
||||||
|
|
|
@ -196,6 +196,7 @@ func (u *email) OnFailure(s *types.Service, f *types.Failure) {
|
||||||
// OnSuccess will trigger successful service
|
// OnSuccess will trigger successful service
|
||||||
func (u *email) OnSuccess(s *types.Service) {
|
func (u *email) OnSuccess(s *types.Service) {
|
||||||
if !u.Online {
|
if !u.Online {
|
||||||
|
u.ResetQueue()
|
||||||
email := &emailOutgoing{
|
email := &emailOutgoing{
|
||||||
To: u.Var2,
|
To: u.Var2,
|
||||||
Subject: fmt.Sprintf("Service %v is Back Online", s.Name),
|
Subject: fmt.Sprintf("Service %v is Back Online", s.Name),
|
||||||
|
|
|
@ -102,7 +102,7 @@ func TestEmailNotifier(t *testing.T) {
|
||||||
|
|
||||||
t.Run("email OnFailure", func(t *testing.T) {
|
t.Run("email OnFailure", func(t *testing.T) {
|
||||||
emailer.OnFailure(TestService, TestFailure)
|
emailer.OnFailure(TestService, TestFailure)
|
||||||
assert.Len(t, emailer.Queue, 1)
|
assert.Equal(t, 1, len(emailer.Queue))
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("email Check Offline", func(t *testing.T) {
|
t.Run("email Check Offline", func(t *testing.T) {
|
||||||
|
@ -111,7 +111,7 @@ func TestEmailNotifier(t *testing.T) {
|
||||||
|
|
||||||
t.Run("email OnSuccess", func(t *testing.T) {
|
t.Run("email OnSuccess", func(t *testing.T) {
|
||||||
emailer.OnSuccess(TestService)
|
emailer.OnSuccess(TestService)
|
||||||
assert.Len(t, emailer.Queue, 2)
|
assert.Equal(t, 1, len(emailer.Queue))
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("email Check Back Online", func(t *testing.T) {
|
t.Run("email Check Back Online", func(t *testing.T) {
|
||||||
|
@ -120,7 +120,7 @@ func TestEmailNotifier(t *testing.T) {
|
||||||
|
|
||||||
t.Run("email OnSuccess Again", func(t *testing.T) {
|
t.Run("email OnSuccess Again", func(t *testing.T) {
|
||||||
emailer.OnSuccess(TestService)
|
emailer.OnSuccess(TestService)
|
||||||
assert.Len(t, emailer.Queue, 2)
|
assert.Equal(t, 1, len(emailer.Queue))
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("email Send", func(t *testing.T) {
|
t.Run("email Send", func(t *testing.T) {
|
||||||
|
|
|
@ -90,6 +90,7 @@ func (u *lineNotifier) OnFailure(s *types.Service, f *types.Failure) {
|
||||||
// OnSuccess will trigger successful service
|
// OnSuccess will trigger successful service
|
||||||
func (u *lineNotifier) OnSuccess(s *types.Service) {
|
func (u *lineNotifier) OnSuccess(s *types.Service) {
|
||||||
if !u.Online {
|
if !u.Online {
|
||||||
|
u.ResetQueue()
|
||||||
msg := fmt.Sprintf("Your service '%v' is back online!", s.Name)
|
msg := fmt.Sprintf("Your service '%v' is back online!", s.Name)
|
||||||
u.AddQueue(msg)
|
u.AddQueue(msg)
|
||||||
}
|
}
|
||||||
|
|
|
@ -36,12 +36,12 @@ var TestService = &types.Service{
|
||||||
Name: "Interpol - All The Rage Back Home",
|
Name: "Interpol - All The Rage Back Home",
|
||||||
Domain: "https://www.youtube.com/watch?v=-u6DvRyyKGU",
|
Domain: "https://www.youtube.com/watch?v=-u6DvRyyKGU",
|
||||||
ExpectedStatus: 200,
|
ExpectedStatus: 200,
|
||||||
|
Expected: "test example",
|
||||||
Interval: 30,
|
Interval: 30,
|
||||||
Type: "http",
|
Type: "http",
|
||||||
Method: "GET",
|
Method: "GET",
|
||||||
Timeout: 20,
|
Timeout: 20,
|
||||||
LastStatusCode: 404,
|
LastStatusCode: 404,
|
||||||
Expected: "test example",
|
|
||||||
LastResponse: "<html>this is an example response</html>",
|
LastResponse: "<html>this is an example response</html>",
|
||||||
CreatedAt: time.Now().Add(-24 * time.Hour),
|
CreatedAt: time.Now().Add(-24 * time.Hour),
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,7 +30,7 @@ import (
|
||||||
const (
|
const (
|
||||||
slackMethod = "slack"
|
slackMethod = "slack"
|
||||||
failingTemplate = `{ "attachments": [ { "fallback": "Service {{.Service.Name}} - is currently failing", "text": "Your Statup service <{{.Service.Domain}}|{{.Service.Name}}> has just received a Failure notification based on your expected results. {{.Service.Name}} responded with a HTTP Status code of {{.Service.LastStatusCode}}.", "fields": [ { "title": "Expected", "value": "{{.Service.Expected}}", "short": true }, { "title": "Status Code", "value": "{{.Service.LastStatusCode}}", "short": true } ], "color": "#FF0000", "thumb_url": "https://statup.io", "footer": "Statup", "footer_icon": "https://img.cjx.io/statuplogo32.png" } ] }`
|
failingTemplate = `{ "attachments": [ { "fallback": "Service {{.Service.Name}} - is currently failing", "text": "Your Statup service <{{.Service.Domain}}|{{.Service.Name}}> has just received a Failure notification based on your expected results. {{.Service.Name}} responded with a HTTP Status code of {{.Service.LastStatusCode}}.", "fields": [ { "title": "Expected", "value": "{{.Service.Expected}}", "short": true }, { "title": "Status Code", "value": "{{.Service.LastStatusCode}}", "short": true } ], "color": "#FF0000", "thumb_url": "https://statup.io", "footer": "Statup", "footer_icon": "https://img.cjx.io/statuplogo32.png" } ] }`
|
||||||
successTemplate = `{ "attachments": [ { "fallback": "Service {{.Service.Name}} - is now back online", "text": "Your Statup service <{{.Service.Domain}}|{{.Service.Name}}> is now back online and meets your expected responses.", "fields": [ { "title": "Issue", "value": "Awesome Project", "short": true }, { "title": "Status Code", "value": "{{.Service.LastStatusCode}}", "short": true } ], "color": "#00FF00", "thumb_url": "https://statup.io", "footer": "Statup", "footer_icon": "https://img.cjx.io/statuplogo32.png" } ] }`
|
successTemplate = `{ "attachments": [ { "fallback": "Service {{.Service.Name}} - is now back online", "text": "Your Statup service <{{.Service.Domain}}|{{.Service.Name}}> is now back online and meets your expected responses.", "color": "#00FF00", "thumb_url": "https://statup.io", "footer": "Statup", "footer_icon": "https://img.cjx.io/statuplogo32.png" } ] }`
|
||||||
slackText = `{"text":"{{.}}"}`
|
slackText = `{"text":"{{.}}"}`
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -127,6 +127,7 @@ func (u *slack) OnFailure(s *types.Service, f *types.Failure) {
|
||||||
// OnSuccess will trigger successful service
|
// OnSuccess will trigger successful service
|
||||||
func (u *slack) OnSuccess(s *types.Service) {
|
func (u *slack) OnSuccess(s *types.Service) {
|
||||||
if !u.Online {
|
if !u.Online {
|
||||||
|
u.ResetQueue()
|
||||||
message := slackMessage{
|
message := slackMessage{
|
||||||
Service: s,
|
Service: s,
|
||||||
Template: successTemplate,
|
Template: successTemplate,
|
||||||
|
|
|
@ -46,6 +46,7 @@ func TestSlackNotifier(t *testing.T) {
|
||||||
t.Run("Load slack", func(t *testing.T) {
|
t.Run("Load slack", func(t *testing.T) {
|
||||||
slacker.Host = SLACK_URL
|
slacker.Host = SLACK_URL
|
||||||
slacker.Delay = time.Duration(100 * time.Millisecond)
|
slacker.Delay = time.Duration(100 * time.Millisecond)
|
||||||
|
slacker.Limits = 3
|
||||||
err := notifier.AddNotifier(slacker)
|
err := notifier.AddNotifier(slacker)
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
assert.Equal(t, "Hunter Long", slacker.Author)
|
assert.Equal(t, "Hunter Long", slacker.Author)
|
||||||
|
@ -60,11 +61,11 @@ func TestSlackNotifier(t *testing.T) {
|
||||||
assert.True(t, slacker.CanTest())
|
assert.True(t, slacker.CanTest())
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("slack parse message", func(t *testing.T) {
|
//t.Run("slack parse message", func(t *testing.T) {
|
||||||
err := parseSlackMessage(slackText, "this is a test!")
|
// err := parseSlackMessage(slackText, "this is a test!")
|
||||||
assert.Nil(t, err)
|
// assert.Nil(t, err)
|
||||||
assert.Equal(t, 1, len(slacker.Queue))
|
// assert.Equal(t, 1, len(slacker.Queue))
|
||||||
})
|
//})
|
||||||
|
|
||||||
t.Run("slack Within Limits", func(t *testing.T) {
|
t.Run("slack Within Limits", func(t *testing.T) {
|
||||||
ok, err := slacker.WithinLimits()
|
ok, err := slacker.WithinLimits()
|
||||||
|
@ -74,7 +75,14 @@ func TestSlackNotifier(t *testing.T) {
|
||||||
|
|
||||||
t.Run("slack OnFailure", func(t *testing.T) {
|
t.Run("slack OnFailure", func(t *testing.T) {
|
||||||
slacker.OnFailure(TestService, TestFailure)
|
slacker.OnFailure(TestService, TestFailure)
|
||||||
assert.Len(t, slacker.Queue, 2)
|
assert.Equal(t, 1, len(slacker.Queue))
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("slack OnFailure multiple times", func(t *testing.T) {
|
||||||
|
for i := 0; i <= 50; i++ {
|
||||||
|
slacker.OnFailure(TestService, TestFailure)
|
||||||
|
}
|
||||||
|
assert.Equal(t, 52, len(slacker.Queue))
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("slack Check Offline", func(t *testing.T) {
|
t.Run("slack Check Offline", func(t *testing.T) {
|
||||||
|
@ -83,22 +91,33 @@ func TestSlackNotifier(t *testing.T) {
|
||||||
|
|
||||||
t.Run("slack OnSuccess", func(t *testing.T) {
|
t.Run("slack OnSuccess", func(t *testing.T) {
|
||||||
slacker.OnSuccess(TestService)
|
slacker.OnSuccess(TestService)
|
||||||
assert.Len(t, slacker.Queue, 3)
|
assert.Equal(t, 1, len(slacker.Queue))
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("slack Check Back Online", func(t *testing.T) {
|
t.Run("slack Queue after being online", func(t *testing.T) {
|
||||||
assert.True(t, slacker.Online)
|
assert.True(t, slacker.Online)
|
||||||
|
assert.Equal(t, 1, len(slacker.Queue))
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("slack OnSuccess Again", func(t *testing.T) {
|
t.Run("slack OnSuccess Again", func(t *testing.T) {
|
||||||
|
assert.True(t, slacker.Online)
|
||||||
slacker.OnSuccess(TestService)
|
slacker.OnSuccess(TestService)
|
||||||
assert.Len(t, slacker.Queue, 3)
|
assert.Equal(t, 1, len(slacker.Queue))
|
||||||
|
go notifier.Queue(slacker)
|
||||||
|
time.Sleep(5 * time.Second)
|
||||||
|
assert.Equal(t, 0, len(slacker.Queue))
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("slack Within Limits again", func(t *testing.T) {
|
||||||
|
ok, err := slacker.WithinLimits()
|
||||||
|
assert.Nil(t, err)
|
||||||
|
assert.True(t, ok)
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("slack Send", func(t *testing.T) {
|
t.Run("slack Send", func(t *testing.T) {
|
||||||
err := slacker.Send(slackTestMessage)
|
err := slacker.Send(slackTestMessage)
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
assert.Len(t, slacker.Queue, 3)
|
assert.Equal(t, 0, len(slacker.Queue))
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("slack Test", func(t *testing.T) {
|
t.Run("slack Test", func(t *testing.T) {
|
||||||
|
@ -108,7 +127,7 @@ func TestSlackNotifier(t *testing.T) {
|
||||||
|
|
||||||
t.Run("slack Queue", func(t *testing.T) {
|
t.Run("slack Queue", func(t *testing.T) {
|
||||||
go notifier.Queue(slacker)
|
go notifier.Queue(slacker)
|
||||||
time.Sleep(4 * time.Second)
|
time.Sleep(5 * time.Second)
|
||||||
assert.Equal(t, SLACK_URL, slacker.Host)
|
assert.Equal(t, SLACK_URL, slacker.Host)
|
||||||
assert.Equal(t, 0, len(slacker.Queue))
|
assert.Equal(t, 0, len(slacker.Queue))
|
||||||
})
|
})
|
||||||
|
|
|
@ -122,6 +122,7 @@ func (u *twilio) OnFailure(s *types.Service, f *types.Failure) {
|
||||||
// OnSuccess will trigger successful service
|
// OnSuccess will trigger successful service
|
||||||
func (u *twilio) OnSuccess(s *types.Service) {
|
func (u *twilio) OnSuccess(s *types.Service) {
|
||||||
if !u.Online {
|
if !u.Online {
|
||||||
|
u.ResetQueue()
|
||||||
msg := fmt.Sprintf("Your service '%v' is back online!", s.Name)
|
msg := fmt.Sprintf("Your service '%v' is back online!", s.Name)
|
||||||
u.AddQueue(msg)
|
u.AddQueue(msg)
|
||||||
}
|
}
|
||||||
|
|
|
@ -177,6 +177,7 @@ func (w *webhooker) OnFailure(s *types.Service, f *types.Failure) {
|
||||||
// OnSuccess will trigger successful service
|
// OnSuccess will trigger successful service
|
||||||
func (w *webhooker) OnSuccess(s *types.Service) {
|
func (w *webhooker) OnSuccess(s *types.Service) {
|
||||||
if !w.Online {
|
if !w.Online {
|
||||||
|
w.ResetQueue()
|
||||||
msg := replaceBodyText(w.Var2, s, nil)
|
msg := replaceBodyText(w.Var2, s, nil)
|
||||||
webhook.AddQueue(msg)
|
webhook.AddQueue(msg)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue