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:
@@ -28,6 +28,7 @@ type Scheduler struct {
|
||||
|
||||
packageActivationHandler *PackageActivationHandler
|
||||
dataResetHandler *DataResetHandler
|
||||
initializer *PollingInitializer // 可选,nil 表示不守卫 Init 完成
|
||||
|
||||
stopChan chan struct{}
|
||||
wg sync.WaitGroup
|
||||
@@ -103,9 +104,29 @@ func (s *Scheduler) SetStopResumeCallback(callback packagepkg.StopResumeCallback
|
||||
}
|
||||
}
|
||||
|
||||
// SetInitializer 注入初始化器(可选,在 Start 前调用)
|
||||
// Init 未完成时调度器跳过分片出队,消除启动期无效轮询噪音
|
||||
func (s *Scheduler) SetInitializer(init *PollingInitializer) {
|
||||
s.initializer = init
|
||||
}
|
||||
|
||||
// writeHeartbeat 写入调度器心跳,表明调度器存活
|
||||
func (s *Scheduler) writeHeartbeat(ctx context.Context) {
|
||||
if err := s.redis.Set(ctx, constants.RedisPollingSchedulerHeartbeatKey(),
|
||||
time.Now().Unix(), 2*s.cfg.ScheduleInterval).Err(); err != nil {
|
||||
s.logger.Warn("写入调度器心跳失败", zap.Error(err))
|
||||
}
|
||||
}
|
||||
|
||||
// scheduleLoop 调度循环
|
||||
func (s *Scheduler) scheduleLoop(ctx context.Context) {
|
||||
defer s.wg.Done()
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
s.logger.Error("调度主循环发生 panic,调度已停止,需重启 Worker",
|
||||
zap.Any("panic", r), zap.Stack("stack"))
|
||||
}
|
||||
}()
|
||||
|
||||
ticker := time.NewTicker(s.cfg.ScheduleInterval)
|
||||
activationTicker := time.NewTicker(10 * time.Second)
|
||||
@@ -123,6 +144,7 @@ func (s *Scheduler) scheduleLoop(ctx context.Context) {
|
||||
s.logger.Info("调度循环收到 ctx 取消信号")
|
||||
return
|
||||
case <-ticker.C:
|
||||
s.writeHeartbeat(ctx)
|
||||
s.processShardSchedule(ctx)
|
||||
case <-activationTicker.C:
|
||||
s.processActivationTasks(ctx)
|
||||
@@ -133,10 +155,16 @@ func (s *Scheduler) scheduleLoop(ctx context.Context) {
|
||||
// processShardSchedule 处理手动队列和分片定时队列(每 1 秒触发)
|
||||
// 使用 90% 的 tick 间隔作为超时,确保单分片 Redis 挂起时不阻塞下一个 tick
|
||||
func (s *Scheduler) processShardSchedule(ctx context.Context) {
|
||||
// 手动队列不受 Init 影响(ConfigManager 已就绪即可)
|
||||
for _, taskType := range allTaskTypes {
|
||||
s.processManualQueue(ctx, taskType, s.cfg.MaxManualBatchSize)
|
||||
}
|
||||
|
||||
// Init 未完成时跳过分片扫描,避免空轮询噪音
|
||||
if s.initializer != nil && !s.initializer.IsCompleted() {
|
||||
return
|
||||
}
|
||||
|
||||
if s.queueMgr == nil {
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user