Skip to content

Commit 7687c06

Browse files
committed
checkRunning
1 parent feeac15 commit 7687c06

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

coderd/notifications/notifier.go

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,18 @@ func (n *notifier) run(ctx context.Context, success chan<- dispatchResult, failu
7171
default:
7272
}
7373

74-
// Check if notifier is paused.
75-
76-
// Call process() immediately (i.e. don't wait an initial tick).
77-
err := n.process(ctx, success, failure)
74+
// Check if notifier is not paused.
75+
ok, err := n.ensureRunning(ctx)
7876
if err != nil {
79-
n.log.Error(ctx, "failed to process messages", slog.Error(err))
77+
n.log.Error(ctx, "failed to check notifier state", slog.Error(err))
78+
}
79+
80+
if ok {
81+
// Call process() immediately (i.e. don't wait an initial tick).
82+
err = n.process(ctx, success, failure)
83+
if err != nil {
84+
n.log.Error(ctx, "failed to process messages", slog.Error(err))
85+
}
8086
}
8187

8288
// Shortcut to bail out quickly if stop() has been called or the context canceled.
@@ -91,6 +97,25 @@ func (n *notifier) run(ctx context.Context, success chan<- dispatchResult, failu
9197
}
9298
}
9399

100+
// ensureRunning checks if notifier is not paused.
101+
func (n *notifier) ensureRunning(ctx context.Context) (bool, error) {
102+
n.log.Debug(ctx, "check if notifier is paused")
103+
104+
settingsJSON, err := n.store.GetNotificationsSettings(ctx)
105+
if err != nil {
106+
return false, xerrors.Errorf("get notifications settings: %w", err)
107+
}
108+
109+
var settings codersdk.NotificationsSettings
110+
if len(settingsJSON) > 0 {
111+
err = json.Unmarshal([]byte(settingsJSON), &settings)
112+
if err != nil {
113+
return false, xerrors.Errorf("unmarshal notifications settings")
114+
}
115+
}
116+
return !settings.NotifierPaused, nil
117+
}
118+
94119
// process is responsible for coordinating the retrieval, processing, and delivery of messages.
95120
// Messages are dispatched concurrently, but they may block when success/failure channels are full.
96121
//

coderd/notifications/spec.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ type Store interface {
2121
EnqueueNotificationMessage(ctx context.Context, arg database.EnqueueNotificationMessageParams) (database.NotificationMessage, error)
2222
FetchNewMessageMetadata(ctx context.Context, arg database.FetchNewMessageMetadataParams) (database.FetchNewMessageMetadataRow, error)
2323
GetNotificationMessagesByStatus(ctx context.Context, arg database.GetNotificationMessagesByStatusParams) ([]database.NotificationMessage, error)
24+
GetNotificationsSettings(ctx context.Context) (string, error)
2425
}
2526

2627
// Handler is responsible for preparing and delivering a notification by a given method.

0 commit comments

Comments
 (0)