119 lines
3.7 KiB
Go
119 lines
3.7 KiB
Go
|
|
package worker
|
|||
|
|
|
|||
|
|
import (
|
|||
|
|
"context"
|
|||
|
|
"encoding/json"
|
|||
|
|
"fmt"
|
|||
|
|
"log"
|
|||
|
|
"time"
|
|||
|
|
|
|||
|
|
"feedsystem_video_go/internal/failedevent"
|
|||
|
|
"feedsystem_video_go/internal/middleware/rabbitmq"
|
|||
|
|
|
|||
|
|
amqp "github.com/rabbitmq/amqp091-go"
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
// failedEventSaver 是这个 worker 对存储的唯一依赖。
|
|||
|
|
// 抽成接口是为了用假实现验证"落库失败必须 requeue"这条契约(无需 MySQL)。
|
|||
|
|
type failedEventSaver interface {
|
|||
|
|
Save(ctx context.Context, e *failedevent.FailedEvent) (bool, error)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// FailedEventWorker 消费停泊队列,把失败消息落库到 failed_events。
|
|||
|
|
type FailedEventWorker struct {
|
|||
|
|
ch *amqp.Channel
|
|||
|
|
repo failedEventSaver
|
|||
|
|
queue string
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// failedEventRetryBackoff 是"落库失败后重投"之前的退避。
|
|||
|
|
// 变量而非常量:单测要把它调到 0 以免为了等 2 秒而拖慢整个套件。
|
|||
|
|
var failedEventRetryBackoff = 2 * time.Second
|
|||
|
|
|
|||
|
|
// NewFailedEventWorker 构造停泊消费者。
|
|||
|
|
func NewFailedEventWorker(ch *amqp.Channel, repo failedEventSaver, queue string) *FailedEventWorker {
|
|||
|
|
return &FailedEventWorker{ch: ch, repo: repo, queue: queue}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// Run 消费停泊队列直到 ctx 取消。
|
|||
|
|
//
|
|||
|
|
// 它自己【不】走 rabbitmq.ConsumeLoop:ConsumeLoop 的失败动作是"停泊",
|
|||
|
|
// 而这个 worker 的职责就是停泊落库——让它停泊自己会形成递归
|
|||
|
|
// (停泊队列消费失败 → 停泊 → 停泊队列 → …)。见方案 §6.9.3。
|
|||
|
|
func (w *FailedEventWorker) Run(ctx context.Context) error {
|
|||
|
|
if w == nil || w.ch == nil {
|
|||
|
|
return fmt.Errorf("failedevent worker: channel is not initialized")
|
|||
|
|
}
|
|||
|
|
deliveries, err := w.ch.Consume(w.queue, "", false, false, false, false, nil)
|
|||
|
|
if err != nil {
|
|||
|
|
return fmt.Errorf("consume %s: %w", w.queue, err)
|
|||
|
|
}
|
|||
|
|
for {
|
|||
|
|
select {
|
|||
|
|
case <-ctx.Done():
|
|||
|
|
return nil
|
|||
|
|
case d, ok := <-deliveries:
|
|||
|
|
if !ok {
|
|||
|
|
return fmt.Errorf("consume %s: delivery channel closed", w.queue)
|
|||
|
|
}
|
|||
|
|
w.persist(ctx, d)
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// persist 是这个 worker 的"最后一道防线":它自己不再停泊。
|
|||
|
|
// 落库失败 → requeue + 退避,不 Ack、不停泊(把消息停泊到一个不存在的消费者没有意义)。
|
|||
|
|
func (w *FailedEventWorker) persist(ctx context.Context, d amqp.Delivery) {
|
|||
|
|
info := rabbitmq.ParseParkedInfo(d)
|
|||
|
|
headers, err := json.Marshal(d.Headers)
|
|||
|
|
if err != nil {
|
|||
|
|
// 序列化失败不该阻止落库:Body 才是重放所需的主体,headers 只是附注。
|
|||
|
|
headers = []byte("{}")
|
|||
|
|
}
|
|||
|
|
record := &failedevent.FailedEvent{
|
|||
|
|
SourceQueue: info.SourceQueue,
|
|||
|
|
EventID: nilIfEmpty(info.EventID),
|
|||
|
|
RoutingKey: d.RoutingKey,
|
|||
|
|
Attempts: info.Attempts,
|
|||
|
|
Body: d.Body,
|
|||
|
|
Headers: string(headers),
|
|||
|
|
LastError: info.LastError,
|
|||
|
|
Status: failedevent.StatusPending,
|
|||
|
|
FirstFailedAt: time.Now(),
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
created, err := w.repo.Save(ctx, record)
|
|||
|
|
if err != nil {
|
|||
|
|
log.Printf("failedevent: 落库失败 queue=%s err=%v(%v 后重投)", info.SourceQueue, err, failedEventRetryBackoff)
|
|||
|
|
_ = d.Nack(false, true)
|
|||
|
|
sleepCtx(ctx, failedEventRetryBackoff)
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
if !created {
|
|||
|
|
log.Printf("failedevent: dedupe=hit source_queue=%s event_id=%s", info.SourceQueue, info.EventID)
|
|||
|
|
}
|
|||
|
|
if err := d.Ack(false); err != nil {
|
|||
|
|
log.Printf("failedevent: ack 失败 queue=%s err=%v", info.SourceQueue, err)
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
func nilIfEmpty(s string) *string {
|
|||
|
|
if s == "" {
|
|||
|
|
return nil
|
|||
|
|
}
|
|||
|
|
return &s
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// sleepCtx 是 worker 包自己的实现:rabbitmq.sleepCtx 未导出,不能跨包复用。
|
|||
|
|
// 返回 false 表示 ctx 已取消(调用方应尽快退出,不要再处理下一条消息)。
|
|||
|
|
func sleepCtx(ctx context.Context, d time.Duration) bool {
|
|||
|
|
t := time.NewTimer(d)
|
|||
|
|
defer t.Stop()
|
|||
|
|
select {
|
|||
|
|
case <-t.C:
|
|||
|
|
return true
|
|||
|
|
case <-ctx.Done():
|
|||
|
|
return false
|
|||
|
|
}
|
|||
|
|
}
|