Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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
ethanndickson merged 4 commits intomainfromethan/notif-warning-logs
Sep 17, 2025
Merged
Show file tree
Hide file tree
Changes fromall 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
18 changes: 16 additions & 2 deletionscoderd/notifications/enqueuer.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -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,
Expand DownExpand Up@@ -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()) {
return nil, ErrCannotEnqueueDisabledNotification
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
}

// 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) {
return nil, ErrDuplicate
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))
Expand Down
35 changes: 23 additions & 12 deletionscoderd/notifications/notifications_test.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -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"

Expand DownExpand Up@@ -1654,18 +1655,21 @@ func TestDisabledByDefaultBeforeEnqueue(t *testing.T) {

ctx := dbauthz.AsNotifier(testutil.Context(t, testutil.WaitSuperLong))
store, _ := dbtestutil.NewDB(t)
logger := testutil.Logger(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 tofail.
// by default. We expect this tobe a no-op that produces a debug log.
templateID := notifications.TemplateWorkspaceManuallyUpdated
_, err = enq.Enqueue(ctx, user.ID, templateID, map[string]string{}, "test")
require.ErrorIs(t, err, notifications.ErrCannotEnqueueDisabledNotification, "enqueuing did not fail with expected error")
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
Expand All@@ -1679,7 +1683,8 @@ func TestDisabledBeforeEnqueue(t *testing.T) {

ctx := dbauthz.AsNotifier(testutil.Context(t, testutil.WaitSuperLong))
store, _ := dbtestutil.NewDB(t)
logger := testutil.Logger(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)
Expand All@@ -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 with an error
_, err = enq.Enqueue(ctx, user.ID, templateID, map[string]string{}, "test")
require.ErrorIs(t, err, notifications.ErrCannotEnqueueDisabledNotification, "enqueueing did not fail with expected error")
// 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
Expand DownExpand Up@@ -1909,7 +1917,8 @@ func TestNotificationDuplicates(t *testing.T) {

ctx := dbauthz.AsNotifier(testutil.Context(t, testutil.WaitSuperLong))
store, pubsub := dbtestutil.NewDB(t)
logger := testutil.Logger(t)
logbuf := strings.Builder{}
logger := testutil.Logger(t).AppendSinks(sloghuman.Sink(&logbuf)).Leveled(slog.LevelDebug)

method := database.NotificationMethodSmtp
cfg := defaultNotificationsConfig(method)
Expand All@@ -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 rejectthe request.
_, err = enq.Enqueue(ctx, user.ID, notifications.TemplateWorkspaceDeleted,
// WHEN: the second is enqueued, the enqueuer will rejectit as a duplicate.
ids, err:= enq.Enqueue(ctx, user.ID, notifications.TemplateWorkspaceDeleted,
map[string]string{"initiator": "danny"}, "test", user.ID)
require.ErrorIs(t, err, notifications.ErrDuplicate)
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
Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp