Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not try to initialize notification helper if settings are not there #3969

Merged
merged 1 commit into from
Jun 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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