All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 9m46s
【核心变更】
1. 停复机逻辑统一(StopResumeService)
- 新增 EvaluateAndAct 统一入口,封装三条件停复机判断
- 停机条件:无套餐(no_package) / 流量耗尽(traffic_exhausted) / 未实名(not_realname)
- 复机条件:stop_reason 合规 + 有套餐且未耗尽 + 已实名或行业卡
- 修复设备套餐 Bug:hasValidPackage 按 device_id 查套餐,而非仅 iot_card_id
- 设备维度停复机加幂等锁(Redis SetNX,TTL 30s),防止多卡并发重复调 Gateway
2. Redis 分片队列(PollingQueueManager)
- 新建 queue_manager.go,封装所有轮询 Redis 操作
- 16 分片 Sorted Set,Key 格式:polling:shard:{shardID}:queue:{taskType}
- Lua 脚本原子出队(ZRANGEBYSCORE + 分批 ZREM),消除竞态窗口
- 新增背压检测:队列深度超 50 万时 Scheduler 跳过该分片
- RemoveFromAllQueues 覆盖 4 种任务类型(含 protect)
3. Handler 拆分(polling_handler.go 1360行 → 5个专注文件)
- polling_base.go:共享基类(并发控制/卡缓存/重入队)
- polling_realname_handler.go:实名采集,实名 0→1 时立即触发复机
- polling_carddata_handler.go:流量采集,保留跨月边界检测逻辑
- polling_package_handler.go:套餐采集,委托 EvaluateAndAct 决策
- polling_protect_handler.go:保护期一致性检查,保护期内强制修正
4. 配置管理(PollingConfigManager)
- 新建 config_manager.go,从 scheduler.go 提取配置职责
- 内存缓存 + 5 分钟定时刷新,刷新失败保留原缓存
- 修复 getCardCondition:停机卡返回 suspended,不再错配 activated 配置
5. 渐进式初始化(CardInitializer)
- 新建 initializer.go,分批加载(每批 10 万),批次间 sleep 500ms
- 过滤 enable_polling=false 的卡,初始化完成前 Scheduler 不出队
6. 卡生命周期服务(PollingLifecycleService)
- 新建 lifecycle_service.go,替代已删除的 callbacks.go 和 api_callback.go
- OnCardCreated/OnCardEnabled/OnCardStatusChanged 入队前检查 enable_polling
7. Scheduler 精简(1000+行 → 227行)
- 保留纯调度循环:scheduleLoop + processShardSchedule + enqueueBatch
- 保留每 10 秒触发套餐过期检测和流量重置
- 移除所有 DB 操作、配置加载、卡初始化逻辑
8. 轮询管控 API(enable_polling)
- 新增 PUT /api/admin/assets/:id/polling-status 接口
- 支持对设备/卡维度开关轮询,关闭后从所有分片队列移除
9. 数据库迁移
- 000103:tb_device 新增 enable_polling 字段(boolean, NOT NULL, DEFAULT true)
- 000104:新增 suspended 轮询配置,为 activated 配置补全 protect_check_interval
【文件统计】
- 新增:19 个文件(handler × 5、polling 组件 × 4、迁移 × 3 等)
- 修改:20 个文件(bootstrap 注入、store 接口、monitoring 适配分片等)
- 删除:3 个文件(polling_handler.go、callbacks.go、api_callback.go)
Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent)
Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
196 lines
6.7 KiB
Go
196 lines
6.7 KiB
Go
package polling
|
||
|
||
import (
|
||
"context"
|
||
"math/rand"
|
||
"time"
|
||
|
||
"go.uber.org/zap"
|
||
|
||
"github.com/break/junhong_cmp_fiber/internal/model"
|
||
"github.com/break/junhong_cmp_fiber/internal/store/postgres"
|
||
"github.com/break/junhong_cmp_fiber/pkg/constants"
|
||
)
|
||
|
||
// PollingLifecycleService 卡生命周期轮询管理服务
|
||
// 替代 callbacks.go 和 api_callback.go 中的生命周期方法
|
||
// 实现 iot_card.PollingCallback 接口,两个进程(API 和 Worker)共享同一实现
|
||
// 依赖:PollingQueueManager(队列操作)+ PollingConfigManager(配置匹配)
|
||
type PollingLifecycleService struct {
|
||
queueMgr *PollingQueueManager
|
||
configMgr *PollingConfigManager
|
||
iotCardStore *postgres.IotCardStore
|
||
deviceBindingStore *postgres.DeviceSimBindingStore
|
||
deviceStore *postgres.DeviceStore
|
||
logger *zap.Logger
|
||
}
|
||
|
||
// NewPollingLifecycleService 创建卡生命周期轮询管理服务
|
||
func NewPollingLifecycleService(
|
||
queueMgr *PollingQueueManager,
|
||
configMgr *PollingConfigManager,
|
||
iotCardStore *postgres.IotCardStore,
|
||
deviceBindingStore *postgres.DeviceSimBindingStore,
|
||
deviceStore *postgres.DeviceStore,
|
||
logger *zap.Logger,
|
||
) *PollingLifecycleService {
|
||
return &PollingLifecycleService{
|
||
queueMgr: queueMgr,
|
||
configMgr: configMgr,
|
||
iotCardStore: iotCardStore,
|
||
deviceBindingStore: deviceBindingStore,
|
||
deviceStore: deviceStore,
|
||
logger: logger,
|
||
}
|
||
}
|
||
|
||
// OnCardCreated 新卡创建后初始化轮询
|
||
func (s *PollingLifecycleService) OnCardCreated(ctx context.Context, card *model.IotCard) {
|
||
if card == nil {
|
||
return
|
||
}
|
||
if !s.shouldEnqueue(ctx, card) {
|
||
s.logger.Debug("卡禁用轮询,跳过入队", zap.Uint("card_id", card.ID))
|
||
return
|
||
}
|
||
s.enqueueCard(ctx, card)
|
||
}
|
||
|
||
// OnBatchCardsCreated 批量卡创建后批量初始化轮询
|
||
func (s *PollingLifecycleService) OnBatchCardsCreated(ctx context.Context, cards []*model.IotCard) {
|
||
for _, card := range cards {
|
||
s.OnCardCreated(ctx, card)
|
||
}
|
||
}
|
||
|
||
// OnCardStatusChanged 卡状态变化后重新匹配配置并更新队列
|
||
func (s *PollingLifecycleService) OnCardStatusChanged(ctx context.Context, cardID uint) {
|
||
if err := s.queueMgr.RemoveFromAllQueues(ctx, cardID); err != nil {
|
||
s.logger.Warn("卡状态变化:从队列移除失败", zap.Uint("card_id", cardID), zap.Error(err))
|
||
}
|
||
card, err := s.iotCardStore.GetByID(ctx, cardID)
|
||
if err != nil {
|
||
s.logger.Error("卡状态变化:加载卡信息失败", zap.Uint("card_id", cardID), zap.Error(err))
|
||
return
|
||
}
|
||
if !s.shouldEnqueue(ctx, card) {
|
||
return
|
||
}
|
||
s.enqueueCard(ctx, card)
|
||
}
|
||
|
||
// OnCardDeleted 卡删除后移除所有队列并清理缓存
|
||
func (s *PollingLifecycleService) OnCardDeleted(ctx context.Context, cardID uint) {
|
||
if err := s.queueMgr.OnCardDeleted(ctx, cardID); err != nil {
|
||
s.logger.Warn("卡删除:清理队列和缓存失败", zap.Uint("card_id", cardID), zap.Error(err))
|
||
}
|
||
}
|
||
|
||
// OnCardEnabled 卡启用轮询后初始化
|
||
func (s *PollingLifecycleService) OnCardEnabled(ctx context.Context, cardID uint) {
|
||
card, err := s.iotCardStore.GetByID(ctx, cardID)
|
||
if err != nil {
|
||
s.logger.Error("卡启用:加载卡信息失败", zap.Uint("card_id", cardID), zap.Error(err))
|
||
return
|
||
}
|
||
if !s.shouldEnqueue(ctx, card) {
|
||
return
|
||
}
|
||
s.enqueueCard(ctx, card)
|
||
}
|
||
|
||
// OnCardDisabled 卡禁用轮询后移除所有队列
|
||
func (s *PollingLifecycleService) OnCardDisabled(ctx context.Context, cardID uint) {
|
||
if err := s.queueMgr.RemoveFromAllQueues(ctx, cardID); err != nil {
|
||
s.logger.Warn("卡禁用:从队列移除失败", zap.Uint("card_id", cardID), zap.Error(err))
|
||
}
|
||
}
|
||
|
||
// shouldEnqueue M3 修复:检查卡或其绑定设备是否允许轮询
|
||
// 两层检查:先查卡级 enable_polling,再查设备级 enable_polling(IsStandalone=false 时)
|
||
// 直接查 device.enable_polling,不依赖级联同步正确性,防止同步失败时生命周期事件绕过设备级禁用
|
||
func (s *PollingLifecycleService) shouldEnqueue(ctx context.Context, card *model.IotCard) bool {
|
||
if !card.EnablePolling {
|
||
return false
|
||
}
|
||
if !card.IsStandalone && s.deviceBindingStore != nil && s.deviceStore != nil {
|
||
binding, err := s.deviceBindingStore.GetActiveBindingByCardID(ctx, card.ID)
|
||
if err == nil && binding != nil {
|
||
device, devErr := s.deviceStore.GetByID(ctx, binding.DeviceID)
|
||
if devErr == nil && !device.EnablePolling {
|
||
s.logger.Debug("设备已禁用轮询,跳过卡入队",
|
||
zap.Uint("card_id", card.ID), zap.Uint("device_id", binding.DeviceID))
|
||
return false
|
||
}
|
||
}
|
||
}
|
||
return true
|
||
}
|
||
|
||
// enqueueCard 按匹配配置将卡入队分片队列
|
||
func (s *PollingLifecycleService) enqueueCard(ctx context.Context, card *model.IotCard) {
|
||
cfg := s.configMgr.MatchConfig(card)
|
||
if cfg == nil {
|
||
return
|
||
}
|
||
taskTypes := getEnabledTaskTypes(cfg)
|
||
for _, taskType := range taskTypes {
|
||
if err := s.queueMgr.Requeue(ctx, card.ID, taskType, calcInitialDelay(card, cfg, taskType)); err != nil {
|
||
s.logger.Warn("卡入队失败",
|
||
zap.Uint("card_id", card.ID), zap.String("task_type", taskType), zap.Error(err))
|
||
}
|
||
}
|
||
}
|
||
|
||
// getEnabledTaskTypes 返回配置中启用的任务类型列表
|
||
func getEnabledTaskTypes(cfg *model.PollingConfig) []string {
|
||
var types []string
|
||
if cfg.RealnameCheckInterval != nil && *cfg.RealnameCheckInterval > 0 {
|
||
types = append(types, constants.TaskTypePollingRealname)
|
||
}
|
||
if cfg.CarddataCheckInterval != nil && *cfg.CarddataCheckInterval > 0 {
|
||
types = append(types, constants.TaskTypePollingCarddata)
|
||
}
|
||
if cfg.PackageCheckInterval != nil && *cfg.PackageCheckInterval > 0 {
|
||
types = append(types, constants.TaskTypePollingPackage)
|
||
}
|
||
if cfg.ProtectCheckInterval != nil && *cfg.ProtectCheckInterval > 0 {
|
||
types = append(types, constants.TaskTypePollingProtect)
|
||
}
|
||
return types
|
||
}
|
||
|
||
// calcInitialDelay 计算新入队卡的初始检查时间(加随机抖动避免惊群)
|
||
// 抖动范围 [0, max(interval/10, 2)) 秒;下界取 2 保证 rand.Intn 有实际随机性
|
||
// (rand.Intn(1) 永远返回 0,对 interval≤10 的配置会导致零抖动)
|
||
func calcInitialDelay(_ *model.IotCard, cfg *model.PollingConfig, taskType string) time.Time {
|
||
now := time.Now()
|
||
var interval int
|
||
switch taskType {
|
||
case constants.TaskTypePollingRealname:
|
||
if cfg.RealnameCheckInterval != nil {
|
||
interval = *cfg.RealnameCheckInterval
|
||
}
|
||
case constants.TaskTypePollingCarddata:
|
||
if cfg.CarddataCheckInterval != nil {
|
||
interval = *cfg.CarddataCheckInterval
|
||
}
|
||
case constants.TaskTypePollingPackage:
|
||
if cfg.PackageCheckInterval != nil {
|
||
interval = *cfg.PackageCheckInterval
|
||
}
|
||
case constants.TaskTypePollingProtect:
|
||
if cfg.ProtectCheckInterval != nil {
|
||
interval = *cfg.ProtectCheckInterval
|
||
}
|
||
}
|
||
if interval <= 0 {
|
||
return now
|
||
}
|
||
jitterMax := interval / 10
|
||
if jitterMax < 2 {
|
||
jitterMax = 2
|
||
}
|
||
return now.Add(time.Duration(rand.Intn(jitterMax)) * time.Second)
|
||
}
|