2026-04-26 15:49:16 +08:00
|
|
|
|
package worker
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"context"
|
|
|
|
|
|
"encoding/json"
|
2026-10-14 14:03:08 +08:00
|
|
|
|
"errors"
|
2026-04-26 15:49:16 +08:00
|
|
|
|
"feedsystem_video_go/internal/middleware/rabbitmq"
|
2026-05-22 23:57:06 +08:00
|
|
|
|
rediscache "feedsystem_video_go/internal/middleware/redis"
|
2026-04-26 15:49:16 +08:00
|
|
|
|
"feedsystem_video_go/internal/video"
|
|
|
|
|
|
"fmt"
|
|
|
|
|
|
"log"
|
|
|
|
|
|
"time"
|
|
|
|
|
|
|
2026-10-14 14:03:08 +08:00
|
|
|
|
amqp "github.com/rabbitmq/amqp091-go"
|
2026-04-26 15:49:16 +08:00
|
|
|
|
oredis "github.com/redis/go-redis/v9"
|
|
|
|
|
|
"gorm.io/gorm"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2026-11-08 15:47:10 +08:00
|
|
|
|
// 断连期间 Publish 会阻塞等待重连,超时让它失败并进入下一轮轮询。
|
2026-10-14 14:03:08 +08:00
|
|
|
|
const outboxPublishTimeout = 5 * time.Second
|
|
|
|
|
|
|
2026-11-08 14:44:34 +08:00
|
|
|
|
const (
|
2026-11-08 15:47:10 +08:00
|
|
|
|
// 空批次后的等待时间。
|
2026-11-08 14:44:34 +08:00
|
|
|
|
outboxPollInterval = time.Second
|
2026-11-08 15:47:10 +08:00
|
|
|
|
outboxBatchSize = 100
|
2026-11-08 14:44:34 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
2026-11-08 15:47:10 +08:00
|
|
|
|
// StartOutboxPoller 阻塞轮询 outbox 表;调用方负责起 goroutine。
|
2026-11-08 14:44:34 +08:00
|
|
|
|
func StartOutboxPoller(ctx context.Context, db *gorm.DB, tmq *rabbitmq.TimelineMQ) {
|
2026-05-22 23:57:06 +08:00
|
|
|
|
if db == nil || tmq == nil {
|
2026-04-26 15:49:16 +08:00
|
|
|
|
log.Printf("Outbox poller disabled: timeline mq is not initialized")
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-11-08 14:44:34 +08:00
|
|
|
|
for ctx.Err() == nil {
|
|
|
|
|
|
var messages []video.OutboxMsg
|
2026-04-26 15:49:16 +08:00
|
|
|
|
|
2026-11-08 14:44:34 +08:00
|
|
|
|
err := db.Where("status = ?", "pending").Order("create_time ASC").Limit(outboxBatchSize).Find(&messages).Error
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
log.Printf("outbox 轮询失败: %v", err)
|
|
|
|
|
|
if !waitOrDone(ctx, outboxPollInterval) {
|
|
|
|
|
|
return
|
2026-10-28 21:54:35 +08:00
|
|
|
|
}
|
2026-11-08 14:44:34 +08:00
|
|
|
|
continue
|
|
|
|
|
|
}
|
|
|
|
|
|
if len(messages) == 0 {
|
|
|
|
|
|
if !waitOrDone(ctx, outboxPollInterval) {
|
|
|
|
|
|
return
|
2026-04-26 15:49:16 +08:00
|
|
|
|
}
|
2026-11-08 14:44:34 +08:00
|
|
|
|
continue
|
|
|
|
|
|
}
|
2026-04-26 15:49:16 +08:00
|
|
|
|
|
2026-11-08 14:44:34 +08:00
|
|
|
|
for _, msg := range messages {
|
|
|
|
|
|
// 超时派生自 poller 的 ctx:进程退出时不再为一条消息多等 5 秒。
|
|
|
|
|
|
pubCtx, cancel := context.WithTimeout(ctx, outboxPublishTimeout)
|
|
|
|
|
|
err := tmq.PublishVideo(pubCtx, msg.VideoID, msg.CreateTime)
|
|
|
|
|
|
cancel()
|
2026-04-26 15:49:16 +08:00
|
|
|
|
|
2026-11-08 14:44:34 +08:00
|
|
|
|
if err == nil {
|
|
|
|
|
|
if err := db.Delete(&msg).Error; err != nil {
|
|
|
|
|
|
log.Printf("删除 outbox 消息失败: id=%d, err=%v", msg.ID, err)
|
2026-04-26 15:49:16 +08:00
|
|
|
|
}
|
2026-11-08 14:44:34 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
log.Printf("投递MQ失败: VideoID: %d, err: %v", msg.VideoID, err)
|
2026-04-26 15:49:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-11-08 14:44:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-11-08 15:47:10 +08:00
|
|
|
|
// waitOrDone 等待 d 到点;ctx 取消则提前返回 false。
|
2026-11-08 14:44:34 +08:00
|
|
|
|
func waitOrDone(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
|
|
|
|
|
|
}
|
2026-04-26 15:49:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-11-08 15:47:10 +08:00
|
|
|
|
// StartConsumer 阻塞消费 timeline 事件;调用方负责起 goroutine。
|
2026-10-14 14:03:08 +08:00
|
|
|
|
func StartConsumer(ctx context.Context, rmq *rabbitmq.RabbitMQ, queueName string, redisClient *rediscache.Client) {
|
|
|
|
|
|
if rmq == nil {
|
2026-05-22 23:57:06 +08:00
|
|
|
|
log.Printf("Timeline consumer disabled: rabbitmq is not initialized")
|
2026-04-26 15:49:16 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
if redisClient == nil {
|
|
|
|
|
|
log.Printf("Timeline consumer disabled: redis is not initialized")
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-11-08 14:44:34 +08:00
|
|
|
|
rabbitmq.RunConsumer(ctx, rmq, "TimelineConsumer", 10, func(ch *amqp.Channel) error {
|
2026-10-14 14:03:08 +08:00
|
|
|
|
msgs, err := ch.Consume(queueName, "", false, false, false, false, nil)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
2026-05-22 23:57:06 +08:00
|
|
|
|
for {
|
2026-10-14 14:03:08 +08:00
|
|
|
|
select {
|
|
|
|
|
|
case <-ctx.Done():
|
|
|
|
|
|
return ctx.Err()
|
|
|
|
|
|
case msg, ok := <-msgs:
|
|
|
|
|
|
if !ok {
|
|
|
|
|
|
return errors.New("deliveries channel closed")
|
2026-05-22 23:57:06 +08:00
|
|
|
|
}
|
2026-10-14 14:03:08 +08:00
|
|
|
|
handleTimelineMessage(ctx, redisClient, msg)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
2026-05-22 23:57:06 +08:00
|
|
|
|
|
2026-10-14 14:03:08 +08:00
|
|
|
|
// handleTimelineMessage 把一条 timeline 事件写入 Redis ZSet(幂等:member 为 VideoID)。
|
|
|
|
|
|
func handleTimelineMessage(ctx context.Context, redisClient *rediscache.Client, msg amqp.Delivery) {
|
|
|
|
|
|
var event rabbitmq.TimelineEvent
|
|
|
|
|
|
if err := json.Unmarshal(msg.Body, &event); err != nil {
|
|
|
|
|
|
log.Printf("Timeline consumer: 反序列化失败: %v", err)
|
|
|
|
|
|
_ = msg.Ack(false)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2026-05-22 23:57:06 +08:00
|
|
|
|
|
2026-10-14 14:03:08 +08:00
|
|
|
|
opCtx, cancel := context.WithTimeout(ctx, 500*time.Millisecond)
|
|
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
|
|
|
|
timelineKey := redisClient.Key("feed:global_timeline")
|
|
|
|
|
|
err := redisClient.ZAdd(opCtx, timelineKey, oredis.Z{
|
|
|
|
|
|
Score: float64(event.CreateTime),
|
|
|
|
|
|
Member: fmt.Sprintf("%d", event.VideoID),
|
|
|
|
|
|
})
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
log.Printf("Timeline consumer: 写入Zset失败: %v", err)
|
|
|
|
|
|
_ = msg.Nack(false, true)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2026-05-22 23:57:06 +08:00
|
|
|
|
|
2026-10-14 14:03:08 +08:00
|
|
|
|
if err := redisClient.ZRemRangeByRank(opCtx, timelineKey, 0, -1001); err != nil {
|
|
|
|
|
|
log.Printf("Timeline consumer: ZRem失败: %v", err)
|
|
|
|
|
|
}
|
2026-04-26 15:49:16 +08:00
|
|
|
|
|
2026-10-14 14:03:08 +08:00
|
|
|
|
_ = msg.Ack(false)
|
2026-04-26 15:49:16 +08:00
|
|
|
|
}
|