feat(jobs): Module wiring + WorkerOptions.OnDeadLetter hook

This commit is contained in:
2026-05-06 08:31:24 +08:00
parent 2d0c45a52b
commit 895a816221
4 changed files with 279 additions and 0 deletions
+31
View File
@@ -10,6 +10,7 @@ import (
"testing"
"time"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
@@ -166,3 +167,33 @@ func TestWorker_UnknownTypeDeadLetters(t *testing.T) {
cancel()
w.Wait()
}
func TestWorker_DeadLetterHookCalled(t *testing.T) {
t.Parallel()
ctx := context.Background()
repo, _ := setupRepo(t)
enqueued, err := repo.Enqueue(ctx, jobs.TypeWebhookDeliver, json.RawMessage(`{}`), time.Now(), 5)
require.NoError(t, err)
h := &recordingHandler{typ: jobs.TypeWebhookDeliver, wantErr: jobs.Permanent(errors.New("4xx"))}
var called atomic.Int32
var seenID atomic.Value // uuid.UUID
w := jobs.NewWorker(repo, map[jobs.JobType]jobs.Handler{jobs.TypeWebhookDeliver: h},
jobs.WorkerOptions{
ID: "t", PollInterval: 5 * time.Millisecond, Backoff: []time.Duration{time.Second},
Clock: clock.Real(), Log: discardLogger(),
OnDeadLetter: func(_ context.Context, j jobs.Job, _ error) {
seenID.Store(j.ID)
called.Add(1)
},
})
cctx, cancel := context.WithTimeout(ctx, 500*time.Millisecond)
defer cancel()
go w.Run(cctx)
require.Eventually(t, func() bool { return called.Load() >= 1 }, 500*time.Millisecond, 10*time.Millisecond)
cancel()
w.Wait()
if id, ok := seenID.Load().(uuid.UUID); ok {
assert.Equal(t, enqueued.ID, id)
}
}