阶段七:完成 React Feature 化与前置重构收口
This commit is contained in:
1 parent
375e84d663
commit
1a228dc35c
305 files changed
+14179
-5431
No files matched your search
@@ -149,6 +149,30 @@ func (n *Notifier) NotifyDelivery(accountID, buyerName, buyerID, itemID, message
|
||||
})
|
||||
}
|
||||
|
||||
// NotifyAutomationRun 将一个自动化运行的终态通知持久化到 outbox。
|
||||
// runID 与 status 共同构成稳定幂等键:同一次恢复重复报告同一终态时不会重复入队;
|
||||
// 状态改变时保留独立通知,便于人工核对后继续执行的运行报告最终结果。
|
||||
func (n *Notifier) NotifyAutomationRun(ctx context.Context, runID int64, accountID, buyerID, itemID, status, message, chatID string) {
|
||||
if n == nil || runID <= 0 || strings.TrimSpace(status) == "" {
|
||||
return
|
||||
}
|
||||
// idempotencyKey 绑定自动化运行主键与终态,避免恢复扫描或状态收口重试创建新 outbox 消息。
|
||||
idempotencyKey := fmt.Sprintf("automation-run:%d:%s", runID, strings.TrimSpace(status))
|
||||
n.notifyEvent(ctx, NotificationEvent{
|
||||
AccountID: accountID,
|
||||
Type: EventDeliveryResult,
|
||||
Level: "info",
|
||||
Title: "自动化运行通知",
|
||||
Body: message,
|
||||
Fields: map[string]string{
|
||||
"买家": fmt.Sprintf("(ID: %s)", buyerID),
|
||||
"商品ID": itemID,
|
||||
"聊天ID": fallback(chatID, "未知"),
|
||||
"结果": message,
|
||||
},
|
||||
}, idempotencyKey)
|
||||
}
|
||||
|
||||
// NotifyAccountAlert 发送账号告警通知(token 失效/自动恢复失败/风控验证等)。
|
||||
// level 取 AlertLevel* 常量。向该账号所有已启用渠道发送。
|
||||
// NotifyAccountAlert 负责Notify账号Alert相关处理。
|
||||
@@ -169,6 +193,11 @@ func (n *Notifier) NotifyAccountEvent(accountID, eventType, level, title, body s
|
||||
|
||||
// NotifyEvent 根据事件类型筛选渠道并发送通知。
|
||||
func (n *Notifier) NotifyEvent(ctx context.Context, ev NotificationEvent) {
|
||||
n.notifyEvent(ctx, ev, "")
|
||||
}
|
||||
|
||||
// notifyEvent 根据事件类型筛选渠道并发送通知;idempotencyKey 只用于 outbox 持久化去重,不能为空时必须来自稳定业务事实。
|
||||
func (n *Notifier) notifyEvent(ctx context.Context, ev NotificationEvent, idempotencyKey string) {
|
||||
if n == nil || n.repository == nil {
|
||||
return
|
||||
}
|
||||
@@ -197,12 +226,14 @@ func (n *Notifier) NotifyEvent(ctx context.Context, ev NotificationEvent) {
|
||||
}
|
||||
eligible = append(eligible, ch)
|
||||
}
|
||||
if n.started.Load() {
|
||||
// messages 保存消息列表,供当前处理流程使用
|
||||
// 自动化运行终态必须先进入 outbox:即使 worker 尚未随应用生命周期启动,也不能回退到
|
||||
// 同步网络发送,否则会绕过业务幂等键和发送成功后的 uncertain 隔离。
|
||||
if n.started.Load() || strings.TrimSpace(idempotencyKey) != "" {
|
||||
// messages 保存本事件每个允许渠道的一条 outbox 记录,并共享同一个业务投递键。
|
||||
messages := make([]db.NotificationOutboxInput, 0, len(eligible))
|
||||
// ch 表示当前遍历过程中的ch
|
||||
for _, ch := range eligible {
|
||||
messages = append(messages, db.NotificationOutboxInput{ChannelID: ch.ID, EventType: ev.Type, Body: full})
|
||||
messages = append(messages, db.NotificationOutboxInput{ChannelID: ch.ID, EventType: ev.Type, Body: full, IdempotencyKey: idempotencyKey})
|
||||
}
|
||||
if // err 保存err,供当前处理流程使用
|
||||
err := n.repository.EnqueueOutbox(ctx, messages); err != nil {
|
||||
|
||||
@@ -404,6 +404,47 @@ func TestNotifyDelivery_EmptyChatID(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestNotifyAutomationRunQueuesEachTerminalStateOnce 验证自动化运行通知即使 worker 尚未启动也写入
|
||||
// 持久化 outbox,同一运行同一终态重复恢复只保留一次;不同终态保留各自独立的人工核对记录。
|
||||
func TestNotifyAutomationRunQueuesEachTerminalStateOnce(t *testing.T) {
|
||||
// store、cleanup 保存本测试的 SQLite 通知存储与关闭责任。
|
||||
store, cleanup := newNotifyStoreBare(t)
|
||||
defer cleanup()
|
||||
// channelID 保存绑定给自动化账号的 webhook 渠道主键。
|
||||
channelID := addWebhookChannel(t, store, "cid", "自动化幂等渠道", "http://127.0.0.1:1")
|
||||
// notifier 保存未启动 worker 的真实 outbox 通知器;自动化终态仍不得降级为同步网络发送。
|
||||
notifier := New("cid", store, nil)
|
||||
// notifyCtx 约束自动化结果入队操作,不携带网络或账号凭证。
|
||||
notifyCtx := context.Background()
|
||||
notifier.NotifyAutomationRun(notifyCtx, 73, "cid", "buyer", "item", "success", "付款发货成功", "chat")
|
||||
notifier.NotifyAutomationRun(notifyCtx, 73, "cid", "buyer", "item", "success", "付款发货成功", "chat")
|
||||
notifier.NotifyAutomationRun(notifyCtx, 73, "cid", "buyer", "item", "needs_review", "付款发货需要人工核对", "chat")
|
||||
// rows 保存该渠道上同一自动化运行的 outbox 终态键和记录数量。
|
||||
rows, queryErr := store.DB.QueryContext(notifyCtx, `SELECT idempotency_key FROM notification_outbox WHERE channel_id=? ORDER BY idempotency_key`, channelID)
|
||||
if queryErr != nil {
|
||||
t.Fatal(queryErr)
|
||||
}
|
||||
defer rows.Close()
|
||||
// keys 保存已持久化的自动化运行终态键,用于断言重复 success 未产生第二条记录。
|
||||
keys := make([]string, 0, 2)
|
||||
for rows.Next() {
|
||||
// key 保存当前 outbox 行绑定的稳定自动化通知键。
|
||||
var key string
|
||||
// scanErr 保存读取当前 outbox 幂等键时的数据库错误。
|
||||
if scanErr := rows.Scan(&key); scanErr != nil {
|
||||
t.Fatal(scanErr)
|
||||
}
|
||||
keys = append(keys, key)
|
||||
}
|
||||
// rowsErr 保存遍历 outbox 查询结果结束时的数据库错误。
|
||||
if rowsErr := rows.Err(); rowsErr != nil {
|
||||
t.Fatal(rowsErr)
|
||||
}
|
||||
if len(keys) != 2 || keys[0] != "automation-run:73:needs_review" || keys[1] != "automation-run:73:success" {
|
||||
t.Fatalf("outbox keys=%v", keys)
|
||||
}
|
||||
}
|
||||
|
||||
// TestParseConfig_InvalidJSON 非法 JSON 走旧格式兼容分支。
|
||||
func TestParseConfig_InvalidJSON(t *testing.T) {
|
||||
// m 保存m,供当前处理流程使用
|
||||
|
||||
Reference in new issue
Block a user