diff --git a/cmd/root.go b/cmd/root.go index daa9437..de75ce2 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -139,6 +139,7 @@ func PreRun(cmd *cobra.Command, _ []string) { }) notifier = notifications.NewNotifier(cmd) + notifier.AddLogHook() } // Run is the main execution flow of the command diff --git a/pkg/notifications/notifier.go b/pkg/notifications/notifier.go index fba5dc0..03ea644 100644 --- a/pkg/notifications/notifier.go +++ b/pkg/notifications/notifier.go @@ -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 diff --git a/pkg/notifications/shoutrrr.go b/pkg/notifications/shoutrrr.go index e816cf7..04436a0 100644 --- a/pkg/notifications/shoutrrr.go +++ b/pkg/notifications/shoutrrr.go @@ -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 { diff --git a/pkg/notifications/shoutrrr_test.go b/pkg/notifications/shoutrrr_test.go index 0a10eb1..c2f94fb 100644 --- a/pkg/notifications/shoutrrr_test.go +++ b/pkg/notifications/shoutrrr_test.go @@ -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) diff --git a/pkg/types/notifier.go b/pkg/types/notifier.go index ccb2cb6..6fcf042 100644 --- a/pkg/types/notifier.go +++ b/pkg/types/notifier.go @@ -4,6 +4,7 @@ package types type Notifier interface { StartNotification() SendNotification(Report) + AddLogHook() GetNames() []string Close() }