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:
Losita
2026-05-04 18:40:39 +08:00
parent 9742dc8b1c
commit abe3b4960e
41 changed files with 2178 additions and 889 deletions

View File

@@ -2,12 +2,12 @@ package api
import (
"context"
"errors"
"net/http"
"time"
"github.com/LoveLosita/smartflow/backend/notification"
"github.com/LoveLosita/smartflow/backend/respond"
contracts "github.com/LoveLosita/smartflow/backend/shared/contracts/notification"
"github.com/LoveLosita/smartflow/backend/shared/ports"
"github.com/gin-gonic/gin"
)
@@ -16,15 +16,15 @@ const notificationAPITimeout = 8 * time.Second
// NotificationAPI 承载当前用户的外部通知通道配置接口。
//
// 职责边界:
// 1. 只负责从 JWT 上下文取得当前 user_id、绑定请求体并调用 notification.ChannelService
// 1. 只负责从 JWT 上下文取得当前 user_id、绑定请求体并调用 notification zrpc client
// 2. 不直接读写 user_notification_channels避免 API 层绕过 webhook 校验和脱敏规则;
// 3. 不参与主动调度、notification_records 状态机和 outbox 消费。
type NotificationAPI struct {
channelService *notification.ChannelService
client ports.NotificationCommandClient
}
func NewNotificationAPI(channelService *notification.ChannelService) *NotificationAPI {
return &NotificationAPI{channelService: channelService}
func NewNotificationAPI(client ports.NotificationCommandClient) *NotificationAPI {
return &NotificationAPI{client: client}
}
type saveFeishuWebhookRequest struct {
@@ -36,7 +36,7 @@ type saveFeishuWebhookRequest struct {
// GetFeishuWebhook 查询当前用户的飞书 Webhook 触发器配置。
func (api *NotificationAPI) GetFeishuWebhook(c *gin.Context) {
if api == nil || api.channelService == nil {
if api == nil || api.client == nil {
c.JSON(http.StatusInternalServerError, respond.InternalError(nilServiceError("通知通道 service 未初始化")))
return
}
@@ -44,9 +44,11 @@ func (api *NotificationAPI) GetFeishuWebhook(c *gin.Context) {
ctx, cancel := context.WithTimeout(c.Request.Context(), notificationAPITimeout)
defer cancel()
channel, err := api.channelService.GetFeishuWebhook(ctx, c.GetInt("user_id"))
channel, err := api.client.GetFeishuWebhook(ctx, contracts.GetFeishuWebhookRequest{
UserID: c.GetInt("user_id"),
})
if err != nil {
writeNotificationError(c, err)
respond.DealWithError(c, err)
return
}
c.JSON(http.StatusOK, respond.RespWithData(respond.Ok, channel))
@@ -54,7 +56,7 @@ func (api *NotificationAPI) GetFeishuWebhook(c *gin.Context) {
// SaveFeishuWebhook 幂等保存当前用户的飞书 Webhook 触发器配置。
func (api *NotificationAPI) SaveFeishuWebhook(c *gin.Context) {
if api == nil || api.channelService == nil {
if api == nil || api.client == nil {
c.JSON(http.StatusInternalServerError, respond.InternalError(nilServiceError("通知通道 service 未初始化")))
return
}
@@ -72,14 +74,15 @@ func (api *NotificationAPI) SaveFeishuWebhook(c *gin.Context) {
ctx, cancel := context.WithTimeout(c.Request.Context(), notificationAPITimeout)
defer cancel()
channel, err := api.channelService.SaveFeishuWebhook(ctx, c.GetInt("user_id"), notification.SaveFeishuWebhookRequest{
channel, err := api.client.SaveFeishuWebhook(ctx, contracts.SaveFeishuWebhookRequest{
UserID: c.GetInt("user_id"),
Enabled: enabled,
WebhookURL: req.WebhookURL,
AuthType: req.AuthType,
BearerToken: req.BearerToken,
})
if err != nil {
writeNotificationError(c, err)
respond.DealWithError(c, err)
return
}
c.JSON(http.StatusOK, respond.RespWithData(respond.Ok, channel))
@@ -87,7 +90,7 @@ func (api *NotificationAPI) SaveFeishuWebhook(c *gin.Context) {
// DeleteFeishuWebhook 删除当前用户的飞书 Webhook 触发器配置。
func (api *NotificationAPI) DeleteFeishuWebhook(c *gin.Context) {
if api == nil || api.channelService == nil {
if api == nil || api.client == nil {
c.JSON(http.StatusInternalServerError, respond.InternalError(nilServiceError("通知通道 service 未初始化")))
return
}
@@ -95,8 +98,10 @@ func (api *NotificationAPI) DeleteFeishuWebhook(c *gin.Context) {
ctx, cancel := context.WithTimeout(c.Request.Context(), notificationAPITimeout)
defer cancel()
if err := api.channelService.DeleteFeishuWebhook(ctx, c.GetInt("user_id")); err != nil {
writeNotificationError(c, err)
if err := api.client.DeleteFeishuWebhook(ctx, contracts.DeleteFeishuWebhookRequest{
UserID: c.GetInt("user_id"),
}); err != nil {
respond.DealWithError(c, err)
return
}
c.JSON(http.StatusOK, respond.RespWithData(respond.Ok, gin.H{"deleted": true}))
@@ -104,7 +109,7 @@ func (api *NotificationAPI) DeleteFeishuWebhook(c *gin.Context) {
// TestFeishuWebhook 发送一条最小业务 JSON 到当前用户配置的飞书 Webhook。
func (api *NotificationAPI) TestFeishuWebhook(c *gin.Context) {
if api == nil || api.channelService == nil {
if api == nil || api.client == nil {
c.JSON(http.StatusInternalServerError, respond.InternalError(nilServiceError("通知通道 service 未初始化")))
return
}
@@ -112,18 +117,12 @@ func (api *NotificationAPI) TestFeishuWebhook(c *gin.Context) {
ctx, cancel := context.WithTimeout(c.Request.Context(), notificationAPITimeout)
defer cancel()
result, err := api.channelService.TestFeishuWebhook(ctx, c.GetInt("user_id"))
result, err := api.client.TestFeishuWebhook(ctx, contracts.TestFeishuWebhookRequest{
UserID: c.GetInt("user_id"),
})
if err != nil {
writeNotificationError(c, err)
respond.DealWithError(c, err)
return
}
c.JSON(http.StatusOK, respond.RespWithData(respond.Ok, result))
}
func writeNotificationError(c *gin.Context, err error) {
if errors.Is(err, notification.ErrInvalidChannelConfig) {
c.JSON(http.StatusBadRequest, respond.WrongParamType)
return
}
respond.DealWithError(c, err)
}