make notify log hook opt-in

This commit is contained in:
nils måsén 2022-09-07 15:03:08 +02:00
parent 102566032a
commit f817098cd5
5 changed files with 22 additions and 14 deletions

View file

@ -13,7 +13,7 @@ import (
// NewNotifier creates and returns a new Notifier, using global configuration.
func NewNotifier(c *cobra.Command) ty.Notifier {
f := c.PersistentFlags()
f := c.Flags()
level, _ := f.GetString("notifications-level")
logLevel, err := log.ParseLevel(level)
@ -35,7 +35,7 @@ func NewNotifier(c *cobra.Command) ty.Notifier {
data := GetTemplateData(c)
urls, delay := AppendLegacyUrls(urls, c, data.Title)
return newShoutrrrNotifier(tplString, levels, !reportTemplate, data, delay, stdout, urls...)
return createNotifier(urls, levels, tplString, !reportTemplate, data, stdout, delay)
}
// AppendLegacyUrls creates shoutrrr equivalent URLs from legacy notification flags

View file

@ -39,6 +39,8 @@ type shoutrrrTypeNotifier struct {
legacyTemplate bool
params *types.Params
data StaticData
receiving bool
delay time.Duration
}
// GetScheme returns the scheme part of a Shoutrrr URL
@ -61,13 +63,15 @@ func (n *shoutrrrTypeNotifier) GetNames() []string {
func newShoutrrrNotifier(tplString string, levels []log.Level, legacy bool, data StaticData, delay time.Duration, stdout bool, urls ...string) t.Notifier {
notifier := createNotifier(urls, levels, tplString, legacy, data, stdout)
log.AddHook(notifier)
func (n *shoutrrrTypeNotifier) AddLogHook() {
if n.receiving {
return
}
n.receiving = true
log.AddHook(n)
// Do the sending in a separate goroutine so we don't block the main process.
go sendNotifications(notifier, delay)
return notifier
go sendNotifications(n)
}
func createNotifier(urls []string, levels []log.Level, tplString string, legacy bool, data StaticData, stdout bool) *shoutrrrTypeNotifier {
@ -105,9 +109,9 @@ func createNotifier(urls []string, levels []log.Level, tplString string, legacy
}
}
func sendNotifications(n *shoutrrrTypeNotifier, delay time.Duration) {
func sendNotifications(n *shoutrrrTypeNotifier) {
for msg := range n.messages {
time.Sleep(delay)
time.Sleep(n.delay)
errs := n.Router.Send(msg, n.params)
for i, err := range errs {

View file

@ -90,7 +90,7 @@ updt1 (mock/updt1:latest): Updated
cmd := new(cobra.Command)
flags.RegisterNotificationFlags(cmd)
shoutrrr := createNotifier([]string{}, logrus.AllLevels, "", true, StaticData{}, false)
shoutrrr := createNotifier([]string{}, logrus.AllLevels, "", true, StaticData{}, false, time.Second)
entries := []*logrus.Entry{
{
@ -245,7 +245,7 @@ Turns out everything is on fire
When("batching notifications", func() {
When("no messages are queued", func() {
It("should not send any notification", func() {
shoutrrr := newShoutrrrNotifier("", allButTrace, true, StaticData{}, time.Duration(0), false, "logger://")
shoutrrr := createNotifier([]string{"logger://"}, allButTrace, "", true, StaticData{}, false, time.Duration(0))
shoutrrr.StartNotification()
shoutrrr.SendNotification(nil)
Consistently(logBuffer).ShouldNot(gbytes.Say(`Shoutrrr:`))
@ -253,7 +253,8 @@ Turns out everything is on fire
})
When("at least one message is queued", func() {
It("should send a notification", func() {
shoutrrr := newShoutrrrNotifier("", allButTrace, true, StaticData{}, time.Duration(0), false, "logger://")
shoutrrr := createNotifier([]string{"logger://"}, allButTrace, "", true, StaticData{}, false, time.Duration(0))
shoutrrr.AddLogHook()
shoutrrr.StartNotification()
logrus.Info("This log message is sponsored by ContainrrrVPN")
shoutrrr.SendNotification(nil)
@ -267,7 +268,7 @@ Turns out everything is on fire
shoutrrr := createNotifier([]string{"logger://"}, allButTrace, "", true, StaticData{
Host: "test.host",
Title: "",
}, false)
}, false, time.Second)
_, found := shoutrrr.params.Title()
Expect(found).ToNot(BeTrue())
})
@ -321,13 +322,14 @@ func sendNotificationsWithBlockingRouter(legacy bool) (*shoutrrrTypeNotifier, *b
Router: router,
legacyTemplate: legacy,
params: &types.Params{},
delay: time.Duration(0),
}
entry := &logrus.Entry{
Message: "foo bar",
}
go sendNotifications(shoutrrr, time.Duration(0))
go sendNotifications(shoutrrr)
shoutrrr.StartNotification()
_ = shoutrrr.Fire(entry)

View file

@ -4,6 +4,7 @@ package types
type Notifier interface {
StartNotification()
SendNotification(Report)
AddLogHook()
GetNames() []string
Close()
}