- Notifications
You must be signed in to change notification settings - Fork1k
chore(coderd/notifications): avoid generating warning logs for trivial enqueue failures#19840
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
4 commits Select commitHold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
18 changes: 16 additions & 2 deletionscoderd/notifications/enqueuer.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -73,12 +73,14 @@ func NewStoreEnqueuer(cfg codersdk.NotificationsConfig, store Store, helpers tem | ||
} | ||
// Enqueue queues a notification message for later delivery, assumes no structured input data. | ||
// Returns the IDs of successfully enqueued messages, if any. | ||
func (s *StoreEnqueuer) Enqueue(ctx context.Context, userID, templateID uuid.UUID, labels map[string]string, createdBy string, targets ...uuid.UUID) ([]uuid.UUID, error) { | ||
return s.EnqueueWithData(ctx, userID, templateID, labels, nil, createdBy, targets...) | ||
} | ||
// Enqueue queues a notification message for later delivery. | ||
// Messages will be dequeued by a notifier later and dispatched. | ||
// Returns the IDs of successfully enqueued messages, if any. | ||
func (s *StoreEnqueuer) EnqueueWithData(ctx context.Context, userID, templateID uuid.UUID, labels map[string]string, data map[string]any, createdBy string, targets ...uuid.UUID) ([]uuid.UUID, error) { | ||
metadata, err := s.store.FetchNewMessageMetadata(ctx, database.FetchNewMessageMetadataParams{ | ||
UserID: userID, | ||
@@ -141,14 +143,26 @@ func (s *StoreEnqueuer) EnqueueWithData(ctx context.Context, userID, templateID | ||
// | ||
// This is more efficient than fetching the user's preferences for each enqueue, and centralizes the business logic. | ||
if strings.Contains(err.Error(), ErrCannotEnqueueDisabledNotification.Error()) { | ||
s.log.Debug(ctx, "notification not enqueued", | ||
slog.F("template_id", templateID), | ||
slog.F("user_id", userID), | ||
slog.F("method", method), | ||
slog.Error(ErrCannotEnqueueDisabledNotification), | ||
) | ||
continue | ||
dannykopping marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
} | ||
// If the enqueue fails due to a dedupe hash conflict, this means that a notification has already been enqueued | ||
// today with identical properties. It's far simpler to prevent duplicate sends in this central manner, rather than | ||
// having each notification enqueue handle its own logic. | ||
if database.IsUniqueViolation(err, database.UniqueNotificationMessagesDedupeHashIndex) { | ||
s.log.Debug(ctx, "notification not enqueued", | ||
slog.F("template_id", templateID), | ||
slog.F("user_id", userID), | ||
slog.F("method", method), | ||
slog.Error(ErrDuplicate), | ||
) | ||
continue | ||
} | ||
s.log.Warn(ctx, "failed to enqueue notification", slog.F("template_id", templateID), slog.F("input", input), slog.Error(err)) | ||
35 changes: 23 additions & 12 deletionscoderd/notifications/notifications_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -35,6 +35,7 @@ import ( | ||
"golang.org/x/xerrors" | ||
"cdr.dev/slog" | ||
"cdr.dev/slog/sloggers/sloghuman" | ||
"github.com/coder/quartz" | ||
"github.com/coder/serpent" | ||
@@ -1654,18 +1655,21 @@ func TestDisabledByDefaultBeforeEnqueue(t *testing.T) { | ||
ctx := dbauthz.AsNotifier(testutil.Context(t, testutil.WaitSuperLong)) | ||
store, _ := dbtestutil.NewDB(t) | ||
logbuf := strings.Builder{} | ||
logger := testutil.Logger(t).AppendSinks(sloghuman.Sink(&logbuf)).Leveled(slog.LevelDebug) | ||
cfg := defaultNotificationsConfig(database.NotificationMethodSmtp) | ||
enq, err := notifications.NewStoreEnqueuer(cfg, store, defaultHelpers(), logger.Named("enqueuer"), quartz.NewReal()) | ||
require.NoError(t, err) | ||
user := createSampleUser(t, store) | ||
// We want to try enqueuing a notification on a template that is disabled | ||
// by default. We expect this tobe a no-op that produces a debug log. | ||
templateID := notifications.TemplateWorkspaceManuallyUpdated | ||
notifIDs, err := enq.Enqueue(ctx, user.ID, templateID, map[string]string{}, "test") | ||
require.NoError(t, err) | ||
require.Contains(t, logbuf.String(), notifications.ErrCannotEnqueueDisabledNotification.Error()) | ||
require.Empty(t, notifIDs) | ||
} | ||
// TestDisabledBeforeEnqueue ensures that notifications cannot be enqueued once a user has disabled that notification template | ||
@@ -1679,7 +1683,8 @@ func TestDisabledBeforeEnqueue(t *testing.T) { | ||
ctx := dbauthz.AsNotifier(testutil.Context(t, testutil.WaitSuperLong)) | ||
store, _ := dbtestutil.NewDB(t) | ||
logbuf := strings.Builder{} | ||
logger := testutil.Logger(t).AppendSinks(sloghuman.Sink(&logbuf)).Leveled(slog.LevelDebug) | ||
// GIVEN: an enqueuer & a sample user | ||
cfg := defaultNotificationsConfig(database.NotificationMethodSmtp) | ||
@@ -1697,9 +1702,12 @@ func TestDisabledBeforeEnqueue(t *testing.T) { | ||
require.NoError(t, err, "failed to set preferences") | ||
require.EqualValues(t, 1, n, "unexpected number of affected rows") | ||
// THEN: enqueuing the "workspace deleted" notification should fail be | ||
// a no-op that produces a debug log | ||
notifIDs, err := enq.Enqueue(ctx, user.ID, templateID, map[string]string{}, "test") | ||
require.NoError(t, err) | ||
require.Contains(t, logbuf.String(), notifications.ErrCannotEnqueueDisabledNotification.Error()) | ||
require.Empty(t, notifIDs) | ||
} | ||
// TestDisabledAfterEnqueue ensures that notifications enqueued before a notification template was disabled will not be | ||
@@ -1909,7 +1917,8 @@ func TestNotificationDuplicates(t *testing.T) { | ||
ctx := dbauthz.AsNotifier(testutil.Context(t, testutil.WaitSuperLong)) | ||
store, pubsub := dbtestutil.NewDB(t) | ||
logbuf := strings.Builder{} | ||
logger := testutil.Logger(t).AppendSinks(sloghuman.Sink(&logbuf)).Leveled(slog.LevelDebug) | ||
method := database.NotificationMethodSmtp | ||
cfg := defaultNotificationsConfig(method) | ||
@@ -1933,10 +1942,12 @@ func TestNotificationDuplicates(t *testing.T) { | ||
map[string]string{"initiator": "danny"}, "test", user.ID) | ||
require.NoError(t, err) | ||
// WHEN: the second is enqueued, the enqueuer will rejectit as a duplicate. | ||
ethanndickson marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
ids, err:= enq.Enqueue(ctx, user.ID, notifications.TemplateWorkspaceDeleted, | ||
map[string]string{"initiator": "danny"}, "test", user.ID) | ||
require.NoError(t, err) | ||
require.Contains(t, logbuf.String(), notifications.ErrDuplicate.Error()) | ||
require.Empty(t, ids) | ||
// THEN: when the clock is advanced 24h, the notification will be accepted. | ||
// NOTE: the time is used in the dedupe hash, so by advancing 24h we're creating a distinct notification from the one | ||
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.