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:
@@ -14,6 +14,7 @@ import (
|
||||
"github.com/LoveLosita/smartflow/backend/pkg"
|
||||
"github.com/LoveLosita/smartflow/backend/routers"
|
||||
"github.com/LoveLosita/smartflow/backend/service"
|
||||
eventsvc "github.com/LoveLosita/smartflow/backend/service/events"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
@@ -61,21 +62,28 @@ func Start() {
|
||||
agentRepo := dao.NewAgentDAO(db)
|
||||
outboxRepo := outboxinfra.NewRepository(db)
|
||||
|
||||
// outbox 异步链路接线:
|
||||
// - 读取 Kafka 配置
|
||||
// - 创建基础设施级 outbox 异步引擎
|
||||
// - 引擎内部负责 dispatch/consume 两个后台循环
|
||||
// outbox 通用事件总线接线(第二阶段):
|
||||
// 1. 读取 Kafka 配置;
|
||||
// 2. 创建 infra 级 EventBus;
|
||||
// 3. 显式注册“聊天持久化”事件处理器;
|
||||
// 4. 启动总线后台 dispatch/consume 循环。
|
||||
kafkaCfg := kafkabus.LoadConfig()
|
||||
asyncPipeline, err := outboxinfra.NewChatHistoryAsync(outboxRepo, kafkaCfg)
|
||||
eventBus, err := outboxinfra.NewEventBus(outboxRepo, kafkaCfg)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to initialize Kafka async pipeline: %v", err)
|
||||
log.Fatalf("Failed to initialize outbox event bus: %v", err)
|
||||
}
|
||||
if asyncPipeline != nil {
|
||||
asyncPipeline.Start(context.Background())
|
||||
defer asyncPipeline.Close()
|
||||
log.Println("Kafka async pipeline started")
|
||||
if eventBus != nil {
|
||||
// 3. 在启动前完成“业务事件处理器”注册。
|
||||
// 3.1 这里显式调用 service/events,保证 infra 层不承载业务语义。
|
||||
// 3.2 若注册失败直接中止启动,避免“消息已入队但无人消费”的隐性故障。
|
||||
if err = eventsvc.RegisterChatHistoryPersistHandler(eventBus, outboxRepo, manager); err != nil {
|
||||
log.Fatalf("Failed to register chat history event handler: %v", err)
|
||||
}
|
||||
eventBus.Start(context.Background())
|
||||
defer eventBus.Close()
|
||||
log.Println("Outbox event bus started")
|
||||
} else {
|
||||
log.Println("Kafka async pipeline is disabled")
|
||||
log.Println("Outbox event bus is disabled")
|
||||
}
|
||||
|
||||
// Service 层初始化。
|
||||
@@ -84,7 +92,7 @@ func Start() {
|
||||
courseService := service.NewCourseService(courseRepo, scheduleRepo)
|
||||
taskClassService := service.NewTaskClassService(taskClassRepo, cacheRepo, scheduleRepo, manager)
|
||||
scheduleService := service.NewScheduleService(scheduleRepo, userRepo, taskClassRepo, manager, cacheRepo)
|
||||
agentService := service.NewAgentService(aiHub, agentRepo, taskRepo, agentCacheRepo, asyncPipeline)
|
||||
agentService := service.NewAgentService(aiHub, agentRepo, taskRepo, agentCacheRepo, eventBus)
|
||||
|
||||
// API 层初始化。
|
||||
userApi := api.NewUserHandler(userService)
|
||||
|
||||
Reference in New Issue
Block a user