From ce6ba0801ff6149e06e44ef3d3037ad4c85f1a89 Mon Sep 17 00:00:00 2001 From: Simon Aronsson Date: Sun, 25 Aug 2019 13:14:02 +0200 Subject: [PATCH] feature: add optional email delay resolves #256 --- docs/notifications.md | 2 ++ internal/flags/flags.go | 6 ++++++ pkg/notifications/email.go | 13 +++++++++++-- 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/docs/notifications.md b/docs/notifications.md index af6a5ec..5741566 100644 --- a/docs/notifications.md +++ b/docs/notifications.md @@ -26,6 +26,7 @@ To receive notifications by email, the following command-line options, or their - `--notification-email-server-port` (env. `WATCHTOWER_NOTIFICATION_EMAIL_SERVER_PORT`): The port used to connect to the SMTP server to send e-mails through. Defaults to `25`. - `--notification-email-server-user` (env. `WATCHTOWER_NOTIFICATION_EMAIL_SERVER_USER`): The username to authenticate with the SMTP server with. - `--notification-email-server-password` (env. `WATCHTOWER_NOTIFICATION_EMAIL_SERVER_PASSWORD`): The password to authenticate with the SMTP server with. +- `--notification-email-delay` (env. `WATCHTOWER_NOTIFICATION_EMAIL_DELAY`): Delay before sending notifications expressed in seconds. Example: @@ -39,6 +40,7 @@ docker run -d \ -e WATCHTOWER_NOTIFICATION_EMAIL_SERVER=smtp.gmail.com \ -e WATCHTOWER_NOTIFICATION_EMAIL_SERVER_USER=fromaddress@gmail.com \ -e WATCHTOWER_NOTIFICATION_EMAIL_SERVER_PASSWORD=app_password \ + -e WATCHTOWER_NOTIFICATION_EMAIL_DELAY=2 \ containrrr/watchtower ``` diff --git a/internal/flags/flags.go b/internal/flags/flags.go index 179bc63..6e9ea55 100644 --- a/internal/flags/flags.go +++ b/internal/flags/flags.go @@ -128,6 +128,12 @@ func RegisterNotificationFlags(rootCmd *cobra.Command) { "", viper.GetString("WATCHTOWER_NOTIFICATION_EMAIL_TO"), "Address to send notification emails to") + + flags.IntP( + "notification-email-delay", + "", + viper.GetInt("WATCHTOWER_NOTIFICATION_EMAIL_DELAY"), + "Delay before sending notifications, expressed in seconds") flags.StringP( "notification-email-server", diff --git a/pkg/notifications/email.go b/pkg/notifications/email.go index 60db9cb..b5ef979 100644 --- a/pkg/notifications/email.go +++ b/pkg/notifications/email.go @@ -29,6 +29,7 @@ type emailTypeNotifier struct { tlsSkipVerify bool entries []*log.Entry logLevels []log.Level + delay time.Duration } func newEmailNotifier(c *cobra.Command, acceptedLogLevels []log.Level) t.Notifier { @@ -41,6 +42,7 @@ func newEmailNotifier(c *cobra.Command, acceptedLogLevels []log.Level) t.Notifie password, _ := flags.GetString("notification-email-server-password") port, _ := flags.GetInt("notification-email-server-port") tlsSkipVerify, _ := flags.GetBool("notification-email-server-tls-skip-verify") + delay, _ := flags.GetInt("notification-email-delay") n := &emailTypeNotifier{ From: from, @@ -51,6 +53,7 @@ func newEmailNotifier(c *cobra.Command, acceptedLogLevels []log.Level) t.Notifie Port: port, tlsSkipVerify: tlsSkipVerify, logLevels: acceptedLogLevels, + delay: time.Duration(delay) * time.Second, } log.AddHook(n) @@ -117,9 +120,15 @@ func (e *emailTypeNotifier) StartNotification() { } func (e *emailTypeNotifier) SendNotification() { - if e.entries != nil && len(e.entries) != 0 { - e.sendEntries(e.entries) + if e.entries == nil || len(e.entries) <= 0 { + return } + + if e.delay > 0 { + time.Sleep(e.delay) + } + + e.sendEntries(e.entries) e.entries = nil }