feat: 轮询系统重构(分片队列 + 停复机统一 + Handler 拆分)
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 9m46s
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>
This commit is contained in:
118
internal/task/polling_base.go
Normal file
118
internal/task/polling_base.go
Normal file
@@ -0,0 +1,118 @@
|
||||
package task
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
"go.uber.org/zap"
|
||||
|
||||
"github.com/break/junhong_cmp_fiber/internal/polling"
|
||||
"github.com/break/junhong_cmp_fiber/internal/store/postgres"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/constants"
|
||||
)
|
||||
|
||||
// acquireConcurrencyScript 原子获取并发信号量的 Lua 脚本
|
||||
// INCR + EXPIRE 合并为单个服务端操作,消除二者之间的崩溃窗口:
|
||||
// 若 Worker 在 INCR 后、EXPIRE 前崩溃,key 将永久留在 Redis 导致计数器卡死。
|
||||
// KEYS[1]: 当前并发计数 key
|
||||
// ARGV[1]: 最大并发数;ARGV[2]: key TTL(秒)
|
||||
// 返回 -1 表示超额拒绝,>0 表示成功获取后的计数值
|
||||
var acquireConcurrencyScript = redis.NewScript(`
|
||||
local current = redis.call('INCR', KEYS[1])
|
||||
if tonumber(current) > tonumber(ARGV[1]) then
|
||||
redis.call('DECR', KEYS[1])
|
||||
return -1
|
||||
end
|
||||
redis.call('EXPIRE', KEYS[1], tonumber(ARGV[2]))
|
||||
return current
|
||||
`)
|
||||
|
||||
// PollingBase 轮询共享基类
|
||||
// 封装并发控制、卡缓存、重入队、配置间隔查询等公共方法,4 个 Handler 共享
|
||||
type PollingBase struct {
|
||||
redis *redis.Client
|
||||
queueMgr *polling.PollingQueueManager
|
||||
configMgr *polling.PollingConfigManager
|
||||
iotCardStore *postgres.IotCardStore
|
||||
logger *zap.Logger
|
||||
}
|
||||
|
||||
// NewPollingBase 创建轮询共享基类
|
||||
func NewPollingBase(
|
||||
redisClient *redis.Client,
|
||||
queueMgr *polling.PollingQueueManager,
|
||||
configMgr *polling.PollingConfigManager,
|
||||
iotCardStore *postgres.IotCardStore,
|
||||
logger *zap.Logger,
|
||||
) *PollingBase {
|
||||
return &PollingBase{
|
||||
redis: redisClient,
|
||||
queueMgr: queueMgr,
|
||||
configMgr: configMgr,
|
||||
iotCardStore: iotCardStore,
|
||||
logger: logger,
|
||||
}
|
||||
}
|
||||
|
||||
// acquireConcurrency 原子获取并发信号量
|
||||
// 使用 Lua 脚本将 INCR 与 EXPIRE 合并为单个服务端操作,消除非原子窗口:
|
||||
// 旧实现中若 Worker 在 INCR 后、EXPIRE 前崩溃,key 永久留在 Redis,计数器卡死。
|
||||
func (b *PollingBase) acquireConcurrency(ctx context.Context, taskType string) bool {
|
||||
shortType := shortTaskType(taskType)
|
||||
configKey := constants.RedisPollingConcurrencyConfigKey(shortType)
|
||||
currentKey := constants.RedisPollingConcurrencyCurrentKey(taskType)
|
||||
|
||||
maxConcurrency, err := b.redis.Get(ctx, configKey).Int()
|
||||
if err != nil {
|
||||
maxConcurrency = constants.PollingDefaultMaxConcurrency
|
||||
}
|
||||
|
||||
result, err := acquireConcurrencyScript.Run(
|
||||
ctx, b.redis, []string{currentKey},
|
||||
maxConcurrency, constants.PollingConcurrencyKeyTTL,
|
||||
).Int64()
|
||||
if err != nil {
|
||||
b.logger.Warn("获取并发计数失败,放行任务", zap.Error(err))
|
||||
return true
|
||||
}
|
||||
|
||||
return result != -1
|
||||
}
|
||||
|
||||
// releaseConcurrency 释放并发信号量
|
||||
func (b *PollingBase) releaseConcurrency(ctx context.Context, taskType string) {
|
||||
currentKey := constants.RedisPollingConcurrencyCurrentKey(taskType)
|
||||
if err := b.redis.Decr(ctx, currentKey).Err(); err != nil {
|
||||
b.logger.Warn("释放并发计数失败", zap.Error(err))
|
||||
}
|
||||
}
|
||||
|
||||
// requeueCard 将卡按匹配配置间隔重新入队分片 Sorted Set
|
||||
// ⚠️ 关键:Lua 脚本原子出队后卡已从队列移除,若并发满时直接 return 会导致卡永久丢失
|
||||
// 调用方必须在 acquireConcurrency 返回 false 时调用此方法入队后再返回
|
||||
func (b *PollingBase) requeueCard(ctx context.Context, cardID uint, taskType string) error {
|
||||
card, err := b.getCardWithCache(ctx, cardID)
|
||||
if err != nil {
|
||||
b.logger.Warn("重入队:获取卡信息失败", zap.Uint("card_id", cardID), zap.Error(err))
|
||||
// 获取卡失败时立即重入队(下次仍然尝试处理)
|
||||
return b.queueMgr.Requeue(ctx, cardID, taskType, time.Now().Add(30*time.Second))
|
||||
}
|
||||
|
||||
cfg := b.configMgr.MatchConfig(card)
|
||||
if cfg == nil {
|
||||
// 兜底:配置未加载(DB 暂时不可达)时延迟 30 秒重入队,防止卡从轮询永久消失
|
||||
b.logger.Warn("无匹配轮询配置,30秒后重入队(配置可能未加载)",
|
||||
zap.Uint("card_id", cardID), zap.String("task_type", taskType))
|
||||
return b.queueMgr.Requeue(ctx, cardID, taskType, time.Now().Add(30*time.Second))
|
||||
}
|
||||
|
||||
interval := getIntervalByTaskType(cfg, taskType)
|
||||
if interval <= 0 {
|
||||
b.logger.Debug("轮询间隔为 NULL,不入队", zap.Uint("card_id", cardID), zap.String("task_type", taskType))
|
||||
return nil
|
||||
}
|
||||
|
||||
nextCheckAt := time.Now().Add(time.Duration(interval) * time.Second)
|
||||
return b.queueMgr.Requeue(ctx, cardID, taskType, nextCheckAt)
|
||||
}
|
||||
136
internal/task/polling_card_cache.go
Normal file
136
internal/task/polling_card_cache.go
Normal file
@@ -0,0 +1,136 @@
|
||||
package task
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"go.uber.org/zap"
|
||||
|
||||
"github.com/break/junhong_cmp_fiber/internal/model"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/constants"
|
||||
)
|
||||
|
||||
// getCardWithCache 优先从 Redis 缓存获取卡信息,缓存 miss 时查 DB 并异步写缓存
|
||||
func (b *PollingBase) getCardWithCache(ctx context.Context, cardID uint) (*model.IotCard, error) {
|
||||
key := constants.RedisPollingCardInfoKey(cardID)
|
||||
|
||||
result, err := b.redis.HGetAll(ctx, key).Result()
|
||||
if err == nil && len(result) > 0 {
|
||||
return parseCardFromCache(cardID, result), nil
|
||||
}
|
||||
|
||||
card, err := b.iotCardStore.GetByID(ctx, cardID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
go writeCardToCache(b, key, card)
|
||||
return card, nil
|
||||
}
|
||||
|
||||
// updateCardCache 更新 Redis 卡信息缓存中的指定字段
|
||||
func (b *PollingBase) updateCardCache(ctx context.Context, cardID uint, updates map[string]any) {
|
||||
key := constants.RedisPollingCardInfoKey(cardID)
|
||||
args := make([]any, 0, len(updates)*2)
|
||||
for k, v := range updates {
|
||||
args = append(args, k, v)
|
||||
}
|
||||
if len(args) > 0 {
|
||||
if err := b.redis.HSet(ctx, key, args...).Err(); err != nil {
|
||||
b.logger.Warn("更新卡缓存失败", zap.Uint("card_id", cardID), zap.Error(err))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// parseCardFromCache 从 Redis Hash 反序列化卡信息
|
||||
func parseCardFromCache(cardID uint, result map[string]string) *model.IotCard {
|
||||
card := &model.IotCard{}
|
||||
card.ID = cardID
|
||||
if v, ok := result["iccid"]; ok {
|
||||
card.ICCID = v
|
||||
}
|
||||
if v, ok := result["card_category"]; ok {
|
||||
card.CardCategory = v
|
||||
}
|
||||
if v, ok := result["real_name_status"]; ok {
|
||||
if status, err := strconv.Atoi(v); err == nil {
|
||||
card.RealNameStatus = status
|
||||
}
|
||||
}
|
||||
if v, ok := result["network_status"]; ok {
|
||||
if status, err := strconv.Atoi(v); err == nil {
|
||||
card.NetworkStatus = status
|
||||
}
|
||||
}
|
||||
if v, ok := result["carrier_id"]; ok {
|
||||
if id, err := strconv.ParseUint(v, 10, 64); err == nil {
|
||||
card.CarrierID = uint(id)
|
||||
}
|
||||
}
|
||||
if v, ok := result["current_month_usage_mb"]; ok {
|
||||
if usage, err := strconv.ParseFloat(v, 64); err == nil {
|
||||
card.CurrentMonthUsageMB = usage
|
||||
}
|
||||
}
|
||||
if v, ok := result["last_gateway_reading_mb"]; ok {
|
||||
if reading, err := strconv.ParseFloat(v, 64); err == nil {
|
||||
card.LastGatewayReadingMB = reading
|
||||
}
|
||||
}
|
||||
if v, ok := result["data_usage_mb"]; ok {
|
||||
if usage, err := strconv.ParseInt(v, 10, 64); err == nil {
|
||||
card.DataUsageMB = usage
|
||||
}
|
||||
}
|
||||
if v, ok := result["stop_reason"]; ok {
|
||||
card.StopReason = v
|
||||
}
|
||||
if v, ok := result["is_standalone"]; ok {
|
||||
card.IsStandalone = v == "1" || v == "true"
|
||||
} else {
|
||||
card.IsStandalone = true
|
||||
}
|
||||
if v, ok := result["series_id"]; ok {
|
||||
if id, err := strconv.ParseUint(v, 10, 64); err == nil {
|
||||
seriesID := uint(id)
|
||||
card.SeriesID = &seriesID
|
||||
}
|
||||
}
|
||||
if v, ok := result["current_month_start_date"]; ok {
|
||||
if ts, err := strconv.ParseInt(v, 10, 64); err == nil {
|
||||
t := time.Unix(ts, 0)
|
||||
card.CurrentMonthStartDate = &t
|
||||
}
|
||||
}
|
||||
return card
|
||||
}
|
||||
|
||||
// writeCardToCache 将卡信息异步写入 Redis 缓存(7 天 TTL)
|
||||
func writeCardToCache(b *PollingBase, key string, card *model.IotCard) {
|
||||
cacheCtx := context.Background()
|
||||
cacheData := map[string]any{
|
||||
"id": card.ID,
|
||||
"iccid": card.ICCID,
|
||||
"card_category": card.CardCategory,
|
||||
"real_name_status": card.RealNameStatus,
|
||||
"network_status": card.NetworkStatus,
|
||||
"carrier_id": card.CarrierID,
|
||||
"current_month_usage_mb": card.CurrentMonthUsageMB,
|
||||
"last_gateway_reading_mb": card.LastGatewayReadingMB,
|
||||
"data_usage_mb": card.DataUsageMB,
|
||||
"stop_reason": card.StopReason,
|
||||
"is_standalone": boolToStr(card.IsStandalone),
|
||||
"cached_at": time.Now().Unix(),
|
||||
}
|
||||
if card.SeriesID != nil {
|
||||
cacheData["series_id"] = *card.SeriesID
|
||||
}
|
||||
if card.CurrentMonthStartDate != nil {
|
||||
cacheData["current_month_start_date"] = card.CurrentMonthStartDate.Unix()
|
||||
}
|
||||
pipe := b.redis.Pipeline()
|
||||
pipe.HSet(cacheCtx, key, cacheData)
|
||||
pipe.Expire(cacheCtx, key, 7*24*time.Hour)
|
||||
_, _ = pipe.Exec(cacheCtx)
|
||||
}
|
||||
188
internal/task/polling_carddata_handler.go
Normal file
188
internal/task/polling_carddata_handler.go
Normal file
@@ -0,0 +1,188 @@
|
||||
package task
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/hibiken/asynq"
|
||||
"go.uber.org/zap"
|
||||
"gorm.io/gorm"
|
||||
|
||||
"github.com/break/junhong_cmp_fiber/internal/gateway"
|
||||
"github.com/break/junhong_cmp_fiber/internal/model"
|
||||
iot_card_svc "github.com/break/junhong_cmp_fiber/internal/service/iot_card"
|
||||
packagepkg "github.com/break/junhong_cmp_fiber/internal/service/package"
|
||||
"github.com/break/junhong_cmp_fiber/internal/store/postgres"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/constants"
|
||||
)
|
||||
|
||||
// PollingCarddataHandler 流量检查任务处理器
|
||||
// 职责:调 Gateway 查流量 → 写 DB → 扣减套餐流量 → 调 EvaluateAndAct 判断停复机
|
||||
// 不做:直接停复机决策,委托给 StopResumeService.EvaluateAndAct
|
||||
type PollingCarddataHandler struct {
|
||||
base *PollingBase
|
||||
gateway *gateway.Client
|
||||
iotCardStore *postgres.IotCardStore
|
||||
carrierStore *postgres.CarrierStore
|
||||
usageService *packagepkg.UsageService
|
||||
stopResumeSvc iot_card_svc.StopResumeServiceInterface
|
||||
}
|
||||
|
||||
// NewPollingCarddataHandler 创建流量检查任务处理器
|
||||
func NewPollingCarddataHandler(
|
||||
base *PollingBase,
|
||||
gw *gateway.Client,
|
||||
iotCardStore *postgres.IotCardStore,
|
||||
carrierStore *postgres.CarrierStore,
|
||||
usageService *packagepkg.UsageService,
|
||||
stopResumeSvc iot_card_svc.StopResumeServiceInterface,
|
||||
) *PollingCarddataHandler {
|
||||
return &PollingCarddataHandler{
|
||||
base: base,
|
||||
gateway: gw,
|
||||
iotCardStore: iotCardStore,
|
||||
carrierStore: carrierStore,
|
||||
usageService: usageService,
|
||||
stopResumeSvc: stopResumeSvc,
|
||||
}
|
||||
}
|
||||
|
||||
// Handle 处理流量检查任务
|
||||
func (h *PollingCarddataHandler) Handle(ctx context.Context, t *asynq.Task) error {
|
||||
startTime := time.Now()
|
||||
|
||||
cardID, ok := parseTaskPayload(t.Payload(), h.base.logger)
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
|
||||
if !h.base.acquireConcurrency(ctx, constants.TaskTypePollingCarddata) {
|
||||
h.base.logger.Debug("并发已满,重新入队", zap.Uint("card_id", cardID))
|
||||
return h.base.requeueCard(ctx, cardID, constants.TaskTypePollingCarddata)
|
||||
}
|
||||
defer h.base.releaseConcurrency(ctx, constants.TaskTypePollingCarddata)
|
||||
|
||||
card, err := h.base.getCardWithCache(ctx, cardID)
|
||||
if err != nil {
|
||||
if isNotFound(err) {
|
||||
return nil
|
||||
}
|
||||
h.base.logger.Error("获取卡信息失败", zap.Uint("card_id", cardID), zap.Error(err))
|
||||
h.base.updateStats(ctx, constants.TaskTypePollingCarddata, false, time.Since(startTime))
|
||||
return h.base.requeueCard(ctx, cardID, constants.TaskTypePollingCarddata)
|
||||
}
|
||||
|
||||
var gatewayFlowMB float64
|
||||
if h.gateway != nil {
|
||||
result, gwErr := h.gateway.QueryFlow(ctx, &gateway.FlowQueryReq{CardNo: card.ICCID})
|
||||
if gwErr != nil {
|
||||
h.base.logger.Warn("查询流量失败",
|
||||
zap.Uint("card_id", cardID), zap.String("iccid", card.ICCID), zap.Error(gwErr))
|
||||
h.base.updateStats(ctx, constants.TaskTypePollingCarddata, false, time.Since(startTime))
|
||||
return h.base.requeueCard(ctx, cardID, constants.TaskTypePollingCarddata)
|
||||
}
|
||||
gatewayFlowMB = result.Used
|
||||
} else {
|
||||
gatewayFlowMB = card.CurrentMonthUsageMB
|
||||
}
|
||||
|
||||
now := time.Now()
|
||||
resetDay := h.carrierStore.GetDataResetDay(ctx, card.CarrierID)
|
||||
updates, flowIncrementMB, isCrossMonth := h.calculateFlowUpdates(card, gatewayFlowMB, now, resetDay)
|
||||
updates["last_data_check_at"] = now
|
||||
|
||||
if updateErr := h.iotCardStore.UpdateFields(ctx, cardID, updates); updateErr != nil {
|
||||
h.base.logger.Error("更新卡流量信息失败", zap.Uint("card_id", cardID), zap.Error(updateErr))
|
||||
}
|
||||
|
||||
go h.base.insertDataUsageRecord(context.Background(), cardID, flowIncrementMB, now)
|
||||
|
||||
cacheUpdates := map[string]any{"last_gateway_reading_mb": gatewayFlowMB}
|
||||
if isCrossMonth {
|
||||
// 跨月时同步更新缓存中的本月开始日期和本月用量,避免下次从缓存读取时再次误判跨月
|
||||
currentMonthStart := time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, now.Location())
|
||||
cacheUpdates["current_month_start_date"] = currentMonthStart.Unix()
|
||||
if flowIncrementMB > 0 {
|
||||
cacheUpdates["current_month_usage_mb"] = flowIncrementMB
|
||||
} else {
|
||||
cacheUpdates["current_month_usage_mb"] = float64(0)
|
||||
}
|
||||
}
|
||||
h.base.updateCardCache(ctx, cardID, cacheUpdates)
|
||||
|
||||
if flowIncrementMB > 0 && h.usageService != nil {
|
||||
if deductErr := h.usageService.DeductDataUsage(ctx, "iot_card", cardID, int64(flowIncrementMB)); deductErr != nil {
|
||||
h.base.logger.Warn("套餐流量扣减失败",
|
||||
zap.Uint("card_id", cardID), zap.Float64("increment_mb", flowIncrementMB), zap.Error(deductErr))
|
||||
}
|
||||
}
|
||||
|
||||
if h.stopResumeSvc != nil {
|
||||
freshCard, loadErr := h.iotCardStore.GetByID(ctx, cardID)
|
||||
if loadErr == nil {
|
||||
if evalErr := h.stopResumeSvc.EvaluateAndAct(ctx, freshCard); evalErr != nil {
|
||||
h.base.logger.Warn("流量检查后停复机评估失败",
|
||||
zap.Uint("card_id", cardID), zap.Error(evalErr))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
h.base.updateStats(ctx, constants.TaskTypePollingCarddata, true, time.Since(startTime))
|
||||
return h.base.requeueCard(ctx, cardID, constants.TaskTypePollingCarddata)
|
||||
}
|
||||
|
||||
// calculateFlowUpdates 计算流量更新字段、增量和是否跨月
|
||||
// 决策13:完整迁移跨月流量边界检测逻辑(月份切换检测、上月总量保存、当月计数器重置)
|
||||
// 第三个返回值 isCrossMonth 供调用方同步更新 Redis 缓存,避免下次误判跨月
|
||||
func (h *PollingCarddataHandler) calculateFlowUpdates(card *model.IotCard, gatewayFlowMB float64, now time.Time, resetDay int) (map[string]any, float64, bool) {
|
||||
updates := make(map[string]any)
|
||||
|
||||
increment := gatewayFlowMB - card.LastGatewayReadingMB
|
||||
if increment < 0 {
|
||||
if isResetWindow(now, resetDay) {
|
||||
increment = gatewayFlowMB
|
||||
h.base.logger.Info("检测到上游运营商重置",
|
||||
zap.Uint("card_id", card.ID),
|
||||
zap.Float64("gateway_flow", gatewayFlowMB),
|
||||
zap.Float64("last_reading", card.LastGatewayReadingMB),
|
||||
zap.Int("reset_day", resetDay))
|
||||
} else {
|
||||
h.base.logger.Warn("流量异常:非重置日出现值下降",
|
||||
zap.Uint("card_id", card.ID),
|
||||
zap.Float64("gateway_flow", gatewayFlowMB),
|
||||
zap.Float64("last_reading", card.LastGatewayReadingMB))
|
||||
increment = 0
|
||||
}
|
||||
}
|
||||
|
||||
updates["last_gateway_reading_mb"] = gatewayFlowMB
|
||||
|
||||
currentMonthStart := time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, now.Location())
|
||||
isCrossMonth := card.CurrentMonthStartDate == nil ||
|
||||
card.CurrentMonthStartDate.Before(currentMonthStart)
|
||||
|
||||
if isCrossMonth {
|
||||
h.base.logger.Info("检测到跨月,重置流量计数",
|
||||
zap.Uint("card_id", card.ID),
|
||||
zap.Float64("last_month_total", card.CurrentMonthUsageMB))
|
||||
updates["last_month_total_mb"] = card.CurrentMonthUsageMB
|
||||
updates["current_month_start_date"] = currentMonthStart
|
||||
if increment > 0 {
|
||||
updates["current_month_usage_mb"] = increment
|
||||
} else {
|
||||
updates["current_month_usage_mb"] = float64(0)
|
||||
}
|
||||
} else if increment > 0 {
|
||||
updates["current_month_usage_mb"] = gorm.Expr("current_month_usage_mb + ?", increment)
|
||||
}
|
||||
|
||||
if increment > 0 {
|
||||
updates["data_usage_mb"] = gorm.Expr("data_usage_mb + ?", int64(increment))
|
||||
}
|
||||
|
||||
if card.CurrentMonthStartDate == nil {
|
||||
updates["current_month_start_date"] = currentMonthStart
|
||||
}
|
||||
|
||||
return updates, increment, isCrossMonth
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
76
internal/task/polling_package_handler.go
Normal file
76
internal/task/polling_package_handler.go
Normal file
@@ -0,0 +1,76 @@
|
||||
package task
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/hibiken/asynq"
|
||||
"go.uber.org/zap"
|
||||
|
||||
iot_card_svc "github.com/break/junhong_cmp_fiber/internal/service/iot_card"
|
||||
"github.com/break/junhong_cmp_fiber/internal/store/postgres"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/constants"
|
||||
)
|
||||
|
||||
// PollingPackageHandler 套餐检查任务处理器
|
||||
// 职责:触发 EvaluateAndAct 检查套餐状态并判断停复机
|
||||
// 不做:直接停复机决策,委托给 StopResumeService.EvaluateAndAct
|
||||
// 注:套餐状态由 PackageActivationHandler/DataResetHandler/UsageService 维护,
|
||||
// 本 Handler 无需查 Gateway,只负责周期性触发再评估。
|
||||
type PollingPackageHandler struct {
|
||||
base *PollingBase
|
||||
iotCardStore *postgres.IotCardStore
|
||||
stopResumeSvc iot_card_svc.StopResumeServiceInterface
|
||||
}
|
||||
|
||||
// NewPollingPackageHandler 创建套餐检查任务处理器
|
||||
func NewPollingPackageHandler(
|
||||
base *PollingBase,
|
||||
iotCardStore *postgres.IotCardStore,
|
||||
stopResumeSvc iot_card_svc.StopResumeServiceInterface,
|
||||
) *PollingPackageHandler {
|
||||
return &PollingPackageHandler{
|
||||
base: base,
|
||||
iotCardStore: iotCardStore,
|
||||
stopResumeSvc: stopResumeSvc,
|
||||
}
|
||||
}
|
||||
|
||||
// Handle 处理套餐检查任务
|
||||
func (h *PollingPackageHandler) Handle(ctx context.Context, t *asynq.Task) error {
|
||||
startTime := time.Now()
|
||||
|
||||
cardID, ok := parseTaskPayload(t.Payload(), h.base.logger)
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
|
||||
if !h.base.acquireConcurrency(ctx, constants.TaskTypePollingPackage) {
|
||||
h.base.logger.Debug("并发已满,重新入队", zap.Uint("card_id", cardID))
|
||||
return h.base.requeueCard(ctx, cardID, constants.TaskTypePollingPackage)
|
||||
}
|
||||
defer h.base.releaseConcurrency(ctx, constants.TaskTypePollingPackage)
|
||||
|
||||
if _, cacheErr := h.base.getCardWithCache(ctx, cardID); cacheErr != nil {
|
||||
if isNotFound(cacheErr) {
|
||||
return nil
|
||||
}
|
||||
h.base.logger.Error("获取卡信息失败", zap.Uint("card_id", cardID), zap.Error(cacheErr))
|
||||
h.base.updateStats(ctx, constants.TaskTypePollingPackage, false, time.Since(startTime))
|
||||
return h.base.requeueCard(ctx, cardID, constants.TaskTypePollingPackage)
|
||||
}
|
||||
|
||||
if h.stopResumeSvc != nil {
|
||||
// EvaluateAndAct 需要完整新鲜数据(含 DeviceID),从 DB 获取
|
||||
freshCard, loadErr := h.iotCardStore.GetByID(ctx, cardID)
|
||||
if loadErr == nil {
|
||||
if evalErr := h.stopResumeSvc.EvaluateAndAct(ctx, freshCard); evalErr != nil {
|
||||
h.base.logger.Warn("套餐检查后停复机评估失败",
|
||||
zap.Uint("card_id", cardID), zap.Error(evalErr))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
h.base.updateStats(ctx, constants.TaskTypePollingPackage, true, time.Since(startTime))
|
||||
return h.base.requeueCard(ctx, cardID, constants.TaskTypePollingPackage)
|
||||
}
|
||||
151
internal/task/polling_protect_handler.go
Normal file
151
internal/task/polling_protect_handler.go
Normal file
@@ -0,0 +1,151 @@
|
||||
package task
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/hibiken/asynq"
|
||||
"go.uber.org/zap"
|
||||
|
||||
"github.com/break/junhong_cmp_fiber/internal/gateway"
|
||||
iot_card_svc "github.com/break/junhong_cmp_fiber/internal/service/iot_card"
|
||||
"github.com/break/junhong_cmp_fiber/internal/store/postgres"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/constants"
|
||||
)
|
||||
|
||||
// PollingProtectHandler 保护期一致性检查任务处理器
|
||||
// 保护期内:直接调 Gateway 强制停/复机(绕过 EvaluateAndAct 三条件判断)
|
||||
// 保护期结束:调 EvaluateAndAct 重新评估正常停复机条件
|
||||
// 两种路径不可混淆:保护期内=强制修正;保护期结束=重新评估
|
||||
type PollingProtectHandler struct {
|
||||
base *PollingBase
|
||||
gateway *gateway.Client
|
||||
iotCardStore *postgres.IotCardStore
|
||||
deviceSimBindingStore *postgres.DeviceSimBindingStore
|
||||
stopResumeSvc iot_card_svc.StopResumeServiceInterface
|
||||
}
|
||||
|
||||
// NewPollingProtectHandler 创建保护期一致性检查任务处理器
|
||||
func NewPollingProtectHandler(
|
||||
base *PollingBase,
|
||||
gw *gateway.Client,
|
||||
iotCardStore *postgres.IotCardStore,
|
||||
deviceSimBindingStore *postgres.DeviceSimBindingStore,
|
||||
stopResumeSvc iot_card_svc.StopResumeServiceInterface,
|
||||
) *PollingProtectHandler {
|
||||
return &PollingProtectHandler{
|
||||
base: base,
|
||||
gateway: gw,
|
||||
iotCardStore: iotCardStore,
|
||||
deviceSimBindingStore: deviceSimBindingStore,
|
||||
stopResumeSvc: stopResumeSvc,
|
||||
}
|
||||
}
|
||||
|
||||
// Handle 处理保护期一致性检查任务
|
||||
func (h *PollingProtectHandler) Handle(ctx context.Context, t *asynq.Task) error {
|
||||
startTime := time.Now()
|
||||
|
||||
cardID, ok := parseTaskPayload(t.Payload(), h.base.logger)
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
|
||||
if !h.base.acquireConcurrency(ctx, constants.TaskTypePollingProtect) {
|
||||
h.base.logger.Debug("并发已满,重新入队", zap.Uint("card_id", cardID))
|
||||
return h.base.requeueCard(ctx, cardID, constants.TaskTypePollingProtect)
|
||||
}
|
||||
defer h.base.releaseConcurrency(ctx, constants.TaskTypePollingProtect)
|
||||
|
||||
card, err := h.iotCardStore.GetByID(ctx, cardID)
|
||||
if err != nil {
|
||||
if isNotFound(err) {
|
||||
return nil
|
||||
}
|
||||
h.base.logger.Error("查询卡信息失败", zap.Uint("card_id", cardID), zap.Error(err))
|
||||
return h.base.requeueCard(ctx, cardID, constants.TaskTypePollingProtect)
|
||||
}
|
||||
|
||||
// 独立卡(未绑设备)跳过保护期检查
|
||||
if card.IsStandalone {
|
||||
return h.base.requeueCard(ctx, cardID, constants.TaskTypePollingProtect)
|
||||
}
|
||||
|
||||
// 未实名卡跳过保护期检查
|
||||
if card.RealNameStatus != constants.RealNameStatusVerified {
|
||||
return h.base.requeueCard(ctx, cardID, constants.TaskTypePollingProtect)
|
||||
}
|
||||
|
||||
binding, err := h.deviceSimBindingStore.GetActiveBindingByCardID(ctx, card.ID)
|
||||
if err != nil || binding == nil {
|
||||
return h.base.requeueCard(ctx, cardID, constants.TaskTypePollingProtect)
|
||||
}
|
||||
deviceID := binding.DeviceID
|
||||
|
||||
stopProtect := h.base.redis.Exists(ctx, constants.RedisDeviceProtectKey(deviceID, "stop")).Val() > 0
|
||||
startProtect := h.base.redis.Exists(ctx, constants.RedisDeviceProtectKey(deviceID, "start")).Val() > 0
|
||||
|
||||
switch {
|
||||
case stopProtect && card.NetworkStatus == constants.NetworkStatusOnline:
|
||||
// 保护期内:停机保护期发现开机卡 → 强制停机(绕过 EvaluateAndAct)
|
||||
h.base.logger.Info("保护期一致性:停机保护期内发现开机卡,强制停机",
|
||||
zap.Uint("card_id", card.ID), zap.Uint("device_id", deviceID))
|
||||
if h.gateway == nil {
|
||||
break
|
||||
}
|
||||
if err := h.gateway.StopCard(ctx, &gateway.CardOperationReq{CardNo: card.ICCID}); err != nil {
|
||||
h.base.logger.Error("保护期强制停机失败",
|
||||
zap.Uint("card_id", card.ID), zap.Error(err))
|
||||
h.base.updateStats(ctx, constants.TaskTypePollingProtect, false, time.Since(startTime))
|
||||
return h.base.requeueCard(ctx, cardID, constants.TaskTypePollingProtect)
|
||||
}
|
||||
if updateErr := h.iotCardStore.UpdateFields(ctx, cardID, map[string]any{
|
||||
"network_status": constants.NetworkStatusOffline,
|
||||
"stopped_at": time.Now(),
|
||||
"stop_reason": constants.StopReasonProtectPeriod,
|
||||
}); updateErr != nil {
|
||||
h.base.logger.Warn("保护期停机 DB 更新失败", zap.Uint("card_id", cardID), zap.Error(updateErr))
|
||||
}
|
||||
h.base.updateCardCache(ctx, cardID, map[string]any{"network_status": constants.NetworkStatusOffline})
|
||||
|
||||
case startProtect && card.NetworkStatus == constants.NetworkStatusOffline:
|
||||
// 保护期内:复机保护期发现停机卡 → 强制复机(绕过 EvaluateAndAct)
|
||||
h.base.logger.Info("保护期一致性:复机保护期内发现停机卡,强制复机",
|
||||
zap.Uint("card_id", card.ID), zap.Uint("device_id", deviceID))
|
||||
if h.gateway == nil {
|
||||
break
|
||||
}
|
||||
if err := h.gateway.StartCard(ctx, &gateway.CardOperationReq{CardNo: card.ICCID}); err != nil {
|
||||
h.base.logger.Error("保护期强制复机失败",
|
||||
zap.Uint("card_id", card.ID), zap.Error(err))
|
||||
h.base.updateStats(ctx, constants.TaskTypePollingProtect, false, time.Since(startTime))
|
||||
return h.base.requeueCard(ctx, cardID, constants.TaskTypePollingProtect)
|
||||
}
|
||||
if updateErr := h.iotCardStore.UpdateFields(ctx, cardID, map[string]any{
|
||||
"network_status": constants.NetworkStatusOnline,
|
||||
"resumed_at": time.Now(),
|
||||
"stop_reason": "",
|
||||
}); updateErr != nil {
|
||||
h.base.logger.Warn("保护期复机 DB 更新失败", zap.Uint("card_id", cardID), zap.Error(updateErr))
|
||||
}
|
||||
h.base.updateCardCache(ctx, cardID, map[string]any{"network_status": constants.NetworkStatusOnline})
|
||||
|
||||
case !stopProtect && !startProtect:
|
||||
// 保护期结束:调 EvaluateAndAct 重新评估正常停复机条件
|
||||
if h.stopResumeSvc != nil {
|
||||
if evalErr := h.stopResumeSvc.EvaluateAndAct(ctx, card); evalErr != nil {
|
||||
h.base.logger.Warn("保护期结束后停复机评估失败",
|
||||
zap.Uint("card_id", cardID), zap.Error(evalErr))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if updateErr := h.iotCardStore.UpdateFields(ctx, cardID, map[string]any{
|
||||
"last_protect_check_at": time.Now(),
|
||||
}); updateErr != nil {
|
||||
h.base.logger.Warn("更新保护期检查时间失败", zap.Uint("card_id", cardID), zap.Error(updateErr))
|
||||
}
|
||||
|
||||
h.base.updateStats(ctx, constants.TaskTypePollingProtect, true, time.Since(startTime))
|
||||
return h.base.requeueCard(ctx, cardID, constants.TaskTypePollingProtect)
|
||||
}
|
||||
172
internal/task/polling_realname_handler.go
Normal file
172
internal/task/polling_realname_handler.go
Normal file
@@ -0,0 +1,172 @@
|
||||
package task
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/bytedance/sonic"
|
||||
"github.com/hibiken/asynq"
|
||||
"go.uber.org/zap"
|
||||
|
||||
"github.com/break/junhong_cmp_fiber/internal/gateway"
|
||||
iot_card_svc "github.com/break/junhong_cmp_fiber/internal/service/iot_card"
|
||||
"github.com/break/junhong_cmp_fiber/internal/store/postgres"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/constants"
|
||||
)
|
||||
|
||||
// PollingRealnameHandler 实名检查任务处理器
|
||||
// 职责:调 Gateway 查实名状态 → 写 DB → 触发首次实名激活任务
|
||||
// 不做:停复机决策(实名状态 0→1 时通过 EvaluateAndAct 触发 not_realname 复机)
|
||||
type PollingRealnameHandler struct {
|
||||
base *PollingBase
|
||||
gateway *gateway.Client
|
||||
iotCardStore *postgres.IotCardStore
|
||||
asynqClient *asynq.Client
|
||||
stopResumeSvc iot_card_svc.StopResumeServiceInterface
|
||||
}
|
||||
|
||||
// NewPollingRealnameHandler 创建实名检查任务处理器
|
||||
func NewPollingRealnameHandler(
|
||||
base *PollingBase,
|
||||
gw *gateway.Client,
|
||||
iotCardStore *postgres.IotCardStore,
|
||||
asynqClient *asynq.Client,
|
||||
stopResumeSvc iot_card_svc.StopResumeServiceInterface,
|
||||
) *PollingRealnameHandler {
|
||||
return &PollingRealnameHandler{
|
||||
base: base,
|
||||
gateway: gw,
|
||||
iotCardStore: iotCardStore,
|
||||
asynqClient: asynqClient,
|
||||
stopResumeSvc: stopResumeSvc,
|
||||
}
|
||||
}
|
||||
|
||||
// Handle 处理实名检查任务
|
||||
func (h *PollingRealnameHandler) Handle(ctx context.Context, t *asynq.Task) error {
|
||||
startTime := time.Now()
|
||||
|
||||
cardID, ok := parseTaskPayload(t.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)
|
||||
|
||||
card, err := h.base.getCardWithCache(ctx, cardID)
|
||||
if err != nil {
|
||||
if isNotFound(err) {
|
||||
return nil
|
||||
}
|
||||
h.base.logger.Error("获取卡信息失败", zap.Uint("card_id", cardID), zap.Error(err))
|
||||
h.base.updateStats(ctx, constants.TaskTypePollingRealname, false, time.Since(startTime))
|
||||
return h.base.requeueCard(ctx, cardID, constants.TaskTypePollingRealname)
|
||||
}
|
||||
|
||||
if card.CardCategory == constants.CardCategoryIndustry {
|
||||
return h.base.requeueCard(ctx, cardID, constants.TaskTypePollingRealname)
|
||||
}
|
||||
|
||||
var newStatus int
|
||||
if h.gateway != nil {
|
||||
result, gwErr := h.gateway.QueryRealnameStatus(ctx, &gateway.CardStatusReq{CardNo: card.ICCID})
|
||||
if gwErr != nil {
|
||||
h.base.logger.Warn("查询实名状态失败",
|
||||
zap.Uint("card_id", cardID), zap.String("iccid", card.ICCID), zap.Error(gwErr))
|
||||
h.base.updateStats(ctx, constants.TaskTypePollingRealname, false, time.Since(startTime))
|
||||
return h.base.requeueCard(ctx, cardID, constants.TaskTypePollingRealname)
|
||||
}
|
||||
if result.RealStatus {
|
||||
newStatus = constants.RealNameStatusVerified
|
||||
} else {
|
||||
newStatus = constants.RealNameStatusNotVerified
|
||||
}
|
||||
} else {
|
||||
newStatus = card.RealNameStatus
|
||||
}
|
||||
|
||||
statusChanged := newStatus != card.RealNameStatus
|
||||
isFirstRealname := statusChanged &&
|
||||
card.RealNameStatus != constants.RealNameStatusVerified &&
|
||||
newStatus == constants.RealNameStatusVerified
|
||||
|
||||
now := time.Now()
|
||||
fields := map[string]any{"last_real_name_check_at": now}
|
||||
if statusChanged {
|
||||
fields["real_name_status"] = newStatus
|
||||
}
|
||||
if isFirstRealname {
|
||||
fields["first_realname_at"] = now
|
||||
}
|
||||
if err := h.iotCardStore.UpdateFields(ctx, cardID, fields); err != nil {
|
||||
h.base.logger.Error("更新卡实名信息失败", zap.Uint("card_id", cardID), zap.Error(err))
|
||||
}
|
||||
|
||||
if statusChanged {
|
||||
h.base.updateCardCache(ctx, cardID, map[string]any{"real_name_status": newStatus})
|
||||
h.base.logger.Info("实名状态已变化",
|
||||
zap.Uint("card_id", cardID),
|
||||
zap.Int("old_status", card.RealNameStatus),
|
||||
zap.Int("new_status", newStatus))
|
||||
|
||||
if isFirstRealname {
|
||||
// 0→1:首次实名,触发套餐激活并立即复机评估(不等待下一个 carddata/package 周期)
|
||||
h.triggerFirstRealnameActivation(ctx, cardID)
|
||||
if h.stopResumeSvc != nil {
|
||||
freshCard, loadErr := h.iotCardStore.GetByID(ctx, cardID)
|
||||
if loadErr == nil {
|
||||
if evalErr := h.stopResumeSvc.EvaluateAndAct(ctx, freshCard); evalErr != nil {
|
||||
h.base.logger.Warn("实名完成后触发复机评估失败",
|
||||
zap.Uint("card_id", cardID), zap.Error(evalErr))
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if newStatus == constants.RealNameStatusNotVerified {
|
||||
// 1→0:实名逆转,立即触发停机评估(不等待下一个 carddata/package 周期,否则最长延迟 1 小时)
|
||||
h.base.logger.Warn("检测到实名逆转,立即触发停机评估",
|
||||
zap.Uint("card_id", cardID))
|
||||
if h.stopResumeSvc != nil {
|
||||
freshCard, loadErr := h.iotCardStore.GetByID(ctx, cardID)
|
||||
if loadErr == nil {
|
||||
if evalErr := h.stopResumeSvc.EvaluateAndAct(ctx, freshCard); evalErr != nil {
|
||||
h.base.logger.Warn("实名逆转后触发停机评估失败",
|
||||
zap.Uint("card_id", cardID), zap.Error(evalErr))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
h.base.updateStats(ctx, constants.TaskTypePollingRealname, true, time.Since(startTime))
|
||||
return h.base.requeueCard(ctx, cardID, constants.TaskTypePollingRealname)
|
||||
}
|
||||
|
||||
// triggerFirstRealnameActivation 首次实名后触发套餐激活任务
|
||||
func (h *PollingRealnameHandler) triggerFirstRealnameActivation(ctx context.Context, cardID uint) {
|
||||
if h.asynqClient == nil {
|
||||
return
|
||||
}
|
||||
payload := map[string]any{
|
||||
"carrier_type": "iot_card",
|
||||
"carrier_id": cardID,
|
||||
"activation_type": "realname",
|
||||
"timestamp": time.Now().Unix(),
|
||||
}
|
||||
payloadBytes, err := sonic.Marshal(payload)
|
||||
if err != nil {
|
||||
h.base.logger.Warn("序列化首次实名激活任务载荷失败", zap.Uint("card_id", cardID), zap.Error(err))
|
||||
return
|
||||
}
|
||||
task := asynq.NewTask(constants.TaskTypePackageFirstActivation, payloadBytes,
|
||||
asynq.MaxRetry(3),
|
||||
asynq.Timeout(30*time.Second),
|
||||
asynq.Queue(constants.QueueDefault),
|
||||
)
|
||||
if _, err := h.asynqClient.EnqueueContext(ctx, task); err != nil {
|
||||
h.base.logger.Warn("提交首次实名激活任务失败", zap.Uint("card_id", cardID), zap.Error(err))
|
||||
}
|
||||
}
|
||||
39
internal/task/polling_stats.go
Normal file
39
internal/task/polling_stats.go
Normal file
@@ -0,0 +1,39 @@
|
||||
package task
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"go.uber.org/zap"
|
||||
|
||||
"github.com/break/junhong_cmp_fiber/pkg/constants"
|
||||
)
|
||||
|
||||
// updateStats 更新监控统计(成功/失败计数 + 耗时)
|
||||
func (b *PollingBase) updateStats(ctx context.Context, taskType string, success bool, duration time.Duration) {
|
||||
key := constants.RedisPollingStatsKey(taskType)
|
||||
pipe := b.redis.Pipeline()
|
||||
if success {
|
||||
pipe.HIncrBy(ctx, key, "success_count_1h", 1)
|
||||
} else {
|
||||
pipe.HIncrBy(ctx, key, "failure_count_1h", 1)
|
||||
}
|
||||
pipe.HIncrBy(ctx, key, "total_duration_1h", duration.Milliseconds())
|
||||
pipe.Expire(ctx, key, 2*time.Hour)
|
||||
_, _ = pipe.Exec(ctx)
|
||||
}
|
||||
|
||||
// insertDataUsageRecord 写入日流量缓冲到 Redis(INCRBYFLOAT + 48h TTL)
|
||||
func (b *PollingBase) insertDataUsageRecord(ctx context.Context, cardID uint, incrementMB float64, checkTime time.Time) {
|
||||
if incrementMB <= 0 {
|
||||
return
|
||||
}
|
||||
dateStr := checkTime.Format("2006-01-02")
|
||||
key := constants.RedisCardDailyTrafficKey(cardID, dateStr)
|
||||
if err := b.redis.IncrByFloat(ctx, key, incrementMB).Err(); err != nil {
|
||||
b.logger.Warn("写入日流量缓冲失败",
|
||||
zap.Uint("card_id", cardID), zap.Float64("increment_mb", incrementMB), zap.Error(err))
|
||||
return
|
||||
}
|
||||
b.redis.Expire(ctx, key, 48*time.Hour)
|
||||
}
|
||||
88
internal/task/polling_utils.go
Normal file
88
internal/task/polling_utils.go
Normal file
@@ -0,0 +1,88 @@
|
||||
package task
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/bytedance/sonic"
|
||||
"go.uber.org/zap"
|
||||
"gorm.io/gorm"
|
||||
|
||||
"github.com/break/junhong_cmp_fiber/internal/model"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/constants"
|
||||
)
|
||||
|
||||
// getIntervalByTaskType 从配置中提取指定任务类型的轮询间隔(秒)
|
||||
func getIntervalByTaskType(cfg *model.PollingConfig, taskType string) int {
|
||||
switch taskType {
|
||||
case constants.TaskTypePollingRealname:
|
||||
if cfg.RealnameCheckInterval != nil && *cfg.RealnameCheckInterval > 0 {
|
||||
return *cfg.RealnameCheckInterval
|
||||
}
|
||||
case constants.TaskTypePollingCarddata:
|
||||
if cfg.CarddataCheckInterval != nil && *cfg.CarddataCheckInterval > 0 {
|
||||
return *cfg.CarddataCheckInterval
|
||||
}
|
||||
case constants.TaskTypePollingPackage:
|
||||
if cfg.PackageCheckInterval != nil && *cfg.PackageCheckInterval > 0 {
|
||||
return *cfg.PackageCheckInterval
|
||||
}
|
||||
case constants.TaskTypePollingProtect:
|
||||
if cfg.ProtectCheckInterval != nil && *cfg.ProtectCheckInterval > 0 {
|
||||
return *cfg.ProtectCheckInterval
|
||||
}
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
// shortTaskType 从完整任务类型中提取简短名称(如 polling:carddata → carddata)
|
||||
func shortTaskType(fullTaskType string) string {
|
||||
for i := len(fullTaskType) - 1; i >= 0; i-- {
|
||||
if fullTaskType[i] == ':' {
|
||||
return fullTaskType[i+1:]
|
||||
}
|
||||
}
|
||||
return fullTaskType
|
||||
}
|
||||
|
||||
// parseTaskPayload 解析轮询任务载荷,返回 cardID
|
||||
func parseTaskPayload(payload []byte, logger *zap.Logger) (uint, bool) {
|
||||
var p struct {
|
||||
CardID string `json:"card_id"`
|
||||
}
|
||||
if err := sonic.Unmarshal(payload, &p); err != nil {
|
||||
logger.Error("解析任务载荷失败", zap.Error(err))
|
||||
return 0, false
|
||||
}
|
||||
id, parseErr := strconv.ParseUint(p.CardID, 10, 64)
|
||||
if parseErr != nil {
|
||||
logger.Error("解析卡ID失败", zap.String("card_id", p.CardID), zap.Error(parseErr))
|
||||
return 0, false
|
||||
}
|
||||
return uint(id), true
|
||||
}
|
||||
|
||||
// boolToStr 布尔值转字符串(Redis hash 存储用)
|
||||
func boolToStr(b bool) string {
|
||||
if b {
|
||||
return "1"
|
||||
}
|
||||
return "0"
|
||||
}
|
||||
|
||||
// isNotFound 判断是否为 GORM 记录不存在错误
|
||||
func isNotFound(err error) bool {
|
||||
return err == gorm.ErrRecordNotFound
|
||||
}
|
||||
|
||||
// isResetWindow 判断今天是否在运营商流量重置日窗口内
|
||||
// 窗口 = 重置日当天 + 前一天(容错网关数据延迟上报)
|
||||
func isResetWindow(now time.Time, resetDay int) bool {
|
||||
today := now.Day()
|
||||
if today == resetDay {
|
||||
return true
|
||||
}
|
||||
resetDate := time.Date(now.Year(), now.Month(), resetDay, 0, 0, 0, 0, now.Location())
|
||||
prevDay := resetDate.AddDate(0, 0, -1).Day()
|
||||
return today == prevDay
|
||||
}
|
||||
Reference in New Issue
Block a user