All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 14m13s
新增 000228 成对迁移 tb_polling_priority_item:卡、任务类型、状态、触发类型、来源订单/套餐使用记录、
触发次数与来源集合、尝试次数、失败原因、人工原因与操作者、店铺快照与各时间列;以活动项部分唯一索引
uq_polling_priority_item_active(仅 deleted_at IS NULL AND status IN ('pending','processing') 占键位)
表达「同卡同任务类型至多一条活动项」,另有状态/时间索引与全列注释;down 守卫在存在活动项或未终态行时
拒绝回滚并给出中文原因。
新增优先轮询请求可靠事件 polling.priority.requested(载荷版本 v1、事件键前缀 prio:)与消费者:只在原
业务事务内追加、幂等键稳定;消费者按卡 × 纳入任务类型(realname/carddata/card_status/package)逐条
建项并在提交后下发执行提示,重复投递只合并触发次数、来源集合与最近触发时间,不新建行也不重复调用。
触发点为四类自动场景 purchase_activated / renewal_activated(按同载体更早套餐使用记录判定)/
queue_activated / addon_activated 与「无有效套餐」no_valid_package(仅在普通套餐轮询来源且存在待生效
套餐使用记录时追加;事件通道显式拒绝 manual_trigger);入队对象恒为卡,绑定设备资产在触发事务内冻结
在用卡快照逐卡建项,不使用设备当前卡槽口径。
轮询共享基类新增认领接缝:四个 Handler(realname/carddata/card_status/package)在并发信号量之后、调用
上游之前探测活动项——待执行条件认领、执行中且 90 秒租约未到期则跳过并延后、无活动项时行为与既有完全
等价;超租约允许相邻执行接管,尝试次数只在真正发起执行后累加,未达上限(3)回到活动态按既有间隔重排,
达上限或业务校验类失败进入失败终态并保留可安全展示原因;执行前校验卡自身与绑定设备的轮询开关。未引入
通用卡级锁与 Redis 活动标记,分片队列的出队、入队与移除路径未改动。
提示通道按任务类型独立键(polling:priority:{taskType}),与既有手动触发队列分离;调度器在同一周期内先
排空优先提示、再排空手动触发队列,提示排空不受分片背压跳过影响;未新建调度设施或异步任务类型。
新增人工优先入队与只读查询三条路由 POST /api/admin/polling-priority-items、
GET /api/admin/polling-priority-items、GET /api/admin/polling-priority-items/:id:人工入队复用既有轮询
权限判定(抽取为同包共享函数),原因必填,不受每日 500 次上限与 24 小时去重约束,重复抑制由活动项合并
承担;读侧按店铺快照下推数据范围,越权与不存在不可区分,不提供优先级分级、有效期或人工重触发入口。
新增 7 个审计动作(enqueue/claim/fail/retry/complete/dequeue/manual_denied)与资源
polling_priority_item,并按(操作者类型,来源)注册,人工侧与 Worker 侧均通过来源校验。
同步 OpenAPI 文档装配三处与路由注册;归档 Change 至
openspec/changes/archive/2026-09-17-add-priority-polling-queue/ 并同步主 Spec(新增
priority-polling-queue、polling-operations 追加单次执行互斥 Requirement 与三条路由索引)与上下文健康
证据(requirement-evidence 150 行、入口矩阵 http 403 / async 56)。
本机验证:junhong_cmp_test 与隔离 Redis DB 15,未连生产、未启动 Worker/API、未调用运营商上游;迁移
up/down/up 与 down 守卫实测(含 dirty=true 记账口径与 force 恢复),A–F 批 94 PASS、接缝 63 PASS、
提示通道 12 PASS、清理零残留 20 PASS。成功路径 Complete、真并发互斥、尝试上限第 3 次判定、HTTP 层权限
矩阵、通道阈值持锁复机边界与三类生效触发点生产集成留待测试部署验证(见
docs/verification/add-priority-polling-queue-verification.md 第 4 节)。自动化测试按项目决策为 N/A,
未新增 *_test.go。
299 lines
10 KiB
Go
299 lines
10 KiB
Go
package polling
|
||
|
||
import (
|
||
"context"
|
||
"sync"
|
||
"time"
|
||
|
||
"github.com/hibiken/asynq"
|
||
"github.com/redis/go-redis/v9"
|
||
"go.uber.org/zap"
|
||
|
||
packagepkg "github.com/break/junhong_cmp_fiber/internal/service/package"
|
||
"github.com/break/junhong_cmp_fiber/pkg/auditcontext"
|
||
"github.com/break/junhong_cmp_fiber/pkg/constants"
|
||
)
|
||
|
||
// InitProgress 已迁移到 initializer.go
|
||
|
||
// Scheduler 轮询调度器
|
||
// 职责:读取分片 Sorted Set 中到期的卡,生成 Asynq 任务
|
||
// 不再负责:配置加载、卡初始化(分别由 PollingConfigManager、PollingInitializer 负责)
|
||
type Scheduler struct {
|
||
redis *redis.Client
|
||
queueClient *asynq.Client
|
||
logger *zap.Logger
|
||
queueMgr *PollingQueueManager
|
||
configMgr *PollingConfigManager
|
||
cfg *SchedulerConfig // 启动时固定,避免每次调度重新创建
|
||
|
||
packageActivationHandler *PackageActivationHandler
|
||
dataResetHandler *DataResetHandler
|
||
initializer *PollingInitializer // 可选,nil 表示不守卫 Init 完成
|
||
|
||
stopChan chan struct{}
|
||
wg sync.WaitGroup
|
||
}
|
||
|
||
// SchedulerConfig 调度器配置
|
||
type SchedulerConfig struct {
|
||
ScheduleInterval time.Duration
|
||
MaxManualBatchSize int
|
||
ScheduleBatchSize int
|
||
}
|
||
|
||
// DefaultSchedulerConfig 默认调度器配置
|
||
func DefaultSchedulerConfig() *SchedulerConfig {
|
||
return &SchedulerConfig{
|
||
ScheduleInterval: 1 * time.Second,
|
||
MaxManualBatchSize: 1000,
|
||
ScheduleBatchSize: constants.PollingDequeueMaxBatchSize,
|
||
}
|
||
}
|
||
|
||
// NewScheduler 创建调度器
|
||
func NewScheduler(
|
||
redisClient *redis.Client,
|
||
queueClient *asynq.Client,
|
||
queueMgr *PollingQueueManager,
|
||
configMgr *PollingConfigManager,
|
||
logger *zap.Logger,
|
||
packageActivationHandler *PackageActivationHandler,
|
||
dataResetHandler *DataResetHandler,
|
||
) *Scheduler {
|
||
return &Scheduler{
|
||
redis: redisClient,
|
||
queueClient: queueClient,
|
||
queueMgr: queueMgr,
|
||
configMgr: configMgr,
|
||
logger: logger,
|
||
cfg: DefaultSchedulerConfig(),
|
||
stopChan: make(chan struct{}),
|
||
packageActivationHandler: packageActivationHandler,
|
||
dataResetHandler: dataResetHandler,
|
||
}
|
||
}
|
||
|
||
// Start 启动调度循环(快速启动,配置加载和初始化由外部完成)
|
||
func (s *Scheduler) Start(ctx context.Context) error {
|
||
s.wg.Add(1)
|
||
go s.scheduleLoop(ctx)
|
||
s.logger.Info("轮询调度器已启动")
|
||
return nil
|
||
}
|
||
|
||
// Stop 停止调度器
|
||
func (s *Scheduler) Stop() {
|
||
s.logger.Info("正在停止轮询调度器...")
|
||
close(s.stopChan)
|
||
s.wg.Wait()
|
||
s.logger.Info("轮询调度器已停止")
|
||
}
|
||
|
||
// RefreshConfigs 刷新配置缓存
|
||
func (s *Scheduler) RefreshConfigs(ctx context.Context) error {
|
||
if s.configMgr == nil {
|
||
return nil
|
||
}
|
||
return s.configMgr.Load(ctx)
|
||
}
|
||
|
||
// SetStopResumeCallback 注入停复机回调(在 Start 前调用)
|
||
func (s *Scheduler) SetStopResumeCallback(callback packagepkg.StopResumeCallback) {
|
||
if s.packageActivationHandler != nil {
|
||
s.packageActivationHandler.stopResumeCallback = callback
|
||
}
|
||
}
|
||
|
||
// 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)
|
||
defer ticker.Stop()
|
||
defer activationTicker.Stop()
|
||
|
||
s.logger.Info("调度循环已启动", zap.Duration("interval", s.cfg.ScheduleInterval))
|
||
|
||
for {
|
||
select {
|
||
case <-s.stopChan:
|
||
s.logger.Info("调度循环收到停止信号")
|
||
return
|
||
case <-ctx.Done():
|
||
s.logger.Info("调度循环收到 ctx 取消信号")
|
||
return
|
||
case <-ticker.C:
|
||
s.writeHeartbeat(ctx)
|
||
s.processShardSchedule(ctx)
|
||
case <-activationTicker.C:
|
||
s.processActivationTasks(ctx)
|
||
}
|
||
}
|
||
}
|
||
|
||
// processShardSchedule 处理手动队列和分片定时队列(每 1 秒触发)
|
||
// 使用 90% 的 tick 间隔作为超时,确保单分片 Redis 挂起时不阻塞下一个 tick
|
||
func (s *Scheduler) processShardSchedule(ctx context.Context) {
|
||
// 优先提示与手动队列都不受 Init 影响(ConfigManager 已就绪即可)。
|
||
// 同一调度周期内先排空优先提示、再排空手动触发队列:优先的强度边界仅为调度入口领先,
|
||
// 提示排空不参与分片背压判断,因此分片积压不会抑制加急入队。
|
||
for _, taskType := range allTaskTypes {
|
||
s.processPriorityQueue(ctx, taskType, s.cfg.MaxManualBatchSize)
|
||
}
|
||
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
|
||
}
|
||
|
||
timeout := s.cfg.ScheduleInterval * 9 / 10
|
||
tickCtx, cancel := context.WithTimeout(ctx, timeout)
|
||
defer cancel()
|
||
|
||
var wg sync.WaitGroup
|
||
for shardID := 0; shardID < s.queueMgr.shardCount; shardID++ {
|
||
wg.Add(1)
|
||
go func(sid int) {
|
||
defer wg.Done()
|
||
defer func() {
|
||
if r := recover(); r != nil {
|
||
s.logger.Error("分片处理 panic,已恢复",
|
||
zap.Int("shard_id", sid), zap.Any("panic", r))
|
||
}
|
||
}()
|
||
s.processOneShard(tickCtx, sid)
|
||
}(shardID)
|
||
}
|
||
wg.Wait()
|
||
}
|
||
|
||
// processOneShard 处理单个分片的所有任务类型出队并推入 Asynq
|
||
func (s *Scheduler) processOneShard(ctx context.Context, shardID int) {
|
||
for _, taskType := range allTaskTypes {
|
||
depth, err := s.queueMgr.GetQueueDepth(ctx, shardID, taskType)
|
||
if err != nil {
|
||
s.logger.Warn("获取分片队列深度失败",
|
||
zap.Int("shard_id", shardID), zap.String("task_type", taskType), zap.Error(err))
|
||
} else if depth > constants.PollingBackpressureThreshold {
|
||
s.logger.Debug("背压:分片队列积压过深,跳过本轮出队",
|
||
zap.Int("shard_id", shardID), zap.String("task_type", taskType), zap.Int64("depth", depth))
|
||
continue
|
||
}
|
||
|
||
entries, err := s.queueMgr.DequeueReady(ctx, shardID, taskType, s.cfg.ScheduleBatchSize)
|
||
if err != nil {
|
||
s.logger.Error("分片出队失败",
|
||
zap.Int("shard_id", shardID), zap.String("task_type", taskType), zap.Error(err))
|
||
continue
|
||
}
|
||
if len(entries) > 0 {
|
||
cardIDs := make([]string, len(entries))
|
||
for i, e := range entries {
|
||
cardIDs[i] = formatUint(e.CardID)
|
||
}
|
||
s.logger.Info("分片出队",
|
||
zap.Int("shard_id", shardID), zap.String("task_type", taskType),
|
||
zap.Int("count", len(entries)))
|
||
s.enqueueBatch(ctx, taskType, cardIDs)
|
||
}
|
||
}
|
||
}
|
||
|
||
// processActivationTasks 套餐激活检查和流量重置调度(每 10 秒触发)
|
||
func (s *Scheduler) processActivationTasks(ctx context.Context) {
|
||
ctx = auditcontext.With(ctx, auditcontext.Context{
|
||
ActorKind: constants.AuditActorScheduledJob, ActorID: constants.AuditActorIDPackageLifecycleScheduler,
|
||
ActorName: "套餐权益生命周期计划任务", Source: constants.AuditSourceScheduler,
|
||
})
|
||
if s.packageActivationHandler != nil {
|
||
if err := s.packageActivationHandler.HandlePackageActivationCheck(ctx); err != nil {
|
||
s.logger.Warn("套餐激活检查失败", zap.Error(err))
|
||
}
|
||
}
|
||
if s.dataResetHandler != nil {
|
||
if err := s.dataResetHandler.HandleDataReset(ctx); err != nil {
|
||
s.logger.Warn("流量重置调度失败", zap.Error(err))
|
||
}
|
||
}
|
||
}
|
||
|
||
// processManualQueue 处理手动触发队列
|
||
func (s *Scheduler) processManualQueue(ctx context.Context, taskType string, maxBatch int) {
|
||
s.drainPollingQueue(ctx, constants.RedisPollingManualQueueKey(taskType), taskType, maxBatch)
|
||
}
|
||
|
||
// processPriorityQueue 处理优先轮询提示通道:与手动触发队列分离,且不参与分片背压判断。
|
||
func (s *Scheduler) processPriorityQueue(ctx context.Context, taskType string, maxBatch int) {
|
||
s.drainPollingQueue(ctx, constants.RedisPollingPriorityQueueKey(taskType), taskType, maxBatch)
|
||
}
|
||
|
||
// drainPollingQueue 从指定提示通道批量取出卡,并复用既有批量提交逻辑提交到同一 Asynq 任务类型与队列。
|
||
func (s *Scheduler) drainPollingQueue(ctx context.Context, key, taskType string, maxBatch int) {
|
||
cardIDs, err := s.redis.LPopCount(ctx, key, maxBatch).Result()
|
||
if err != nil || len(cardIDs) == 0 {
|
||
return
|
||
}
|
||
s.enqueueBatch(ctx, taskType, cardIDs)
|
||
}
|
||
|
||
// enqueueBatch 批量提交任务到 Asynq 队列;入队失败时回退至分片队列防止卡永久丢失
|
||
func (s *Scheduler) enqueueBatch(ctx context.Context, taskType string, cardIDs []string) {
|
||
for _, cardID := range cardIDs {
|
||
payload := map[string]interface{}{
|
||
"card_id": cardID,
|
||
"is_manual": false,
|
||
"timestamp": time.Now().Unix(),
|
||
}
|
||
payloadBytes, marshalErr := marshalJSON(payload)
|
||
if marshalErr != nil {
|
||
s.logger.Error("序列化任务载荷失败,跳过该卡",
|
||
zap.String("task_type", taskType), zap.String("card_id", cardID), zap.Error(marshalErr))
|
||
continue
|
||
}
|
||
task := asynq.NewTask(taskType, payloadBytes,
|
||
asynq.MaxRetry(0),
|
||
asynq.Timeout(60*time.Second),
|
||
asynq.Queue(constants.QueueForTaskType(taskType)),
|
||
)
|
||
if _, err := s.queueClient.Enqueue(task); err != nil {
|
||
s.logger.Error("提交任务失败,回退至分片队列防止卡永久丢失",
|
||
zap.String("task_type", taskType), zap.String("card_id", cardID), zap.Error(err))
|
||
if id, parseErr := parseUint(cardID); parseErr == nil {
|
||
if reqErr := s.queueMgr.Requeue(ctx, id, taskType, time.Now()); reqErr != nil {
|
||
s.logger.Error("回退入队失败,卡可能永久丢失",
|
||
zap.String("card_id", cardID), zap.Error(reqErr))
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|