Version: 0.9.68.dev.260504
后端: 1. 阶段 3 notification 服务边界落地,新增 `cmd/notification`、`services/notification`、`gateway/notification`、`shared/contracts/notification` 和 notification port,按 userauth 同款最小手搓 zrpc 样板收口 2. notification outbox consumer、relay 和 retry loop 迁入独立服务入口,处理 `notification.feishu.requested`,gateway 改为通过 zrpc client 调用 notification 3. 清退旧单体 notification DAO/model/service/provider/runner 和 `service/events/notification_feishu.go`,旧实现不再作为活跃编译路径 4. 修复 outbox 路由归属、dispatch 启动扫描、Kafka topic 探测/投递超时、sending 租约恢复、毒消息 MarkDead 错误回传和 RPC timeout 边界 5. 同步调整 active-scheduler 触发通知事件、核心 outbox handler、MySQL 迁移边界和 notification 配置 文档: 1. 更新微服务迁移计划,将阶段 3 notification 标记为已完成,并明确下一阶段从 active-scheduler 开始
This commit is contained in:
@@ -6,7 +6,6 @@ import (
|
||||
"github.com/LoveLosita/smartflow/backend/dao"
|
||||
outboxinfra "github.com/LoveLosita/smartflow/backend/infra/outbox"
|
||||
"github.com/LoveLosita/smartflow/backend/memory"
|
||||
"github.com/LoveLosita/smartflow/backend/notification"
|
||||
sharedevents "github.com/LoveLosita/smartflow/backend/shared/events"
|
||||
"github.com/LoveLosita/smartflow/backend/shared/ports"
|
||||
)
|
||||
@@ -37,9 +36,10 @@ func RegisterCoreOutboxHandlers(
|
||||
// RegisterAllOutboxHandlers 注册当前阶段所有 outbox handler。
|
||||
//
|
||||
// 职责边界:
|
||||
// 1. 只负责把 core / active_scheduler / notification 三类路由一次性接线;
|
||||
// 1. 只负责把当前单体残留域的 core / active_scheduler 路由一次性接线;
|
||||
// 2. 不负责创建依赖,也不负责启动事件总线;
|
||||
// 3. 供当前启动流程在“总线启动前”统一完成显式路由注册。
|
||||
// 3. notification 已独立到 cmd/notification,自有 outbox consumer 不再由单体注册;
|
||||
// 4. 供当前启动流程在“总线启动前”统一完成显式路由注册。
|
||||
func RegisterAllOutboxHandlers(
|
||||
eventBus OutboxBus,
|
||||
outboxRepo *outboxinfra.Repository,
|
||||
@@ -48,10 +48,9 @@ func RegisterAllOutboxHandlers(
|
||||
cacheRepo *dao.CacheDAO,
|
||||
memoryModule *memory.Module,
|
||||
activeTriggerWorkflow ActiveScheduleTriggeredProcessor,
|
||||
notificationService *notification.NotificationService,
|
||||
adjuster ports.TokenUsageAdjuster,
|
||||
) error {
|
||||
if err := validateAllOutboxHandlerDeps(eventBus, outboxRepo, repoManager, agentRepo, cacheRepo, memoryModule, activeTriggerWorkflow, notificationService); err != nil {
|
||||
if err := validateAllOutboxHandlerDeps(eventBus, outboxRepo, repoManager, agentRepo, cacheRepo, memoryModule, activeTriggerWorkflow); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -63,7 +62,6 @@ func RegisterAllOutboxHandlers(
|
||||
cacheRepo,
|
||||
memoryModule,
|
||||
activeTriggerWorkflow,
|
||||
notificationService,
|
||||
adjuster,
|
||||
))
|
||||
}
|
||||
@@ -102,7 +100,7 @@ func validateCoreOutboxHandlerDeps(
|
||||
return nil
|
||||
}
|
||||
|
||||
// validateAllOutboxHandlerDeps 在核心依赖基础上,额外校验 active_scheduler 和 notification 相关依赖。
|
||||
// validateAllOutboxHandlerDeps 在核心依赖基础上,额外校验 active_scheduler 相关依赖。
|
||||
func validateAllOutboxHandlerDeps(
|
||||
eventBus OutboxBus,
|
||||
outboxRepo *outboxinfra.Repository,
|
||||
@@ -111,7 +109,6 @@ func validateAllOutboxHandlerDeps(
|
||||
cacheRepo *dao.CacheDAO,
|
||||
memoryModule *memory.Module,
|
||||
activeTriggerWorkflow ActiveScheduleTriggeredProcessor,
|
||||
notificationService *notification.NotificationService,
|
||||
) error {
|
||||
if err := validateCoreOutboxHandlerDeps(eventBus, outboxRepo, repoManager, agentRepo, cacheRepo, memoryModule); err != nil {
|
||||
return err
|
||||
@@ -119,9 +116,6 @@ func validateAllOutboxHandlerDeps(
|
||||
if activeTriggerWorkflow == nil {
|
||||
return errors.New("active schedule triggered processor is nil")
|
||||
}
|
||||
if notificationService == nil {
|
||||
return errors.New("notification service is nil")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -190,7 +184,6 @@ func allOutboxHandlerRoutes(
|
||||
cacheRepo *dao.CacheDAO,
|
||||
memoryModule *memory.Module,
|
||||
activeTriggerWorkflow ActiveScheduleTriggeredProcessor,
|
||||
notificationService *notification.NotificationService,
|
||||
adjuster ports.TokenUsageAdjuster,
|
||||
) []outboxHandlerRoute {
|
||||
routes := coreOutboxHandlerRoutes(eventBus, outboxRepo, repoManager, agentRepo, cacheRepo, memoryModule, adjuster)
|
||||
@@ -202,13 +195,6 @@ func allOutboxHandlerRoutes(
|
||||
return RegisterActiveScheduleTriggeredHandler(eventBus, outboxRepo, activeTriggerWorkflow)
|
||||
},
|
||||
},
|
||||
outboxHandlerRoute{
|
||||
EventType: sharedevents.NotificationFeishuRequestedEventType,
|
||||
Service: outboxHandlerServiceNotification,
|
||||
Register: func() error {
|
||||
return RegisterFeishuNotificationHandler(eventBus, outboxRepo, notificationService)
|
||||
},
|
||||
},
|
||||
)
|
||||
return routes
|
||||
}
|
||||
|
||||
@@ -1,109 +0,0 @@
|
||||
package events
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"log"
|
||||
"strings"
|
||||
|
||||
kafkabus "github.com/LoveLosita/smartflow/backend/infra/kafka"
|
||||
outboxinfra "github.com/LoveLosita/smartflow/backend/infra/outbox"
|
||||
"github.com/LoveLosita/smartflow/backend/notification"
|
||||
sharedevents "github.com/LoveLosita/smartflow/backend/shared/events"
|
||||
)
|
||||
|
||||
// RegisterFeishuNotificationHandler 注册 `notification.feishu.requested` 消费 handler。
|
||||
//
|
||||
// 职责边界:
|
||||
// 1. 只负责事件解析、协议校验、调用 NotificationService 和推进 outbox consumed;
|
||||
// 2. 不承担 notification_records 状态机细节,状态流转全部下沉到 notification 模块;
|
||||
// 3. 不在 handler 内部创建 provider/service,避免事件消费与 retry loop 使用两套不同配置。
|
||||
func RegisterFeishuNotificationHandler(
|
||||
bus OutboxBus,
|
||||
outboxRepo *outboxinfra.Repository,
|
||||
svc *notification.NotificationService,
|
||||
) error {
|
||||
if bus == nil {
|
||||
return errors.New("event bus is nil")
|
||||
}
|
||||
if outboxRepo == nil {
|
||||
return errors.New("outbox repository is nil")
|
||||
}
|
||||
if svc == nil {
|
||||
return errors.New("notification service is nil")
|
||||
}
|
||||
eventOutboxRepo, err := scopedOutboxRepoForEvent(outboxRepo, sharedevents.NotificationFeishuRequestedEventType)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
handler := func(ctx context.Context, envelope kafkabus.Envelope) error {
|
||||
// 1. 先校验 event_version,避免未来协议破坏性升级后旧 handler 误吃新消息。
|
||||
// 2. 当前阶段只接受 v1;版本不匹配属于不可恢复协议错误,直接标记 dead。
|
||||
eventVersion := strings.TrimSpace(envelope.EventVersion)
|
||||
if eventVersion != "" && eventVersion != sharedevents.NotificationFeishuRequestedEventVersion {
|
||||
_ = eventOutboxRepo.MarkDead(ctx, envelope.OutboxID, "notification.feishu.requested event_version 不匹配: "+eventVersion)
|
||||
return nil
|
||||
}
|
||||
|
||||
var payload sharedevents.FeishuNotificationRequestedPayload
|
||||
if unmarshalErr := json.Unmarshal(envelope.Payload, &payload); unmarshalErr != nil {
|
||||
_ = eventOutboxRepo.MarkDead(ctx, envelope.OutboxID, "解析 notification.feishu.requested 载荷失败: "+unmarshalErr.Error())
|
||||
return nil
|
||||
}
|
||||
if validateErr := payload.Validate(); validateErr != nil {
|
||||
_ = eventOutboxRepo.MarkDead(ctx, envelope.OutboxID, "notification.feishu.requested 载荷非法: "+validateErr.Error())
|
||||
return nil
|
||||
}
|
||||
|
||||
result, handleErr := svc.HandleFeishuRequested(ctx, payload)
|
||||
if handleErr != nil {
|
||||
return handleErr
|
||||
}
|
||||
|
||||
if consumeErr := eventOutboxRepo.ConsumeAndMarkConsumed(ctx, envelope.OutboxID, nil); consumeErr != nil {
|
||||
return consumeErr
|
||||
}
|
||||
|
||||
log.Printf(
|
||||
"notification.feishu.requested 消费完成: outbox_id=%d notification_id=%d status=%s delivered=%t reused=%t attempt_count=%d",
|
||||
envelope.OutboxID,
|
||||
result.RecordID,
|
||||
result.Status,
|
||||
result.Delivered,
|
||||
result.Reused,
|
||||
result.AttemptCount,
|
||||
)
|
||||
return nil
|
||||
}
|
||||
|
||||
return bus.RegisterEventHandler(sharedevents.NotificationFeishuRequestedEventType, handler)
|
||||
}
|
||||
|
||||
// PublishFeishuNotificationRequested 发布 `notification.feishu.requested` 事件。
|
||||
//
|
||||
// 职责边界:
|
||||
// 1. 只负责把 shared/events payload 投递到 outbox;
|
||||
// 2. 不等待 provider 结果,也不提前创建 notification_records;
|
||||
// 3. 供主动调度 preview 阶段后续切入通知时直接复用。
|
||||
func PublishFeishuNotificationRequested(
|
||||
ctx context.Context,
|
||||
publisher outboxinfra.EventPublisher,
|
||||
payload sharedevents.FeishuNotificationRequestedPayload,
|
||||
) error {
|
||||
if publisher == nil {
|
||||
return errors.New("event publisher is nil")
|
||||
}
|
||||
if err := payload.Validate(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return publisher.Publish(ctx, outboxinfra.PublishRequest{
|
||||
EventType: sharedevents.NotificationFeishuRequestedEventType,
|
||||
EventVersion: sharedevents.NotificationFeishuRequestedEventVersion,
|
||||
MessageKey: payload.MessageKey(),
|
||||
AggregateID: payload.AggregateID(),
|
||||
Payload: payload,
|
||||
})
|
||||
}
|
||||
@@ -170,7 +170,6 @@ func OutboxServiceNames() []string {
|
||||
string(outboxHandlerServiceTask),
|
||||
string(outboxHandlerServiceMemory),
|
||||
string(outboxHandlerServiceActiveScheduler),
|
||||
string(outboxHandlerServiceNotification),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user