Skip to content

Commit 8b7fa18

Browse files
committed
Use UUID for notifier IDs
Fix test which uses coalesced fullname/username and was failing Signed-off-by: Danny Kopping <danny@coder.com>
1 parent 49b2163 commit 8b7fa18

File tree

3 files changed

+13
-10
lines changed

3 files changed

+13
-10
lines changed

coderd/notifications/manager.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ func (m *Manager) loop(ctx context.Context, notifiers int) error {
146146
for i := 0; i < notifiers; i++ {
147147
eg.Go(func() error {
148148
m.notifierMu.Lock()
149-
n := newNotifier(ctx, m.cfg, i+1, m.log, m.store, m.handlers)
149+
n := newNotifier(ctx, m.cfg, uuid.New(), m.log, m.store, m.handlers)
150150
m.notifiers = append(m.notifiers, n)
151151
m.notifierMu.Unlock()
152152
return n.run(ctx, success, failure)
@@ -424,22 +424,22 @@ func (m *Manager) Stop(ctx context.Context) error {
424424
}
425425

426426
type dispatchResult struct {
427-
notifier int
427+
notifier uuid.UUID
428428
msg uuid.UUID
429429
ts time.Time
430430
err error
431431
retryable bool
432432
}
433433

434-
func newSuccessfulDispatch(notifier int, msg uuid.UUID) dispatchResult {
434+
func newSuccessfulDispatch(notifier, msg uuid.UUID) dispatchResult {
435435
return dispatchResult{
436436
notifier: notifier,
437437
msg: msg,
438438
ts: time.Now(),
439439
}
440440
}
441441

442-
func newFailedDispatch(notifier int, msg uuid.UUID, err error, retryable bool) dispatchResult {
442+
func newFailedDispatch(notifier, msg uuid.UUID, err error, retryable bool) dispatchResult {
443443
return dispatchResult{
444444
notifier: notifier,
445445
msg: msg,

coderd/notifications/notifications_test.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,8 @@ func TestWebhookDispatch(t *testing.T) {
172172
require.Equal(t, *msgID, payload.MsgID)
173173
require.Equal(t, payload.Payload.Labels, input)
174174
require.Equal(t, payload.Payload.UserEmail, "bob@coder.com")
175-
require.Equal(t, payload.Payload.UserName, "bob")
175+
// UserName is coalesced from `name` and `username`; in this case `name` wins.
176+
require.Equal(t, payload.Payload.UserName, "Robert McBobbington")
176177
require.Equal(t, payload.Payload.NotificationName, "Workspace Deleted")
177178

178179
w.WriteHeader(http.StatusOK)
@@ -203,6 +204,7 @@ func TestWebhookDispatch(t *testing.T) {
203204
_, user := coderdtest.CreateAnotherUserMutators(t, client, first.OrganizationID, nil, func(r *codersdk.CreateUserRequest) {
204205
r.Email = "bob@coder.com"
205206
r.Username = "bob"
207+
r.Name = "Robert McBobbington"
206208
})
207209

208210
// when

coderd/notifications/notifier.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"sync"
77
"time"
88

9+
"github.com/google/uuid"
910
"golang.org/x/sync/errgroup"
1011
"golang.org/x/xerrors"
1112

@@ -21,7 +22,7 @@ import (
2122
// notifier is a consumer of the notifications_messages queue. It dequeues messages from that table and processes them
2223
// through a pipeline of fetch -> prepare -> render -> acquire handler -> deliver.
2324
type notifier struct {
24-
id int
25+
id uuid.UUID
2526
cfg codersdk.NotificationsConfig
2627
ctx context.Context
2728
log slog.Logger
@@ -35,7 +36,7 @@ type notifier struct {
3536
handlers *HandlerRegistry
3637
}
3738

38-
func newNotifier(ctx context.Context, cfg codersdk.NotificationsConfig, id int, log slog.Logger, db Store, hr *HandlerRegistry) *notifier {
39+
func newNotifier(ctx context.Context, cfg codersdk.NotificationsConfig, id uuid.UUID, log slog.Logger, db Store, hr *HandlerRegistry) *notifier {
3940
return &notifier{
4041
id: id,
4142
ctx: ctx,
@@ -63,7 +64,7 @@ func (n *notifier) run(ctx context.Context, success chan<- dispatchResult, failu
6364
for {
6465
select {
6566
case <-ctx.Done():
66-
return xerrors.Errorf("notifier %d context canceled: %w", n.id, ctx.Err())
67+
return xerrors.Errorf("notifier %q context canceled: %w", n.id, ctx.Err())
6768
case <-n.quit:
6869
return nil
6970
default:
@@ -78,7 +79,7 @@ func (n *notifier) run(ctx context.Context, success chan<- dispatchResult, failu
7879
// Shortcut to bail out quickly if stop() has been called or the context canceled.
7980
select {
8081
case <-ctx.Done():
81-
return xerrors.Errorf("notifier %d context canceled: %w", n.id, ctx.Err())
82+
return xerrors.Errorf("notifier %q context canceled: %w", n.id, ctx.Err())
8283
case <-n.quit:
8384
return nil
8485
case <-n.tick.C:
@@ -137,7 +138,7 @@ func (n *notifier) fetch(ctx context.Context) ([]database.AcquireNotificationMes
137138
msgs, err := n.store.AcquireNotificationMessages(ctx, database.AcquireNotificationMessagesParams{
138139
Count: int32(n.cfg.LeaseCount),
139140
MaxAttemptCount: int32(n.cfg.MaxSendAttempts),
140-
NotifierID: int32(n.id),
141+
NotifierID: n.id,
141142
LeaseSeconds: int32(n.cfg.LeasePeriod.Value().Seconds()),
142143
})
143144
if err != nil {

0 commit comments

Comments
 (0)