From b8284d857b86a9961cdaabf8d73456f090b409b7 Mon Sep 17 00:00:00 2001 From: Javier Ferrer Date: Wed, 7 Jun 2023 17:16:39 +0200 Subject: [PATCH] pass if notifications are not configured --- changelog/unreleased/notifications-skip-init.md | 6 ++++++ pkg/notification/db_changes.sql | 4 +--- .../notificationhelper/notificationhelper.go | 12 ++++++++---- pkg/notification/utils/nats.go | 10 ++++++++-- 4 files changed, 23 insertions(+), 9 deletions(-) create mode 100644 changelog/unreleased/notifications-skip-init.md diff --git a/changelog/unreleased/notifications-skip-init.md b/changelog/unreleased/notifications-skip-init.md new file mode 100644 index 0000000000..70d532fcb7 --- /dev/null +++ b/changelog/unreleased/notifications-skip-init.md @@ -0,0 +1,6 @@ +Enhancement: Conditional notifications initialization + +Notification helpers in services will not try to initalize if +there is no specific configuration. + +https://github.com/cs3org/reva/pull/3969 diff --git a/pkg/notification/db_changes.sql b/pkg/notification/db_changes.sql index 7b7be6ad03..a240b63300 100644 --- a/pkg/notification/db_changes.sql +++ b/pkg/notification/db_changes.sql @@ -19,8 +19,6 @@ -- This file can be used to make the required changes to the MySQL DB. This is -- not a proper migration but it should work on most situations. -USE cernboxngcopy; - CREATE TABLE `cbox_notifications` ( `id` INT PRIMARY KEY AUTO_INCREMENT, `ref` VARCHAR(3072) UNIQUE NOT NULL, @@ -45,7 +43,7 @@ CREATE INDEX `cbox_notifications_ix0` ON `cbox_notifications` (`ref`); CREATE INDEX `cbox_notification_recipients_ix0` ON `cbox_notification_recipients` (`notification_id`); CREATE INDEX `cbox_notification_recipients_ix1` ON `cbox_notification_recipients` (`user_name`); --- changes for added notifications on ocm shares +-- changes for added notifications on oc shares ALTER TABLE cernboxngcopy.oc_share ADD notify_uploads BOOL DEFAULT false; diff --git a/pkg/notification/notificationhelper/notificationhelper.go b/pkg/notification/notificationhelper/notificationhelper.go index af92e2f4d1..f9ef0420eb 100644 --- a/pkg/notification/notificationhelper/notificationhelper.go +++ b/pkg/notification/notificationhelper/notificationhelper.go @@ -58,11 +58,18 @@ func defaultConfig() *Config { // New creates a new Notification Helper. func New(name string, m map[string]interface{}, log *zerolog.Logger) *NotificationHelper { + annotatedLogger := log.With().Str("service", name).Str("scope", "notifications").Logger() + conf := defaultConfig() nh := &NotificationHelper{ Name: name, Conf: conf, - Log: log, + Log: &annotatedLogger, + } + + if len(m) == 0 { + log.Info().Msgf("no 'notifications' field in service config, notifications will be disabled") + return nh } if err := mapstructure.Decode(m, conf); err != nil { @@ -70,9 +77,6 @@ func New(name string, m map[string]interface{}, log *zerolog.Logger) *Notificati return nh } - annotatedLogger := log.With().Str("service", nh.Name).Str("scope", "notifications").Logger() - nh.Log = &annotatedLogger - if err := nh.connect(); err != nil { log.Error().Err(err).Msgf("connecting to nats failed, notifications will be disabled") return nh diff --git a/pkg/notification/utils/nats.go b/pkg/notification/utils/nats.go index cf206f46e8..7639a2e60f 100644 --- a/pkg/notification/utils/nats.go +++ b/pkg/notification/utils/nats.go @@ -38,10 +38,16 @@ func ConnectToNats(natsAddress, natsToken string, log zerolog.Logger) (*nats.Con log.Error().Err(err).Msgf("nats error") }), nats.ClosedHandler(func(c *nats.Conn) { - log.Error().Err(c.LastError()).Msgf("connection to nats server closed") + if c.LastError() != nil { + log.Error().Err(c.LastError()).Msgf("connection to nats server closed") + } else { + log.Debug().Msgf("connection to nats server closed") + } }), nats.DisconnectErrHandler(func(_ *nats.Conn, err error) { - log.Error().Err(err).Msgf("connection to nats server disconnected") + if err != nil { + log.Error().Err(err).Msgf("connection to nats server disconnected") + } }), nats.CustomReconnectDelay(func(attempts int) time.Duration { if attempts%3 == 0 {