fix: 补全三处轮询覆盖缺口
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 7m11s
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 7m11s
问题一:protect 轮询任务从未被调度器初始化入队 PollingConfig 新增 protect_check_interval 字段(NULL=不参与), 调度器 initCardsBatch/initCardPolling/requeueCard 补全 protect 队列 初始化逻辑,IotCard 新增 last_protect_check_at 记录上次检查时间。 迁移文件:000102_add_polling_protect_fields 问题二:设备套餐流量耗尽时绑定卡未被停机 UsageService.checkAndTriggerSuspension 新增 carrier_type=device 分支, 通过 DeviceSimBindingStore.ListByDeviceID 查询绑定卡,对每张卡异步 触发 CheckAndStopCard,同步注入 Bootstrap 依赖。 问题三:主套餐过期后不立即停机,依赖下次轮询兜底 PackageActivationHandler.processExpiredPackage 在 updateCarrierSuspended Status 后,若载体无后续生效套餐,立即异步调用 CheckAndStopCard(iot_card 类型直接触发,device 类型遍历绑定卡),消除停机延迟窗口。 Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
This commit is contained in:
@@ -231,6 +231,9 @@ func (h *PollingHandler) HandleCarddataCheck(ctx context.Context, t *asynq.Task)
|
||||
return h.requeueCard(ctx, uint(cardID), constants.TaskTypePollingCarddata)
|
||||
}
|
||||
|
||||
// 必须在 gateway 查询前执行:gateway 报错会提前 return,checkStopResume 放在后面时无套餐在线卡无法被自动停机
|
||||
h.checkStopResume(ctx, card)
|
||||
|
||||
// 调用 Gateway API 查询流量
|
||||
var gatewayFlowMB float64
|
||||
if h.gatewayClient != nil {
|
||||
@@ -293,9 +296,6 @@ func (h *PollingHandler) HandleCarddataCheck(ctx context.Context, t *asynq.Task)
|
||||
}
|
||||
}
|
||||
|
||||
// 独立停机/复机检查(不依赖流量增量,每次轮询都执行)
|
||||
h.checkStopResume(ctx, card)
|
||||
|
||||
// 更新监控统计
|
||||
h.updateStats(ctx, constants.TaskTypePollingCarddata, true, time.Since(startTime))
|
||||
|
||||
@@ -911,10 +911,20 @@ func (h *PollingHandler) requeueCard(ctx context.Context, cardID uint, taskType
|
||||
}
|
||||
|
||||
nextCheck := time.Now().Add(time.Duration(intervalSeconds) * time.Second)
|
||||
return h.redis.ZAdd(ctx, queueKey, redis.Z{
|
||||
if err := h.redis.ZAdd(ctx, queueKey, redis.Z{
|
||||
Score: float64(nextCheck.Unix()),
|
||||
Member: strconv.FormatUint(uint64(cardID), 10),
|
||||
}).Err()
|
||||
}).Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// protect 任务重新入队时更新检查时间,供下次初始化计算队列延迟
|
||||
if taskType == constants.TaskTypePollingProtect {
|
||||
now := time.Now()
|
||||
h.db.Model(&model.IotCard{}).Where("id = ?", cardID).Update("last_protect_check_at", now)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// getMatchedPollingInterval 读取该卡匹配的 PollingConfig,返回指定任务类型的间隔秒数
|
||||
@@ -952,9 +962,8 @@ func (h *PollingHandler) getMatchedPollingInterval(ctx context.Context, cardID u
|
||||
return *cfg.PackageCheckInterval, nil
|
||||
}
|
||||
case constants.TaskTypePollingProtect:
|
||||
// protect 任务暂无专用间隔字段,使用 realname 间隔兜底
|
||||
if cfg.RealnameCheckInterval != nil && *cfg.RealnameCheckInterval > 0 {
|
||||
return *cfg.RealnameCheckInterval, nil
|
||||
if cfg.ProtectCheckInterval != nil && *cfg.ProtectCheckInterval > 0 {
|
||||
return *cfg.ProtectCheckInterval, nil
|
||||
}
|
||||
}
|
||||
// 匹配到配置但该任务类型间隔为 NULL,说明此卡不需要此类轮询
|
||||
@@ -1202,18 +1211,18 @@ func (h *PollingHandler) HandleProtectConsistencyCheck(ctx context.Context, t *a
|
||||
|
||||
// 未绑设备(独立卡),跳过
|
||||
if card.IsStandalone {
|
||||
return nil
|
||||
return h.requeueCard(ctx, card.ID, constants.TaskTypePollingProtect)
|
||||
}
|
||||
|
||||
// 未实名,跳过
|
||||
if card.RealNameStatus != constants.RealNameStatusVerified {
|
||||
return nil
|
||||
return h.requeueCard(ctx, card.ID, constants.TaskTypePollingProtect)
|
||||
}
|
||||
|
||||
// 查绑定设备
|
||||
binding, err := h.deviceSimBindingStore.GetActiveBindingByCardID(ctx, card.ID)
|
||||
if err != nil || binding == nil {
|
||||
return nil
|
||||
return h.requeueCard(ctx, card.ID, constants.TaskTypePollingProtect)
|
||||
}
|
||||
deviceID := binding.DeviceID
|
||||
|
||||
@@ -1234,7 +1243,7 @@ func (h *PollingHandler) HandleProtectConsistencyCheck(ctx context.Context, t *a
|
||||
zap.Uint("card_id", card.ID),
|
||||
zap.String("iccid", card.ICCID),
|
||||
zap.Error(err))
|
||||
return nil
|
||||
return h.requeueCard(ctx, card.ID, constants.TaskTypePollingProtect)
|
||||
}
|
||||
h.logger.Info("网关停机成功(保护期)",
|
||||
zap.Uint("card_id", card.ID),
|
||||
@@ -1247,7 +1256,7 @@ func (h *PollingHandler) HandleProtectConsistencyCheck(ctx context.Context, t *a
|
||||
"stop_reason": constants.StopReasonProtectPeriod,
|
||||
})
|
||||
h.updateCardCache(ctx, card.ID, map[string]any{"network_status": constants.NetworkStatusOffline})
|
||||
return nil
|
||||
return h.requeueCard(ctx, card.ID, constants.TaskTypePollingProtect)
|
||||
}
|
||||
|
||||
// 检查 start 保护期:设备处于复机保护期,但卡是停机状态 → 需要复机
|
||||
@@ -1267,7 +1276,7 @@ func (h *PollingHandler) HandleProtectConsistencyCheck(ctx context.Context, t *a
|
||||
zap.Uint("card_id", card.ID),
|
||||
zap.String("iccid", card.ICCID),
|
||||
zap.Error(err))
|
||||
return nil
|
||||
return h.requeueCard(ctx, card.ID, constants.TaskTypePollingProtect)
|
||||
}
|
||||
h.logger.Info("网关复机成功(保护期)",
|
||||
zap.Uint("card_id", card.ID),
|
||||
@@ -1282,7 +1291,7 @@ func (h *PollingHandler) HandleProtectConsistencyCheck(ctx context.Context, t *a
|
||||
h.updateCardCache(ctx, card.ID, map[string]any{"network_status": constants.NetworkStatusOnline})
|
||||
}
|
||||
|
||||
return nil
|
||||
return h.requeueCard(ctx, card.ID, constants.TaskTypePollingProtect)
|
||||
}
|
||||
|
||||
// triggerFirstRealnameActivation 任务 21.3-21.4: 首次实名后触发套餐激活
|
||||
|
||||
Reference in New Issue
Block a user