feat: 停复机规则演练与终态自动关闭轮询
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 9m26s
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 9m26s
统一停复机规则转正的第一次发布:总开关仍关闭,现有停复机行为不变。 - 新增停复机规则演练:GET /stop-resume-rule/dry-run 按新规则对开机与停机卡只判断不执行,返回运营商账户 × 停机原因 × 判断结果汇总与分页明细,可假设通道实名管控配置;POST /stop-resume-rule/dry-run/export 导出明细(场景 stop_resume_rule_dry_run);仅超级管理员与平台账号 - 风险停机、已销户关闭轮询不再限于独立卡,只关该卡自身轮询,不级联设备 - 错误分类规则增加 auto_disable_polling,仅终态类别可勾选;停复机(含设备停复机)命中该规则时不重试、关闭该卡轮询、回写网关扩展状态并写审计 iot_card.terminal_disable_polling - 迁移 000250 加列与约束,并为"已销户""仅支持风险复机"置位
This commit is contained in:
@@ -30,6 +30,7 @@ import (
|
||||
|
||||
type Service struct {
|
||||
stopResumeRecorder *gatewaylimit.StopResumeRecorder
|
||||
terminalPolling *gatewaylimit.TerminalPollingDisabler
|
||||
db *gorm.DB
|
||||
redis *redis.Client
|
||||
deviceStore *postgres.DeviceStore
|
||||
@@ -1436,6 +1437,7 @@ func (s *Service) StopDevice(ctx context.Context, deviceID uint) (*dto.DeviceSus
|
||||
s.recordCardCommand(ctx, card, constants.StopResumeActionStop, constants.StopReasonManual, gwErr, time.Since(callStartedAt))
|
||||
integrationID = observer.integration
|
||||
if gwErr != nil {
|
||||
s.disablePollingOnTerminal(ctx, card, constants.StopResumeActionStop, gwErr)
|
||||
log.Error("设备停机-调网关停机失败",
|
||||
zap.Uint("device_id", deviceID),
|
||||
zap.String("iccid", card.ICCID),
|
||||
@@ -1582,6 +1584,7 @@ func (s *Service) StartDevice(ctx context.Context, deviceID uint) error {
|
||||
s.recordCardCommand(ctx, card, constants.StopResumeActionStart, "", gwErr, time.Since(callStartedAt))
|
||||
integrationID = observer.integration
|
||||
if gwErr != nil {
|
||||
s.disablePollingOnTerminal(ctx, card, constants.StopResumeActionStart, gwErr)
|
||||
log.Error("设备复机-调网关复机失败",
|
||||
zap.Uint("device_id", deviceID),
|
||||
zap.String("iccid", card.ICCID),
|
||||
@@ -1735,6 +1738,18 @@ func (s *Service) SetStopResumeRecorder(recorder *gatewaylimit.StopResumeRecorde
|
||||
s.stopResumeRecorder = recorder
|
||||
}
|
||||
|
||||
// SetTerminalPollingDisabler 注入终态关闭轮询处理器:设备下某张卡停复机命中"自动关闭轮询"规则时只关闭该卡轮询。
|
||||
func (s *Service) SetTerminalPollingDisabler(disabler *gatewaylimit.TerminalPollingDisabler) {
|
||||
s.terminalPolling = disabler
|
||||
}
|
||||
|
||||
// disablePollingOnTerminal 设备下单卡停复机失败命中终态规则时关闭该卡轮询并清除轮询卡缓存。
|
||||
func (s *Service) disablePollingOnTerminal(ctx context.Context, card *model.IotCard, action string, callErr error) {
|
||||
if s.terminalPolling.HandleCommandError(ctx, card, action, callErr) {
|
||||
s.invalidatePollingCardCache(card.ID)
|
||||
}
|
||||
}
|
||||
|
||||
// recordCardCommand 写设备下单卡的停复机记录;停复机只发一次请求,尝试序号恒为 1。
|
||||
func (s *Service) recordCardCommand(ctx context.Context, card *model.IotCard, action, reason string, callErr error, duration time.Duration) {
|
||||
if s.stopResumeRecorder == nil || card == nil {
|
||||
|
||||
@@ -58,6 +58,8 @@ type StopResumeService struct {
|
||||
channelThresholdGuard ChannelThresholdLockGuard
|
||||
// stopResumeRecorder 写每次上游停复机调用的停复机记录;未注入时不写。
|
||||
stopResumeRecorder *gatewaylimit.StopResumeRecorder
|
||||
// terminalPolling 停复机命中"自动关闭轮询"终态规则时关闭该卡轮询;未注入时按普通失败处理。
|
||||
terminalPolling *gatewaylimit.TerminalPollingDisabler
|
||||
}
|
||||
|
||||
// SetStopResumeRecorder 注入停复机记录写入器。
|
||||
@@ -65,6 +67,23 @@ func (s *StopResumeService) SetStopResumeRecorder(recorder *gatewaylimit.StopRes
|
||||
s.stopResumeRecorder = recorder
|
||||
}
|
||||
|
||||
// SetTerminalPollingDisabler 注入终态关闭轮询处理器。
|
||||
func (s *StopResumeService) SetTerminalPollingDisabler(disabler *gatewaylimit.TerminalPollingDisabler) {
|
||||
s.terminalPolling = disabler
|
||||
}
|
||||
|
||||
// disablePollingOnTerminal 停复机上游失败命中"自动关闭轮询"规则时关闭该卡轮询并通知调度器;返回 true 表示不再重试。
|
||||
func (s *StopResumeService) disablePollingOnTerminal(ctx context.Context, card *model.IotCard, action string, callErr error) bool {
|
||||
if !s.terminalPolling.HandleCommandError(ctx, card, action, callErr) {
|
||||
return false
|
||||
}
|
||||
card.EnablePolling = false
|
||||
if s.pollingCallback != nil {
|
||||
s.pollingCallback.OnCardDisabled(ctx, card.ID)
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// SetObservationSeriesEventWriter 注入停复机成功观测序列 Outbox Writer。
|
||||
func (s *StopResumeService) SetObservationSeriesEventWriter(db *gorm.DB, writer cardObservationApp.SeriesEventWriter) {
|
||||
s.db = db
|
||||
@@ -747,6 +766,9 @@ func (s *StopResumeService) stopCardWithRetry(ctx context.Context, card *model.I
|
||||
}
|
||||
|
||||
lastErr = callErr
|
||||
if s.disablePollingOnTerminal(ctx, card, constants.StopResumeActionStop, callErr) {
|
||||
break
|
||||
}
|
||||
s.logger.Warn("调用停机接口失败,准备重试",
|
||||
zap.Int("attempt", i+1),
|
||||
zap.String("iccid", card.ICCID),
|
||||
@@ -823,6 +845,9 @@ func (s *StopResumeService) resumeCardWithRetry(ctx context.Context, card *model
|
||||
}
|
||||
|
||||
lastErr = callErr
|
||||
if s.disablePollingOnTerminal(ctx, card, constants.StopResumeActionStart, callErr) {
|
||||
break
|
||||
}
|
||||
s.logger.Warn("调用复机接口失败,准备重试",
|
||||
zap.Int("attempt", i+1),
|
||||
zap.String("iccid", card.ICCID),
|
||||
|
||||
@@ -491,7 +491,8 @@ func (s *GatewayRateLimitService) TestRule(ctx context.Context, req *dto.TestGat
|
||||
matched := result.Keyword != ""
|
||||
return &dto.TestGatewayErrorRuleResponse{
|
||||
Category: result.Category, CategoryName: constants.GatewayErrorClassName(result.Category),
|
||||
RuleID: result.RuleID, Keyword: result.Keyword, MaybeAccepted: result.MaybeAccepted, Matched: matched,
|
||||
RuleID: result.RuleID, Keyword: result.Keyword, MaybeAccepted: result.MaybeAccepted,
|
||||
AutoDisablePolling: result.AutoDisablePolling, Matched: matched,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -510,6 +511,9 @@ func validateRule(req *dto.SaveGatewayErrorRuleRequest) error {
|
||||
if strings.TrimSpace(req.Keyword) == "" {
|
||||
return errors.New(errors.CodeInvalidParam, "匹配关键字不能为空")
|
||||
}
|
||||
if req.AutoDisablePolling != nil && *req.AutoDisablePolling && req.Category != constants.GatewayClassTerminal {
|
||||
return errors.New(errors.CodeInvalidParam, "只有终态不可操作类别的规则可以勾选自动关闭轮询")
|
||||
}
|
||||
if req.MatchType == constants.GatewayMatchRegex {
|
||||
if _, err := regexp.Compile(req.Keyword); err != nil {
|
||||
return errors.New(errors.CodeInvalidParam, "正则表达式不合法")
|
||||
@@ -528,6 +532,13 @@ func applyRuleRequest(row *model.GatewayErrorClassification, req *dto.SaveGatewa
|
||||
row.Priority = 100
|
||||
}
|
||||
row.MaybeAccepted = req.MaybeAccepted
|
||||
if req.AutoDisablePolling != nil {
|
||||
row.AutoDisablePolling = *req.AutoDisablePolling
|
||||
}
|
||||
// 类别改为非终态时自动关闭轮询标记随之失效,避免违反"仅终态可置位"。
|
||||
if row.Category != constants.GatewayClassTerminal {
|
||||
row.AutoDisablePolling = false
|
||||
}
|
||||
row.Remark = strings.TrimSpace(req.Remark)
|
||||
if req.Status != nil {
|
||||
row.Status = *req.Status
|
||||
@@ -537,7 +548,8 @@ func applyRuleRequest(row *model.GatewayErrorClassification, req *dto.SaveGatewa
|
||||
func ruleState(row *model.GatewayErrorClassification) map[string]any {
|
||||
ops, _ := gatewaylimit.DecodeOperations(row.Operations)
|
||||
return map[string]any{"keyword": row.Keyword, "match_type": row.MatchType, "category": row.Category,
|
||||
"operations": ops, "priority": row.Priority, "maybe_accepted": row.MaybeAccepted, "status": row.Status}
|
||||
"operations": ops, "priority": row.Priority, "maybe_accepted": row.MaybeAccepted,
|
||||
"auto_disable_polling": row.AutoDisablePolling, "status": row.Status}
|
||||
}
|
||||
|
||||
func ruleAudit(action, summary string, row *model.GatewayErrorClassification, operatorID uint, before map[string]any) auditinfra.PollingInput {
|
||||
@@ -556,7 +568,7 @@ func toRuleResponse(row *model.GatewayErrorClassification) dto.GatewayErrorRuleR
|
||||
return dto.GatewayErrorRuleResponse{
|
||||
ID: row.ID, Keyword: row.Keyword, MatchType: row.MatchType, Category: row.Category,
|
||||
CategoryName: constants.GatewayErrorClassName(row.Category), Operations: nonNil(ops), Priority: row.Priority,
|
||||
MaybeAccepted: row.MaybeAccepted, Status: row.Status, Remark: row.Remark, CreatedAt: row.CreatedAt, UpdatedAt: row.UpdatedAt,
|
||||
MaybeAccepted: row.MaybeAccepted, AutoDisablePolling: row.AutoDisablePolling, Status: row.Status, Remark: row.Remark, CreatedAt: row.CreatedAt, UpdatedAt: row.UpdatedAt,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user