2026-05-20 16:34:19 +08:00
|
|
|
|
package worker
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"context"
|
|
|
|
|
|
"encoding/json"
|
|
|
|
|
|
"errors"
|
2026-10-30 15:20:15 +08:00
|
|
|
|
"fmt"
|
2026-05-20 16:34:19 +08:00
|
|
|
|
"log"
|
|
|
|
|
|
"strings"
|
2026-10-30 15:20:15 +08:00
|
|
|
|
|
|
|
|
|
|
"feedsystem_video_go/internal/middleware/rabbitmq"
|
|
|
|
|
|
"feedsystem_video_go/internal/video"
|
2026-05-20 16:34:19 +08:00
|
|
|
|
|
|
|
|
|
|
amqp "github.com/rabbitmq/amqp091-go"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
type CommentWorker struct {
|
|
|
|
|
|
ch *amqp.Channel
|
2026-10-30 15:20:15 +08:00
|
|
|
|
parker rabbitmq.Parker
|
2026-10-30 13:29:08 +08:00
|
|
|
|
comments video.CommentStore
|
2026-05-20 16:34:19 +08:00
|
|
|
|
videos *video.VideoRepository
|
|
|
|
|
|
queue string
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-10-30 15:20:15 +08:00
|
|
|
|
func NewCommentWorker(ch *amqp.Channel, parker rabbitmq.Parker, comments video.CommentStore, videos *video.VideoRepository, queue string) *CommentWorker {
|
|
|
|
|
|
return &CommentWorker{ch: ch, parker: parker, comments: comments, videos: videos, queue: queue}
|
2026-05-20 16:34:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (w *CommentWorker) Run(ctx context.Context) error {
|
|
|
|
|
|
if w == nil || w.ch == nil || w.comments == nil || w.videos == nil {
|
|
|
|
|
|
return errors.New("comment worker is not initialized")
|
|
|
|
|
|
}
|
|
|
|
|
|
if w.queue == "" {
|
|
|
|
|
|
return errors.New("queue is required")
|
|
|
|
|
|
}
|
2026-10-30 15:20:15 +08:00
|
|
|
|
return rabbitmq.ConsumeLoop(ctx, w.ch, w.parker, w.queue, rabbitmq.MaxAttempts, w.process)
|
2026-05-20 16:34:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-10-30 15:20:15 +08:00
|
|
|
|
func (w *CommentWorker) process(ctx context.Context, d amqp.Delivery) error {
|
2026-05-20 16:34:19 +08:00
|
|
|
|
var evt rabbitmq.CommentEvent
|
2026-10-30 15:20:15 +08:00
|
|
|
|
if err := json.Unmarshal(d.Body, &evt); err != nil {
|
|
|
|
|
|
return rabbitmq.Permanent(fmt.Errorf("comment worker: decode event: %w", err))
|
2026-05-20 16:34:19 +08:00
|
|
|
|
}
|
2026-10-30 15:20:15 +08:00
|
|
|
|
|
2026-05-20 16:34:19 +08:00
|
|
|
|
switch evt.Action {
|
|
|
|
|
|
case "publish":
|
|
|
|
|
|
return w.applyPublish(ctx, &evt)
|
|
|
|
|
|
case "delete":
|
|
|
|
|
|
return w.applyDelete(ctx, &evt)
|
|
|
|
|
|
default:
|
2026-10-30 15:20:15 +08:00
|
|
|
|
return rabbitmq.Permanent(fmt.Errorf("comment worker: unknown action %q", evt.Action))
|
2026-05-20 16:34:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (w *CommentWorker) applyPublish(ctx context.Context, evt *rabbitmq.CommentEvent) error {
|
|
|
|
|
|
if evt == nil || evt.VideoID == 0 || evt.AuthorID == 0 || strings.TrimSpace(evt.Content) == "" {
|
2026-10-30 15:20:15 +08:00
|
|
|
|
return rabbitmq.Permanent(fmt.Errorf("comment worker: invalid publish event video_id=%d author_id=%d", evt.VideoID, evt.AuthorID))
|
2026-05-20 16:34:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ok, err := w.videos.IsExist(ctx, evt.VideoID)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
if !ok {
|
2026-10-31 00:03:53 +08:00
|
|
|
|
// 视频已删除:处理完了,不是失败。
|
2026-05-20 16:34:19 +08:00
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
c := &video.Comment{
|
|
|
|
|
|
Username: strings.TrimSpace(evt.Username),
|
|
|
|
|
|
VideoID: evt.VideoID,
|
|
|
|
|
|
AuthorID: evt.AuthorID,
|
|
|
|
|
|
Content: strings.TrimSpace(evt.Content),
|
|
|
|
|
|
}
|
2026-10-31 00:03:53 +08:00
|
|
|
|
// event_id 即意图键:API 已直写时这里会命中唯一索引。
|
2026-10-30 13:29:08 +08:00
|
|
|
|
if evt.EventID != "" {
|
|
|
|
|
|
key := evt.EventID
|
|
|
|
|
|
c.IdempotencyKey = &key
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
created, err := w.comments.CreateIdempotentWithPopularity(ctx, c, 1)
|
|
|
|
|
|
if err != nil {
|
2026-05-20 16:34:19 +08:00
|
|
|
|
return err
|
|
|
|
|
|
}
|
2026-10-30 13:29:08 +08:00
|
|
|
|
if !created {
|
|
|
|
|
|
log.Printf("comment worker: dedupe=hit event_id=%s video_id=%d author_id=%d",
|
|
|
|
|
|
evt.EventID, evt.VideoID, evt.AuthorID)
|
|
|
|
|
|
}
|
|
|
|
|
|
return nil
|
2026-05-20 16:34:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (w *CommentWorker) applyDelete(ctx context.Context, evt *rabbitmq.CommentEvent) error {
|
|
|
|
|
|
if evt == nil || evt.CommentID == 0 {
|
2026-10-30 15:20:15 +08:00
|
|
|
|
return rabbitmq.Permanent(errors.New("comment worker: invalid delete event: comment_id is required"))
|
2026-05-20 16:34:19 +08:00
|
|
|
|
}
|
2026-10-30 13:29:08 +08:00
|
|
|
|
deleted, err := w.comments.DeleteByID(ctx, evt.CommentID)
|
2026-05-20 16:34:19 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
2026-10-30 13:29:08 +08:00
|
|
|
|
if !deleted {
|
|
|
|
|
|
// 重复投递的删除是空操作,不是错误。
|
|
|
|
|
|
log.Printf("comment worker: comment already deleted id=%d event_id=%s", evt.CommentID, evt.EventID)
|
2026-05-20 16:34:19 +08:00
|
|
|
|
}
|
2026-10-30 13:29:08 +08:00
|
|
|
|
return nil
|
2026-05-20 16:34:19 +08:00
|
|
|
|
}
|