Skip to content

Commit

Permalink
pass if notifications are not configured (#3969)
Browse files Browse the repository at this point in the history
  • Loading branch information
javfg authored Jun 14, 2023
1 parent 4a502bb commit 43b4669
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 9 deletions.
6 changes: 6 additions & 0 deletions changelog/unreleased/notifications-skip-init.md
Original file line number Diff line number Diff line change
@@ -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
4 changes: 1 addition & 3 deletions pkg/notification/db_changes.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;

Expand Down
12 changes: 8 additions & 4 deletions pkg/notification/notificationhelper/notificationhelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,21 +58,25 @@ 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 {
log.Error().Err(err).Msgf("decoding config failed, notifications will be disabled")
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
Expand Down
10 changes: 8 additions & 2 deletions pkg/notification/utils/nats.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 43b4669

Please sign in to comment.