All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 14m13s
新增 000228 成对迁移 tb_polling_priority_item:卡、任务类型、状态、触发类型、来源订单/套餐使用记录、
触发次数与来源集合、尝试次数、失败原因、人工原因与操作者、店铺快照与各时间列;以活动项部分唯一索引
uq_polling_priority_item_active(仅 deleted_at IS NULL AND status IN ('pending','processing') 占键位)
表达「同卡同任务类型至多一条活动项」,另有状态/时间索引与全列注释;down 守卫在存在活动项或未终态行时
拒绝回滚并给出中文原因。
新增优先轮询请求可靠事件 polling.priority.requested(载荷版本 v1、事件键前缀 prio:)与消费者:只在原
业务事务内追加、幂等键稳定;消费者按卡 × 纳入任务类型(realname/carddata/card_status/package)逐条
建项并在提交后下发执行提示,重复投递只合并触发次数、来源集合与最近触发时间,不新建行也不重复调用。
触发点为四类自动场景 purchase_activated / renewal_activated(按同载体更早套餐使用记录判定)/
queue_activated / addon_activated 与「无有效套餐」no_valid_package(仅在普通套餐轮询来源且存在待生效
套餐使用记录时追加;事件通道显式拒绝 manual_trigger);入队对象恒为卡,绑定设备资产在触发事务内冻结
在用卡快照逐卡建项,不使用设备当前卡槽口径。
轮询共享基类新增认领接缝:四个 Handler(realname/carddata/card_status/package)在并发信号量之后、调用
上游之前探测活动项——待执行条件认领、执行中且 90 秒租约未到期则跳过并延后、无活动项时行为与既有完全
等价;超租约允许相邻执行接管,尝试次数只在真正发起执行后累加,未达上限(3)回到活动态按既有间隔重排,
达上限或业务校验类失败进入失败终态并保留可安全展示原因;执行前校验卡自身与绑定设备的轮询开关。未引入
通用卡级锁与 Redis 活动标记,分片队列的出队、入队与移除路径未改动。
提示通道按任务类型独立键(polling:priority:{taskType}),与既有手动触发队列分离;调度器在同一周期内先
排空优先提示、再排空手动触发队列,提示排空不受分片背压跳过影响;未新建调度设施或异步任务类型。
新增人工优先入队与只读查询三条路由 POST /api/admin/polling-priority-items、
GET /api/admin/polling-priority-items、GET /api/admin/polling-priority-items/:id:人工入队复用既有轮询
权限判定(抽取为同包共享函数),原因必填,不受每日 500 次上限与 24 小时去重约束,重复抑制由活动项合并
承担;读侧按店铺快照下推数据范围,越权与不存在不可区分,不提供优先级分级、有效期或人工重触发入口。
新增 7 个审计动作(enqueue/claim/fail/retry/complete/dequeue/manual_denied)与资源
polling_priority_item,并按(操作者类型,来源)注册,人工侧与 Worker 侧均通过来源校验。
同步 OpenAPI 文档装配三处与路由注册;归档 Change 至
openspec/changes/archive/2026-09-17-add-priority-polling-queue/ 并同步主 Spec(新增
priority-polling-queue、polling-operations 追加单次执行互斥 Requirement 与三条路由索引)与上下文健康
证据(requirement-evidence 150 行、入口矩阵 http 403 / async 56)。
本机验证:junhong_cmp_test 与隔离 Redis DB 15,未连生产、未启动 Worker/API、未调用运营商上游;迁移
up/down/up 与 down 守卫实测(含 dirty=true 记账口径与 force 恢复),A–F 批 94 PASS、接缝 63 PASS、
提示通道 12 PASS、清理零残留 20 PASS。成功路径 Complete、真并发互斥、尝试上限第 3 次判定、HTTP 层权限
矩阵、通道阈值持锁复机边界与三类生效触发点生产集成留待测试部署验证(见
docs/verification/add-priority-polling-queue-verification.md 第 4 节)。自动化测试按项目决策为 N/A,
未新增 *_test.go。
133 lines
6.3 KiB
Go
133 lines
6.3 KiB
Go
package task
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/hibiken/asynq"
|
|
"go.uber.org/zap"
|
|
|
|
cardapp "github.com/break/junhong_cmp_fiber/internal/application/cardobservation"
|
|
carddomain "github.com/break/junhong_cmp_fiber/internal/domain/cardobservation"
|
|
"github.com/break/junhong_cmp_fiber/internal/gateway"
|
|
"github.com/break/junhong_cmp_fiber/internal/infrastructure/integrationlog"
|
|
"github.com/break/junhong_cmp_fiber/pkg/constants"
|
|
)
|
|
|
|
// PollingRealnameHandler 负责实名轮询编排,查询结果统一交给卡观测应用服务。
|
|
type PollingRealnameHandler struct {
|
|
base *PollingBase
|
|
gateway *gateway.Client
|
|
observation *cardapp.Service
|
|
integration *integrationlog.Repository
|
|
}
|
|
|
|
// NewPollingRealnameHandler 创建实名检查任务处理器。
|
|
func NewPollingRealnameHandler(base *PollingBase, gw *gateway.Client, observation *cardapp.Service, integration *integrationlog.Repository) *PollingRealnameHandler {
|
|
return &PollingRealnameHandler{base: base, gateway: gw, observation: observation, integration: integration}
|
|
}
|
|
|
|
// Handle 处理实名检查任务。
|
|
func (h *PollingRealnameHandler) Handle(ctx context.Context, task *asynq.Task) error {
|
|
startedAt := time.Now()
|
|
cardID, ok := parseTaskPayload(task.Payload(), h.base.logger)
|
|
if !ok {
|
|
return nil
|
|
}
|
|
if !h.base.acquireConcurrency(ctx, constants.TaskTypePollingRealname) {
|
|
h.base.logger.Debug("并发已满,重新入队", zap.Uint("card_id", cardID))
|
|
return h.base.requeueCard(ctx, cardID, constants.TaskTypePollingRealname)
|
|
}
|
|
defer h.base.releaseConcurrency(ctx, constants.TaskTypePollingRealname)
|
|
|
|
// 优先轮询互斥接缝:存在活动优先项时本轮即优先执行或被跳过;无活动项时行为与既有实现一致。
|
|
priority, skip, priorityErr := h.base.beginPollingPriority(ctx, cardID, constants.TaskTypePollingRealname)
|
|
if priorityErr != nil {
|
|
return h.failAndRequeue(ctx, cardID, startedAt, "查询活动优先轮询项失败", priorityErr)
|
|
}
|
|
if skip {
|
|
h.base.logger.Debug("卡轮询优先项已被其它执行持有,本轮跳过并延后",
|
|
zap.Uint("card_id", cardID), zap.String("task_type", constants.TaskTypePollingRealname))
|
|
return h.base.requeueCard(ctx, cardID, constants.TaskTypePollingRealname)
|
|
}
|
|
|
|
card, err := h.base.getCardWithCache(ctx, cardID)
|
|
if err != nil {
|
|
if isNotFound(err) {
|
|
priority.FailFinal(ctx, "卡不存在")
|
|
return nil
|
|
}
|
|
// 本地读卡失败属可恢复失败:先按真实执行累加尝试次数,再按上限收敛,避免加急事实被静默丢弃。
|
|
priority.MarkExecution(ctx)
|
|
priority.FailRetryable(ctx, "获取卡信息失败")
|
|
return h.failAndRequeue(ctx, cardID, startedAt, "获取卡信息失败", err)
|
|
}
|
|
// 执行前轮询范围校验:优先通道不得重启已停止轮询的卡。
|
|
if !h.base.priorityInPollingScope(ctx, priority, card) {
|
|
return h.base.requeueCard(ctx, cardID, constants.TaskTypePollingRealname)
|
|
}
|
|
if card.CardCategory == constants.CardCategoryIndustry || h.gateway == nil {
|
|
priority.FailFinal(ctx, "卡类型不适用实名轮询或实名查询能力未配置")
|
|
return h.base.requeueCard(ctx, cardID, constants.TaskTypePollingRealname)
|
|
}
|
|
priority.MarkExecution(ctx)
|
|
attempt := newGatewayAttempt(cardID, constants.IntegrationOperationGatewayRealname, constants.CardObservationSceneRealnamePolling)
|
|
result, err := h.gateway.QueryRealnameStatus(ctx, &gateway.CardStatusReq{CardNo: card.ICCID})
|
|
if err != nil {
|
|
_ = recordGatewayAttempt(ctx, h.integration, attempt, constants.IntegrationResultFailed, false, "查询实名状态失败")
|
|
priority.FailRetryable(ctx, "查询实名状态失败")
|
|
return h.failAndRequeue(ctx, cardID, startedAt, "查询实名状态失败", err)
|
|
}
|
|
if strings.TrimSpace(result.ICCID) == "" {
|
|
_ = recordGatewayAttempt(ctx, h.integration, attempt, constants.IntegrationResultInvalidPayload, false, "实名查询响应缺少 ICCID")
|
|
priority.FailRetryable(ctx, "实名查询响应缺少 ICCID")
|
|
return h.failAndRequeue(ctx, cardID, startedAt, "实名查询响应缺少 ICCID", nil)
|
|
}
|
|
ctx = withPollingWorkerAuditContext(ctx, constants.TaskTypePollingRealname, "实名状态轮询任务", attempt.IntegrationID)
|
|
if h.observation == nil {
|
|
priority.FailFinal(ctx, "卡实名观测能力未配置")
|
|
return h.failAndRequeue(ctx, cardID, startedAt, "卡实名观测能力未配置", nil)
|
|
}
|
|
now := time.Now()
|
|
decision, err := h.observation.ApplyCardObservation(ctx, carddomain.RealnameObservation{
|
|
CardID: cardID, Verified: result.RealStatus,
|
|
Metadata: carddomain.ObservationMetadata{
|
|
ObservationID: attempt.IntegrationID, Source: constants.CardObservationSourcePolling,
|
|
Scene: constants.CardObservationSceneRealnamePolling, ObservedAt: now, CorrelationID: attempt.IntegrationID,
|
|
},
|
|
})
|
|
if err != nil {
|
|
_ = recordGatewayAttempt(ctx, h.integration, attempt, constants.IntegrationResultUnknown, false, "应用实名观测失败")
|
|
priority.FailRetryable(ctx, "应用实名观测失败")
|
|
return h.failAndRequeue(ctx, cardID, startedAt, "应用实名观测失败", err)
|
|
}
|
|
changed := decision.StatusChanged
|
|
if changed {
|
|
_ = recordGatewayAttempt(ctx, h.integration, attempt, constants.IntegrationResultSuccess, true, "实名状态发生变化")
|
|
}
|
|
if h.base.verboseLog {
|
|
h.base.logger.Info("实名状态轮询详情", zap.Uint("card_id", cardID), zap.String("iccid", card.ICCID),
|
|
zap.Bool("real_status", result.RealStatus), zap.Int("new_status", decision.AfterStatus),
|
|
zap.Bool("changed", decision.StatusChanged), zap.Bool("reversal_pending", decision.ReversalPending))
|
|
}
|
|
metric := "polling.observation.unchanged"
|
|
if changed {
|
|
metric = "polling.observation.persisted"
|
|
}
|
|
h.base.logger.Info("实名轮询观测完成", zap.Uint("card_id", cardID), zap.Bool("changed", changed), zap.String("metric", metric))
|
|
priority.Complete(ctx)
|
|
h.base.updateStats(ctx, constants.TaskTypePollingRealname, true, time.Since(startedAt))
|
|
return h.base.requeueCard(ctx, cardID, constants.TaskTypePollingRealname)
|
|
}
|
|
|
|
func (h *PollingRealnameHandler) failAndRequeue(ctx context.Context, cardID uint, startedAt time.Time, message string, err error) error {
|
|
fields := []zap.Field{zap.Uint("card_id", cardID)}
|
|
if err != nil {
|
|
fields = append(fields, zap.Error(err))
|
|
}
|
|
h.base.logger.Warn(message, fields...)
|
|
h.base.updateStats(ctx, constants.TaskTypePollingRealname, false, time.Since(startedAt))
|
|
return h.base.requeueCard(ctx, cardID, constants.TaskTypePollingRealname)
|
|
}
|