Version: 0.6.1.dev.260316

♻️ refactor(outbox): 抽离通用事件总线,并完成 event_type-only 收口

-  新增 `infra` 层通用 `EventBus` / `EventContract`,统一事件发布与消费协议
- 🔄 将聊天持久化链路调整为通过 `service/events` 注册 handler 并发布事件,进一步解耦业务逻辑与异步处理流程
- 🧹 移除 `chat_history_async` 旧适配实现,以及基于 `biz_type` 的兼容分发逻辑
- 📝 更新 Outbox 异步持久化决策记录,明确保留方案 A,并正式启用方案 B
- 📚 同步更新 README 中关于 Outbox + Kafka 可靠异步链路的说明
- 🚚 当前 `outbox + kafka` 已与项目业务链路完全解耦,沉淀为通用、可靠性更强的消息队列能力;后续将参考消息队列的典型使用方式,逐步扩展到更多业务场景
-  补充跨不同分类事务管理器中的 `agent dao` 注册与接入支持
This commit is contained in:
Losita
2026-03-16 13:00:26 +08:00
parent 712bcd3605
commit 626fc700d2
17 changed files with 782 additions and 422 deletions

View File

@@ -3,40 +3,36 @@ package model
import "time"
const (
// OutboxStatusPending 表示消息已 outbox等待投递或等待下次重试窗口到达。
// OutboxStatusPending 表示消息已写入 outbox等待投递或重试窗口到达。
OutboxStatusPending = "pending"
// OutboxStatusPublished 表示消息已成功写入 Kafka尚未完成业务消费。
// OutboxStatusPublished 表示消息已成功写入 Kafka但业务消费尚未完成
OutboxStatusPublished = "published"
// OutboxStatusConsumed 表示消息对应业务逻辑已成功执行(本项目中即聊天记录已落库)。
// OutboxStatusConsumed 表示消息对应业务处理已成功完成(最终态)。
OutboxStatusConsumed = "consumed"
// OutboxStatusDead 表示达到最大重试次数或出现不可恢复错误,进入死信终态。
// OutboxStatusDead 表示达到重试上限或出现不可恢复错误(最终态
OutboxStatusDead = "dead"
// OutboxBizTypeChatHistoryPersist 当前唯一业务类型:聊天记录异步持久化。
OutboxBizTypeChatHistoryPersist = "chat_history_persist"
)
// AgentOutboxMessage 是 outbox 模式的核心表结构:
// 1. 先写本地数据库(保证事务内可见);
// 2. 再由后台扫描并投递 Kafka
// 3. 由消费者完成最终业务落库并回写状态。
// AgentOutboxMessage 是 outbox 状态机表模型。
//
// 关键说明:
// 1. EventType 映射到数据库 `biz_type` 列(为兼容历史表结构,不改 DDL
// 2. Payload 保存统一事件外壳 JSON
// 3. Status/RetryCount/NextRetryAt 组成重试状态机。
type AgentOutboxMessage struct {
ID int64 `gorm:"column:id;primaryKey;autoIncrement"`
// BizType 决定消费者侧如何解释 Payload。
BizType string `gorm:"column:biz_type;type:varchar(64);not null;index:idx_outbox_status_next,priority:3;comment:业务类型"`
// Topic/MessageKey 用于 Kafka 路由与分区稳定性。
EventType string `gorm:"column:biz_type;type:varchar(64);not null;index:idx_outbox_status_next,priority:3;comment:事件类型"`
Topic string `gorm:"column:topic;type:varchar(128);not null;comment:Kafka Topic"`
MessageKey string `gorm:"column:message_key;type:varchar(128);not null;comment:Kafka 消息键"`
// Payload 存储业务 JSON消费时再反序列化为具体 payload 结构。
Payload string `gorm:"column:payload;type:longtext;not null;comment:业务载荷(JSON)"`
// Status + NextRetryAt + RetryCount 共同描述“是否可被调度重试”。
Payload string `gorm:"column:payload;type:longtext;not null;comment:业务载荷(JSON)"`
Status string `gorm:"column:status;type:varchar(32);not null;index:idx_outbox_status_next,priority:1;comment:pending/published/consumed/dead"`
RetryCount int `gorm:"column:retry_count;not null;default:0;comment:已重试次数"`
MaxRetry int `gorm:"column:max_retry;not null;default:20;comment:最大重试次数"`
NextRetryAt *time.Time `gorm:"column:next_retry_at;index:idx_outbox_status_next,priority:2;comment:下次重试时间"`
// LastError 记录最近一次失败原因,便于排障和可观测。
LastError *string `gorm:"column:last_error;type:text;comment:最后一次错误"`
// PublishedAt/ConsumedAt 便于统计“投递延迟”和“消费完成耗时”。
LastError *string `gorm:"column:last_error;type:text;comment:最后一次错误"`
PublishedAt *time.Time `gorm:"column:published_at;comment:投递到 Kafka 时间"`
ConsumedAt *time.Time `gorm:"column:consumed_at;comment:消费完成时间"`
CreatedAt *time.Time `gorm:"column:created_at;autoCreateTime"`
@@ -46,12 +42,3 @@ type AgentOutboxMessage struct {
func (AgentOutboxMessage) TableName() string {
return "agent_outbox_messages"
}
// ChatHistoryPersistPayload 是“聊天记录持久化”消息的业务载荷。
// 注意:该载荷既会被写入 outbox也会被封装到 Kafka Envelope 中传输。
type ChatHistoryPersistPayload struct {
UserID int `json:"user_id"`
ConversationID string `json:"conversation_id"`
Role string `json:"role"`
Message string `json:"message"`
}