statping/core/events.go

101 lines
1.9 KiB
Go
Raw Normal View History

2018-06-30 00:57:05 +00:00
package core
2018-06-14 01:19:00 +00:00
2018-06-19 04:48:25 +00:00
import (
"fmt"
2018-06-19 04:48:25 +00:00
"github.com/fatih/structs"
2018-07-02 06:21:41 +00:00
"github.com/hunterlong/statup/notifications"
2018-06-19 04:48:25 +00:00
"github.com/hunterlong/statup/plugin"
2018-07-02 06:21:41 +00:00
"github.com/hunterlong/statup/types"
2018-06-19 04:48:25 +00:00
"upper.io/db.v3/lib/sqlbuilder"
)
func OnLoad(db sqlbuilder.Database) {
2018-06-30 00:57:05 +00:00
for _, p := range AllPlugins {
2018-06-19 04:48:25 +00:00
p.OnLoad(db)
}
}
2018-06-14 01:19:00 +00:00
2018-06-14 06:38:15 +00:00
func OnSuccess(s *Service) {
2018-06-30 00:57:05 +00:00
for _, p := range AllPlugins {
2018-06-19 04:48:25 +00:00
p.OnSuccess(structs.Map(s))
2018-06-14 01:19:00 +00:00
}
}
func OnFailure(s *Service, f FailureData) {
2018-06-30 00:57:05 +00:00
for _, p := range AllPlugins {
2018-06-19 04:48:25 +00:00
p.OnFailure(structs.Map(s))
}
2018-07-02 06:21:41 +00:00
onFailureEmail(s, f)
onFailureSlack(s, f)
}
func onFailureSlack(s *Service, f FailureData) {
slack := SelectCommunication(2)
if slack.Enabled {
msg := fmt.Sprintf("Service %v is currently offline! Issue: %v", s.Name, f.Issue)
2018-07-02 06:21:41 +00:00
notifications.SendSlack(msg)
}
}
type failedEmail struct {
Service *Service
FailureData FailureData
Domain string
}
func onFailureEmail(s *Service, f FailureData) {
email := SelectCommunication(1)
if email.Enabled {
data := failedEmail{s, f, CoreApp.Domain}
admin, _ := SelectUser(1)
email := &types.Email{
To: admin.Email,
Subject: fmt.Sprintf("Service %v is Down", s.Name),
Template: "failure.html",
Data: data,
From: email.Var1,
}
notifications.SendEmail(EmailBox, email)
}
2018-06-19 04:48:25 +00:00
}
func OnSettingsSaved(c *Core) {
2018-06-30 00:57:05 +00:00
for _, p := range AllPlugins {
2018-06-19 04:48:25 +00:00
p.OnSettingsSaved(structs.Map(c))
}
}
func OnNewUser(u *User) {
2018-06-30 00:57:05 +00:00
for _, p := range AllPlugins {
2018-06-19 04:48:25 +00:00
p.OnNewUser(structs.Map(u))
}
}
func OnNewService(s *Service) {
2018-06-30 00:57:05 +00:00
for _, p := range AllPlugins {
2018-06-19 04:48:25 +00:00
p.OnNewService(structs.Map(s))
}
}
func OnDeletedService(s *Service) {
2018-06-30 00:57:05 +00:00
for _, p := range AllPlugins {
2018-06-19 04:48:25 +00:00
p.OnDeletedService(structs.Map(s))
}
}
func OnUpdateService(s *Service) {
2018-06-30 00:57:05 +00:00
for _, p := range AllPlugins {
2018-06-19 04:48:25 +00:00
p.OnUpdatedService(structs.Map(s))
2018-06-14 01:19:00 +00:00
}
}
func SelectPlugin(name string) plugin.PluginActions {
2018-06-30 00:57:05 +00:00
for _, p := range AllPlugins {
2018-06-14 01:19:00 +00:00
if p.GetInfo().Name == name {
return p
}
}
return plugin.PluginInfo{}
}