Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d9f1aaf

Browse files
authored
fix: stop logging errors on cancel in notifier (#15186)
fixes coder/internal#121 We shouldn't log errors when context is canceled, e.g. on shutdown. This breaks our tests and alarms customers needlessly.
1 parent 5bcaa93 commit d9f1aaf

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

coderd/notifications/fetcher.go

-4
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,15 @@ import (
77
"text/template"
88

99
"golang.org/x/xerrors"
10-
11-
"cdr.dev/slog"
1210
)
1311

1412
func (n *notifier) fetchHelpers(ctx context.Context) (map[string]any, error) {
1513
appName, err := n.fetchAppName(ctx)
1614
if err != nil {
17-
n.log.Error(ctx, "failed to fetch app name", slog.Error(err))
1815
return nil, xerrors.Errorf("fetch app name: %w", err)
1916
}
2017
logoURL, err := n.fetchLogoURL(ctx)
2118
if err != nil {
22-
n.log.Error(ctx, "failed to fetch logo URL", slog.Error(err))
2319
return nil, xerrors.Errorf("fetch logo URL: %w", err)
2420
}
2521

coderd/notifications/notifier.go

+24-4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package notifications
33
import (
44
"context"
55
"encoding/json"
6+
"fmt"
67
"sync"
78
"text/template"
89

@@ -27,7 +28,22 @@ const (
2728
notificationsDefaultAppName = "Coder"
2829
)
2930

30-
var errDecorateHelpersFailed = xerrors.New("failed to decorate helpers")
31+
type decorateHelpersError struct {
32+
inner error
33+
}
34+
35+
func (e decorateHelpersError) Error() string {
36+
return fmt.Sprintf("failed to decorate helpers: %s", e.inner.Error())
37+
}
38+
39+
func (e decorateHelpersError) Unwrap() error {
40+
return e.inner
41+
}
42+
43+
func (decorateHelpersError) Is(other error) bool {
44+
_, ok := other.(decorateHelpersError)
45+
return ok
46+
}
3147

3248
// notifier is a consumer of the notifications_messages queue. It dequeues messages from that table and processes them
3349
// through a pipeline of fetch -> prepare -> render -> acquire handler -> deliver.
@@ -164,8 +180,12 @@ func (n *notifier) process(ctx context.Context, success chan<- dispatchResult, f
164180
// A message failing to be prepared correctly should not affect other messages.
165181
deliverFn, err := n.prepare(ctx, msg)
166182
if err != nil {
167-
n.log.Warn(ctx, "dispatcher construction failed", slog.F("msg_id", msg.ID), slog.Error(err))
168-
failure <- n.newFailedDispatch(msg, err, xerrors.Is(err, errDecorateHelpersFailed))
183+
if database.IsQueryCanceledError(err) {
184+
n.log.Debug(ctx, "dispatcher construction canceled", slog.F("msg_id", msg.ID), slog.Error(err))
185+
} else {
186+
n.log.Error(ctx, "dispatcher construction failed", slog.F("msg_id", msg.ID), slog.Error(err))
187+
}
188+
failure <- n.newFailedDispatch(msg, err, xerrors.Is(err, decorateHelpersError{}))
169189
n.metrics.PendingUpdates.Set(float64(len(success) + len(failure)))
170190
continue
171191
}
@@ -226,7 +246,7 @@ func (n *notifier) prepare(ctx context.Context, msg database.AcquireNotification
226246

227247
helpers, err := n.fetchHelpers(ctx)
228248
if err != nil {
229-
return nil, errDecorateHelpersFailed
249+
return nil, decorateHelpersError{err}
230250
}
231251

232252
var title, body string

0 commit comments

Comments
 (0)