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

@ -1,11 +1,13 @@
package notifications
import (
"os"
"time"
ty "github.com/containrrr/watchtower/pkg/types"
"github.com/johntdyer/slackrus"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"os"
)
// NewNotifier creates and returns a new Notifier, using global configuration.
@ -28,14 +30,14 @@ func NewNotifier(c *cobra.Command) ty.Notifier {
tplString, _ := f.GetString("notification-template")
urls, _ := f.GetStringArray("notification-url")
urls = AppendLegacyUrls(urls, c)
urls, delay := AppendLegacyUrls(urls, c)
title := GetTitle(c)
return newShoutrrrNotifier(tplString, acceptedLogLevels, !reportTemplate, title, urls...)
return newShoutrrrNotifier(tplString, acceptedLogLevels, !reportTemplate, title, delay, urls...)
}
// AppendLegacyUrls creates shoutrrr equivalent URLs from legacy notification flags
func AppendLegacyUrls(urls []string, cmd *cobra.Command) []string {
func AppendLegacyUrls(urls []string, cmd *cobra.Command) ([]string, time.Duration) {
// Parse types and create notifiers.
types, err := cmd.Flags().GetStringSlice("notifications")
@ -43,6 +45,8 @@ func AppendLegacyUrls(urls []string, cmd *cobra.Command) []string {
log.WithError(err).Fatal("could not read notifications argument")
}
delay := time.Duration(0)
for _, t := range types {
var legacyNotifier ty.ConvertibleNotifier
@ -71,9 +75,13 @@ func AppendLegacyUrls(urls []string, cmd *cobra.Command) []string {
}
urls = append(urls, shoutrrrURL)
if delayNotifier, ok := legacyNotifier.(ty.DelayNotifier); ok {
delay = delayNotifier.GetDelay()
}
log.WithField("URL", shoutrrrURL).Trace("created Shoutrrr URL from legacy notifier")
}
return urls
return urls, delay
}
// GetTitle returns a common notification title with hostname appended