fix: 修正轮询配置多匹配逻辑,支持同卡匹配多个配置并按 priority 合并 interval
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 7m27s
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 7m27s
核心变更: - MatchConfig 改为 MatchConfigs,返回所有匹配配置 - MergedTaskIntervals 按 task type 合并各配置,选取最高优先级(非 nil 且最小 priority 值) - hasAnyEnabledInterval 过滤所有 interval 均为 NULL 的配置 - calcInitialDelay 重构为纯函数,接收 interval 参数 - 移除 getEnabledTaskTypes 和 getIntervalByTaskType(被 MergedTaskIntervals 替代) - scheduler.go 新增心跳 key + 顶层 panic recovery + Init 完成守卫 - initializer.go 批量失败日志升级为 Error,逐条检查 Pipeline 命令错误 - 数据迁移:禁用 id=29 的轮询配置(所有 interval 均为 NULL) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -9,7 +9,6 @@ import (
|
||||
|
||||
"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 卡生命周期轮询管理服务
|
||||
@@ -134,68 +133,23 @@ func (s *PollingLifecycleService) shouldEnqueue(ctx context.Context, card *model
|
||||
|
||||
// enqueueCard 按匹配配置将卡入队分片队列
|
||||
func (s *PollingLifecycleService) enqueueCard(ctx context.Context, card *model.IotCard) {
|
||||
cfg := s.configMgr.MatchConfig(card)
|
||||
if cfg == nil {
|
||||
intervals := s.configMgr.MergedTaskIntervals(card)
|
||||
if len(intervals) == 0 {
|
||||
return
|
||||
}
|
||||
taskTypes := getEnabledTaskTypes(cfg)
|
||||
for _, taskType := range taskTypes {
|
||||
if err := s.queueMgr.Requeue(ctx, card.ID, taskType, calcInitialDelay(card, cfg, taskType)); err != nil {
|
||||
for taskType, info := range intervals {
|
||||
if err := s.queueMgr.Requeue(ctx, card.ID, taskType, calcInitialDelay(info.Interval)); 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)
|
||||
}
|
||||
if cfg.CardStatusCheckInterval != nil && *cfg.CardStatusCheckInterval > 0 {
|
||||
types = append(types, constants.TaskTypePollingCardStatus)
|
||||
}
|
||||
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 {
|
||||
func calcInitialDelay(interval int) 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
|
||||
}
|
||||
case constants.TaskTypePollingCardStatus:
|
||||
if cfg.CardStatusCheckInterval != nil {
|
||||
interval = *cfg.CardStatusCheckInterval
|
||||
}
|
||||
}
|
||||
if interval <= 0 {
|
||||
return now
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user