feat(notifications): support delayed sending (#1142)

This commit is contained in:
nils måsén 2022-01-05 09:31:01 +01:00 committed by GitHub
parent 2fa8a2ad0c
commit 1d59fb83dd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 37 additions and 13 deletions

View file

@ -5,6 +5,7 @@ import (
stdlog "log"
"strings"
"text/template"
"time"
"github.com/containrrr/shoutrrr"
"github.com/containrrr/shoutrrr/pkg/types"
@ -77,14 +78,14 @@ func (n *shoutrrrTypeNotifier) GetNames() []string {
return names
}
func newShoutrrrNotifier(tplString string, acceptedLogLevels []log.Level, legacy bool, title string, urls ...string) t.Notifier {
func newShoutrrrNotifier(tplString string, acceptedLogLevels []log.Level, legacy bool, title string, delay time.Duration, urls ...string) t.Notifier {
notifier := createNotifier(urls, acceptedLogLevels, tplString, legacy)
notifier.params = &types.Params{"title": title}
log.AddHook(notifier)
// Do the sending in a separate goroutine so we don't block the main process.
go sendNotifications(notifier)
go sendNotifications(notifier, delay)
return notifier
}
@ -112,8 +113,9 @@ func createNotifier(urls []string, levels []log.Level, tplString string, legacy
}
}
func sendNotifications(n *shoutrrrTypeNotifier) {
func sendNotifications(n *shoutrrrTypeNotifier, delay time.Duration) {
for msg := range n.messages {
time.Sleep(delay)
errs := n.Router.Send(msg, n.params)
for i, err := range errs {