后端: 1. 服务级 outbox 基础设施全量落地——新增 service route / service catalog / route registry,重构 outbox engine、repository、event bus 和 model,按 `event_type -> service -> table/topic/group` 统一写入与投递,保留 `agent` 兼容壳但不再依赖共享 outbox 2. Kafka 投递、消费与启动装配同步切换——更新 kafka config、consumer、envelope,接入服务级 topic 与 consumer group,并同步调整 mysql 初始化、start/main/router 装配,保证各服务 relay / consumer 独立装配 3. 业务事件处理器按服务归属重接新 bus——`active-scheduler` 触发链路,以及 `agent` / `memory` / `notification` / `task` 相关 outbox handler 统一切到新路由注册与服务目录,避免新流量回流共享表 4. 同步更新《微服务四步迁移与第二阶段并行开发计划》,把阶段 1 改成当前基线并补齐结构图、阶段快照、风险回退和多代理执行口径
75 lines
3.0 KiB
Go
75 lines
3.0 KiB
Go
package events
|
||
|
||
import (
|
||
"fmt"
|
||
"strings"
|
||
|
||
outboxinfra "github.com/LoveLosita/smartflow/backend/infra/outbox"
|
||
)
|
||
|
||
// outboxHandlerService 表示 outbox 路由归属的业务服务。
|
||
//
|
||
// 这里只记录服务归属,不承载具体实现包名,方便在启动日志和路由表里直接阅读。
|
||
type outboxHandlerService string
|
||
|
||
const (
|
||
outboxHandlerServiceAgent outboxHandlerService = "agent"
|
||
outboxHandlerServiceTask outboxHandlerService = "task"
|
||
outboxHandlerServiceMemory outboxHandlerService = "memory"
|
||
outboxHandlerServiceActiveScheduler outboxHandlerService = "active-scheduler"
|
||
outboxHandlerServiceNotification outboxHandlerService = "notification"
|
||
)
|
||
|
||
// outboxHandlerRoute 显式描述“事件类型 -> 服务归属 -> handler 注册动作”。
|
||
//
|
||
// 1. EventType 负责唯一定位 outbox 路由键;
|
||
// 2. Service 负责标明该路由归属的业务服务;
|
||
// 3. Register 只负责把对应 handler 挂到总线,不承载业务逻辑。
|
||
type outboxHandlerRoute struct {
|
||
EventType string
|
||
Service outboxHandlerService
|
||
Register func() error
|
||
}
|
||
|
||
// registerOutboxHandlerRoutes 逐条注册路由表里的 handler。
|
||
//
|
||
// 1. 先把事件类型和服务归属写进路由表,避免启动入口散落多处 if err != nil;
|
||
// 2. 再统一执行注册动作,保证失败时能直接定位到具体 event_type 和 service;
|
||
// 3. 若某条路由缺少注册函数,直接返回 error,避免静默漏注册。
|
||
func registerOutboxHandlerRoutes(routes []outboxHandlerRoute) error {
|
||
for _, route := range routes {
|
||
if route.Register == nil {
|
||
return fmt.Errorf("outbox handler route 缺少注册函数: event_type=%s service=%s", route.EventType, route.Service)
|
||
}
|
||
if err := outboxinfra.RegisterEventService(route.EventType, string(route.Service)); err != nil {
|
||
return fmt.Errorf("登记 outbox 事件归属失败(event_type=%s, service=%s): %w", route.EventType, route.Service, err)
|
||
}
|
||
if err := route.Register(); err != nil {
|
||
return fmt.Errorf("注册 outbox handler 失败(event_type=%s, service=%s): %w", route.EventType, route.Service, err)
|
||
}
|
||
}
|
||
return nil
|
||
}
|
||
|
||
// scopedOutboxRepoForEvent 负责把通用 outbox 仓库收敛到某个事件所属的服务表。//
|
||
// 职责边界:
|
||
// 1. 只做事件->服务->表的路由,不碰业务写入语义;
|
||
// 2. 返回的仓库只适合当前事件的 MarkDead / ConsumeAndMarkConsumed / MarkFailedForRetry;
|
||
// 3. 路由缺失时直接返回错误,避免默默写回默认表。
|
||
func scopedOutboxRepoForEvent(outboxRepo *outboxinfra.Repository, eventType string) (*outboxinfra.Repository, error) {
|
||
if outboxRepo == nil {
|
||
return nil, fmt.Errorf("outbox repository is nil")
|
||
}
|
||
|
||
eventType = strings.TrimSpace(eventType)
|
||
if eventType == "" {
|
||
return nil, fmt.Errorf("eventType is empty")
|
||
}
|
||
|
||
route, ok := outboxinfra.ResolveEventRoute(eventType)
|
||
if !ok {
|
||
return nil, fmt.Errorf("outbox route not registered: eventType=%s", eventType)
|
||
}
|
||
return outboxRepo.WithRoute(route), nil
|
||
}
|