feature: add optional email delay

resolves #256
pull/379/head
Simon Aronsson 5 years ago
parent 17cbf86d48
commit ce6ba0801f

@ -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-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-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-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: Example:
@ -39,6 +40,7 @@ docker run -d \
-e WATCHTOWER_NOTIFICATION_EMAIL_SERVER=smtp.gmail.com \ -e WATCHTOWER_NOTIFICATION_EMAIL_SERVER=smtp.gmail.com \
-e WATCHTOWER_NOTIFICATION_EMAIL_SERVER_USER=fromaddress@gmail.com \ -e WATCHTOWER_NOTIFICATION_EMAIL_SERVER_USER=fromaddress@gmail.com \
-e WATCHTOWER_NOTIFICATION_EMAIL_SERVER_PASSWORD=app_password \ -e WATCHTOWER_NOTIFICATION_EMAIL_SERVER_PASSWORD=app_password \
-e WATCHTOWER_NOTIFICATION_EMAIL_DELAY=2 \
containrrr/watchtower containrrr/watchtower
``` ```

@ -129,6 +129,12 @@ func RegisterNotificationFlags(rootCmd *cobra.Command) {
viper.GetString("WATCHTOWER_NOTIFICATION_EMAIL_TO"), viper.GetString("WATCHTOWER_NOTIFICATION_EMAIL_TO"),
"Address to send notification emails 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( flags.StringP(
"notification-email-server", "notification-email-server",
"", "",

@ -29,6 +29,7 @@ type emailTypeNotifier struct {
tlsSkipVerify bool tlsSkipVerify bool
entries []*log.Entry entries []*log.Entry
logLevels []log.Level logLevels []log.Level
delay time.Duration
} }
func newEmailNotifier(c *cobra.Command, acceptedLogLevels []log.Level) t.Notifier { 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") password, _ := flags.GetString("notification-email-server-password")
port, _ := flags.GetInt("notification-email-server-port") port, _ := flags.GetInt("notification-email-server-port")
tlsSkipVerify, _ := flags.GetBool("notification-email-server-tls-skip-verify") tlsSkipVerify, _ := flags.GetBool("notification-email-server-tls-skip-verify")
delay, _ := flags.GetInt("notification-email-delay")
n := &emailTypeNotifier{ n := &emailTypeNotifier{
From: from, From: from,
@ -51,6 +53,7 @@ func newEmailNotifier(c *cobra.Command, acceptedLogLevels []log.Level) t.Notifie
Port: port, Port: port,
tlsSkipVerify: tlsSkipVerify, tlsSkipVerify: tlsSkipVerify,
logLevels: acceptedLogLevels, logLevels: acceptedLogLevels,
delay: time.Duration(delay) * time.Second,
} }
log.AddHook(n) log.AddHook(n)
@ -117,9 +120,15 @@ func (e *emailTypeNotifier) StartNotification() {
} }
func (e *emailTypeNotifier) SendNotification() { func (e *emailTypeNotifier) SendNotification() {
if e.entries != nil && len(e.entries) != 0 { if e.entries == nil || len(e.entries) <= 0 {
e.sendEntries(e.entries) return
}
if e.delay > 0 {
time.Sleep(e.delay)
} }
e.sendEntries(e.entries)
e.entries = nil e.entries = nil
} }

Loading…
Cancel
Save