feat(coderd): add new dispatch logic for coder inbox (#16764)

This PR is [resolving the dispatch part of Coder
Inbocx](https://github.com/coder/internal/issues/403).

Since the DB layer has been merged - we now want to insert notifications
into Coder Inbox in parallel of the other delivery target.

To do so, we push two messages instead of one using the `Enqueue`
method.
This commit is contained in:
Vincent Vielle
2025-03-05 22:43:18 +01:00
committed by GitHub
parent 32450a2f77
commit 522181fead
42 changed files with 415 additions and 120 deletions
+12 -7
View File
@@ -38,6 +38,7 @@ func TestBufferedUpdates(t *testing.T) {
interceptor := &syncInterceptor{Store: store}
santa := &santaHandler{}
santaInbox := &santaHandler{}
cfg := defaultNotificationsConfig(database.NotificationMethodSmtp)
cfg.StoreSyncInterval = serpent.Duration(time.Hour) // Ensure we don't sync the store automatically.
@@ -45,9 +46,13 @@ func TestBufferedUpdates(t *testing.T) {
// GIVEN: a manager which will pass or fail notifications based on their "nice" labels
mgr, err := notifications.NewManager(cfg, interceptor, defaultHelpers(), createMetrics(), logger.Named("notifications-manager"))
require.NoError(t, err)
mgr.WithHandlers(map[database.NotificationMethod]notifications.Handler{
database.NotificationMethodSmtp: santa,
})
handlers := map[database.NotificationMethod]notifications.Handler{
database.NotificationMethodSmtp: santa,
database.NotificationMethodInbox: santaInbox,
}
mgr.WithHandlers(handlers)
enq, err := notifications.NewStoreEnqueuer(cfg, interceptor, defaultHelpers(), logger.Named("notifications-enqueuer"), quartz.NewReal())
require.NoError(t, err)
@@ -79,7 +84,7 @@ func TestBufferedUpdates(t *testing.T) {
// Wait for the expected number of buffered updates to be accumulated.
require.Eventually(t, func() bool {
success, failure := mgr.BufferedUpdatesCount()
return success == expectedSuccess && failure == expectedFailure
return success == expectedSuccess*len(handlers) && failure == expectedFailure*len(handlers)
}, testutil.WaitShort, testutil.IntervalFast)
// Stop the manager which forces an update of buffered updates.
@@ -93,8 +98,8 @@ func TestBufferedUpdates(t *testing.T) {
ct.FailNow()
}
assert.EqualValues(ct, expectedFailure, interceptor.failed.Load())
assert.EqualValues(ct, expectedSuccess, interceptor.sent.Load())
assert.EqualValues(ct, expectedFailure*len(handlers), interceptor.failed.Load())
assert.EqualValues(ct, expectedSuccess*len(handlers), interceptor.sent.Load())
}, testutil.WaitMedium, testutil.IntervalFast)
}
@@ -229,7 +234,7 @@ type enqueueInterceptor struct {
}
func newEnqueueInterceptor(db notifications.Store, metadataFn func() database.FetchNewMessageMetadataRow) *enqueueInterceptor {
return &enqueueInterceptor{Store: db, payload: make(chan types.MessagePayload, 1), metadataFn: metadataFn}
return &enqueueInterceptor{Store: db, payload: make(chan types.MessagePayload, 2), metadataFn: metadataFn}
}
func (e *enqueueInterceptor) EnqueueNotificationMessage(_ context.Context, arg database.EnqueueNotificationMessageParams) error {