2025-11-14 08:42:26 +08:00
|
|
|
package app
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"caatsm/internal/adapter"
|
|
|
|
|
"caatsm/internal/adapter/parser"
|
2025-11-14 21:42:04 +08:00
|
|
|
"context"
|
2025-11-14 08:42:26 +08:00
|
|
|
"fmt"
|
2025-11-14 23:18:10 +08:00
|
|
|
"strings"
|
|
|
|
|
"time"
|
2025-11-14 22:43:04 +08:00
|
|
|
|
2025-11-14 08:42:26 +08:00
|
|
|
"go.uber.org/zap"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// MessageProcessor handles message processing
|
|
|
|
|
type MessageProcessor struct {
|
|
|
|
|
parser parser.Parser
|
|
|
|
|
repository adapter.Repository
|
|
|
|
|
publisher adapter.Publisher
|
|
|
|
|
logger *zap.Logger
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NewMessageProcessor creates a new message processor
|
|
|
|
|
func NewMessageProcessor(
|
|
|
|
|
parser parser.Parser,
|
|
|
|
|
repository adapter.Repository,
|
|
|
|
|
publisher adapter.Publisher,
|
|
|
|
|
logger *zap.Logger,
|
|
|
|
|
) *MessageProcessor {
|
|
|
|
|
return &MessageProcessor{
|
|
|
|
|
parser: parser,
|
|
|
|
|
repository: repository,
|
|
|
|
|
publisher: publisher,
|
|
|
|
|
logger: logger,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Handle processes a message
|
|
|
|
|
func (p *MessageProcessor) Handle(ctx context.Context, raw []byte, msgID string) error {
|
2025-11-14 22:43:04 +08:00
|
|
|
if len(raw) == 0 {
|
2025-11-14 21:42:04 +08:00
|
|
|
return Permanent(fmt.Errorf("empty message"))
|
2025-11-14 08:42:26 +08:00
|
|
|
}
|
|
|
|
|
|
2025-11-14 23:18:10 +08:00
|
|
|
receivedAt := time.Now()
|
|
|
|
|
|
2025-11-14 08:42:26 +08:00
|
|
|
parsed := p.parser.Parse(string(raw))
|
|
|
|
|
if parsed == nil {
|
2025-11-14 21:42:04 +08:00
|
|
|
return Permanent(fmt.Errorf("parser returned nil"))
|
2025-11-14 08:42:26 +08:00
|
|
|
}
|
|
|
|
|
|
2025-11-14 23:18:10 +08:00
|
|
|
if msgID != "" {
|
|
|
|
|
if parsed.Comments == "" {
|
|
|
|
|
parsed.Comments = fmt.Sprintf("nats_msg_id=%s", msgID)
|
|
|
|
|
} else if !strings.Contains(parsed.Comments, "nats_msg_id=") {
|
|
|
|
|
parsed.Comments = fmt.Sprintf("%s; nats_msg_id=%s", parsed.Comments, msgID)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if parsed.ReceivedAt.IsZero() {
|
|
|
|
|
parsed.ReceivedAt = receivedAt
|
|
|
|
|
}
|
|
|
|
|
if parsed.ParsedAt.IsZero() {
|
|
|
|
|
parsed.ParsedAt = time.Now()
|
|
|
|
|
}
|
2025-11-14 08:42:26 +08:00
|
|
|
|
|
|
|
|
// Log parsing result
|
|
|
|
|
if !parsed.Parsed {
|
2025-11-14 23:18:10 +08:00
|
|
|
p.logger.Warn("Message not parsed",
|
2025-11-14 08:42:26 +08:00
|
|
|
zap.String("msg_id", msgID),
|
2025-11-14 23:18:10 +08:00
|
|
|
zap.String("message_id", parsed.MessageID),
|
|
|
|
|
zap.String("category", parsed.Category),
|
|
|
|
|
zap.String("content_preview", truncateContent(parsed.Content, 256)),
|
2025-11-14 08:42:26 +08:00
|
|
|
)
|
|
|
|
|
} else {
|
|
|
|
|
p.logger.Info("Message parsed successfully",
|
|
|
|
|
zap.String("msg_id", msgID),
|
|
|
|
|
zap.String("message_id", parsed.MessageID),
|
|
|
|
|
zap.String("category", parsed.Category),
|
2025-11-14 23:18:10 +08:00
|
|
|
zap.Time("received_at", parsed.ReceivedAt),
|
|
|
|
|
zap.Time("parsed_at", parsed.ParsedAt),
|
2025-11-14 08:42:26 +08:00
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Insert into database
|
|
|
|
|
if err := p.repository.InsertOne(ctx, parsed); err != nil {
|
|
|
|
|
return fmt.Errorf("failed to insert message: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Publish parsed message
|
|
|
|
|
if err := p.publisher.Publish(parsed); err != nil {
|
|
|
|
|
// Log error but don't fail the entire operation
|
|
|
|
|
p.logger.Error("Failed to publish message",
|
|
|
|
|
zap.String("msg_id", msgID),
|
|
|
|
|
zap.Error(err),
|
|
|
|
|
)
|
2025-11-14 22:43:04 +08:00
|
|
|
// Mark as permanent so the consumer will ack instead of retrying
|
|
|
|
|
return Permanent(fmt.Errorf("failed to publish message: %w", err))
|
2025-11-14 08:42:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2025-11-14 22:43:04 +08:00
|
|
|
|
|
|
|
|
func truncateContent(content string, limit int) string {
|
|
|
|
|
if limit <= 0 || len(content) <= limit {
|
|
|
|
|
return content
|
|
|
|
|
}
|
|
|
|
|
if limit <= 3 {
|
|
|
|
|
return content[:limit]
|
|
|
|
|
}
|
|
|
|
|
return content[:limit-3] + "..."
|
|
|
|
|
}
|