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-10-14 14:03:08 +08:00
|
|
|
|
// outboxPublishTimeout 限制单条 outbox 消息的发布时间。
|
|
|
|
|
|
// 连接管理器在断连期间会阻塞等待重连,这里加超时可以保持"失败即进入下一轮轮询"
|
|
|
|
|
|
// 的既有语义,而不是把整个轮询器挂在一条消息上。
|
|
|
|
|
|
const outboxPublishTimeout = 5 * time.Second
|
|
|
|
|
|
|
2026-07-15 11:54:01 +08:00
|
|
|
|
// 轮询器,轮询数据库中的 outbox 表,获取待投递的消息,投递到 MQ 中
|
2026-04-26 15:49:16 +08:00
|
|
|
|
func StartOutboxPoller(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
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
|
|
for {
|
|
|
|
|
|
var messages []video.OutboxMsg
|
|
|
|
|
|
|
|
|
|
|
|
err := db.Where("status = ?", "pending").Order("create_time ASC").Limit(100).Find(&messages).Error
|
|
|
|
|
|
|
2026-10-28 21:54:35 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
log.Printf("outbox 轮询失败: %v", err)
|
|
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
|
|
continue
|
|
|
|
|
|
}
|
|
|
|
|
|
if len(messages) == 0 {
|
2026-04-26 15:49:16 +08:00
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
|
|
continue
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
for _, msg := range messages {
|
2026-10-14 14:03:08 +08:00
|
|
|
|
pubCtx, cancel := context.WithTimeout(context.Background(), outboxPublishTimeout)
|
|
|
|
|
|
err := tmq.PublishVideo(pubCtx, msg.VideoID, msg.CreateTime)
|
|
|
|
|
|
cancel()
|
2026-04-26 15:49:16 +08:00
|
|
|
|
|
|
|
|
|
|
if err == nil {
|
2026-05-20 16:34:46 +08:00
|
|
|
|
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
|
|
|
|
} else {
|
|
|
|
|
|
log.Printf("投递MQ失败: VideoID: %d, err: %v", msg.VideoID, err)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-10-14 14:03:08 +08:00
|
|
|
|
// StartConsumer 消费 timeline 事件并写入 Redis ZSet。
|
|
|
|
|
|
//
|
|
|
|
|
|
// 连接级重连、拓扑重放、channel 重建全部交给连接管理器(rabbitmq.RunConsumer),
|
|
|
|
|
|
// 这里只关心"一条消息应该怎么被处理"。
|
|
|
|
|
|
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-10-14 14:03:08 +08:00
|
|
|
|
go rabbitmq.RunConsumer(ctx, rmq, "TimelineConsumer", 10, func(ch *amqp.Channel) error {
|
|
|
|
|
|
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
|
|
|
|
}
|