feat(worker): 五个消费者改用 ConsumeLoop 分派重试与停泊

每个 worker 的 Run 只保留参数校验。消费循环统一由 rabbitmq.ConsumeLoop 驱动。
因此重试、停泊、Ack 的规则只有一份实现。

坏消息一律标记 rabbitmq.Permanent。因此解析失败与未知 action 立即停泊。
未知 action 不再被静默 Ack 丢弃。

notification worker 改按队列与 body.action 分派。重试回源会把 routing key
改写成队列名。因此按 routing key 判断会让第二次投递立即停泊。

验证:go test -count=1 ./... 全绿
This commit is contained in:
2026-10-30 15:20:15 +08:00
parent 03d4301020
commit 49fc17c445
10 changed files with 290 additions and 369 deletions

View File

@@ -93,7 +93,7 @@ func TestNotificationWorker_DuplicateDeliveryWritesOneRow(t *testing.T) {
t.Fatalf("seed video: %v", err)
}
hub := &fakeHub{}
w := NewNotificationWorker(nil, db, rabbitmq.NotificationLikeQueue, hub)
w := NewNotificationWorker(nil, nil, db, rabbitmq.NotificationLikeQueue, hub)
ctx := context.Background()
d := likeDelivery(t, "intent-0009", 1, 10)
@@ -121,7 +121,7 @@ func TestNotificationWorker_MissingEventIDDoesNotDedupe(t *testing.T) {
t.Fatalf("seed video: %v", err)
}
hub := &fakeHub{}
w := NewNotificationWorker(nil, db, rabbitmq.NotificationLikeQueue, hub)
w := NewNotificationWorker(nil, nil, db, rabbitmq.NotificationLikeQueue, hub)
ctx := context.Background()
d := likeDelivery(t, "", 1, 11)
@@ -144,7 +144,7 @@ func TestNotificationWorker_SelfActionWritesNothing(t *testing.T) {
if err := db.Create(&testVideoRow{ID: 12, AuthorID: 7}).Error; err != nil {
t.Fatalf("seed video: %v", err)
}
w := NewNotificationWorker(nil, db, rabbitmq.NotificationLikeQueue, &fakeHub{})
w := NewNotificationWorker(nil, nil, db, rabbitmq.NotificationLikeQueue, &fakeHub{})
ctx := context.Background()
if err := w.process(ctx, likeDelivery(t, "intent-0010", 7, 12)); err != nil {
@@ -156,3 +156,28 @@ func TestNotificationWorker_SelfActionWritesNothing(t *testing.T) {
t.Fatalf("notifications 行数 = %d, want 0", count)
}
}
// 重试队列回源时会把 routing key 改写成队列名(`like.like` → `notification.like`)。
// 这个用例用改写后的 routing key 复现该场景,断言仍然正常写入:
// 它守护的是"分派依据是队列 + body.action,不是 routing key"。
func TestNotificationWorker_RewrittenRoutingKeyStillWorks(t *testing.T) {
db := newWorkerTestDB(t)
if err := db.Create(&testVideoRow{ID: 13, AuthorID: 2}).Error; err != nil {
t.Fatalf("seed video: %v", err)
}
hub := &fakeHub{}
w := NewNotificationWorker(nil, nil, db, rabbitmq.NotificationLikeQueue, hub)
d := likeDelivery(t, "intent-0011", 1, 13)
d.RoutingKey = rabbitmq.NotificationLikeQueue // 第二次投递时的 routing key
if err := w.process(context.Background(), d); err != nil {
t.Fatalf("process: %v", err)
}
var count int64
db.Model(&Notification{}).Count(&count)
if count != 1 {
t.Fatalf("notifications 行数 = %d, want 1(routing key 被改写不应影响分派)", count)
}
}