feat: 按运营商账户调度的后台接口与重算到期
- /polling-carrier-params 列表(含默认标记与消费能力)、详情、保存(执行上限不得大于订阅进程 max_in_flight 之和)、删除;/polling-check-scenes 列表与修改(检查类型、递增的确认延迟、冷却);/polling-failure-alerts 列表、按账户与错误类别汇总、详情、标记已处理;GET /iot-cards/:id/polling-state 单卡轮询状态;GET /polling-stats/carrier-queues 按账户队列与执行监控 - 读取仅超级管理员与平台账号,修改仅超级管理员(告警处理平台账号可用),均写统一审计;新增审计动作与资源 - carrier 模式下重算到期时间写入卡到期信息与账户队列,只提前不推后
This commit is contained in:
@@ -2,10 +2,12 @@ package polling
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strconv"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
"go.uber.org/zap"
|
||||
|
||||
"github.com/break/junhong_cmp_fiber/internal/model"
|
||||
@@ -197,3 +199,28 @@ func (i *CarrierInitializer) setProgress(update func(*InitProgress)) {
|
||||
update(&i.progress)
|
||||
i.mu.Unlock()
|
||||
}
|
||||
|
||||
// AdvanceDue 把卡某检查类型的到期时间提前到 due(只提前不推后),并同步把账户队列中的 score 提前;返回是否提前。
|
||||
// 卡不在轮询中(无到期信息)时不处理。
|
||||
func (c *CarrierScheduling) AdvanceDue(ctx context.Context, card *model.IotCard, checkType string, due time.Time) (bool, error) {
|
||||
if c == nil || c.store == nil || card == nil {
|
||||
return false, nil
|
||||
}
|
||||
current, err := c.store.ReadDue(ctx, card.ID)
|
||||
if err != nil || current.CarrierID == 0 {
|
||||
return false, err
|
||||
}
|
||||
item, ok := current.Types[checkType]
|
||||
if !ok || !item.HasDue || !due.Before(item.Due) {
|
||||
return false, nil
|
||||
}
|
||||
client := c.store.Client()
|
||||
member := strconv.FormatUint(uint64(card.ID), 10)
|
||||
pipe := client.TxPipeline()
|
||||
pipe.HSet(ctx, constants.RedisCardDueKey(card.ID), carrierqueue.FieldDue(checkType), due.UnixMilli())
|
||||
score := redis.ZAddArgs{XX: true, LT: true, Members: []redis.Z{{Score: float64(due.UnixMilli()), Member: member}}}
|
||||
pipe.ZAddArgs(ctx, constants.RedisCarrierQueueKey(current.CarrierID), score)
|
||||
pipe.ZAddArgs(ctx, constants.RedisCarrierPriorityQueueKey(current.CarrierID), score)
|
||||
_, err = pipe.Exec(ctx)
|
||||
return err == nil, err
|
||||
}
|
||||
|
||||
@@ -32,6 +32,39 @@ type DueRecalculator struct {
|
||||
configMgr *PollingConfigManager
|
||||
queueMgr *PollingQueueManager
|
||||
logger *zap.Logger
|
||||
// carrier 按运营商账户调度接缝:carrier 模式下重算写入卡到期信息与账户队列。
|
||||
carrier *CarrierScheduling
|
||||
}
|
||||
|
||||
// SetCarrierScheduling 注入按运营商账户调度接缝。
|
||||
func (r *DueRecalculator) SetCarrierScheduling(carrier *CarrierScheduling) {
|
||||
r.carrier = carrier
|
||||
}
|
||||
|
||||
// recalculateCarrierBatch 按运营商账户调度时重算:各检查类型候选到期同旧结构口径,只提前不推后。
|
||||
func (r *DueRecalculator) recalculateCarrierBatch(ctx context.Context, cards []*model.IotCard) (int, error) {
|
||||
now := time.Now()
|
||||
advanced := 0
|
||||
for _, card := range cards {
|
||||
for taskType, info := range r.configMgr.MergedTaskIntervals(card) {
|
||||
checkType := constants.CheckTypeFromTaskType(taskType)
|
||||
if checkType == "" || info.Interval <= 0 {
|
||||
continue
|
||||
}
|
||||
due := recalculatedDue(lastCheckAtByTaskType(card, taskType), info.Interval, now)
|
||||
if windowDue := r.configMgr.NextCheckAt(ctx, card, taskType, info, now); windowDue.Before(due) && windowDue.After(now) {
|
||||
due = windowDue
|
||||
}
|
||||
ok, err := r.carrier.AdvanceDue(ctx, card, checkType, due)
|
||||
if err != nil {
|
||||
return advanced, err
|
||||
}
|
||||
if ok {
|
||||
advanced++
|
||||
}
|
||||
}
|
||||
}
|
||||
return advanced, nil
|
||||
}
|
||||
|
||||
// NewDueRecalculator 创建重算器。
|
||||
@@ -97,6 +130,9 @@ func (r *DueRecalculator) recalculateBatch(ctx context.Context, cards []*model.I
|
||||
if tier := r.configMgr.TierResolver(); tier != nil {
|
||||
tier.Prefill(ctx, cards)
|
||||
}
|
||||
if r.carrier.Active(ctx) {
|
||||
return r.recalculateCarrierBatch(ctx, cards)
|
||||
}
|
||||
now := time.Now()
|
||||
pipe := r.redis.Pipeline()
|
||||
var cmds []*redis.IntCmd
|
||||
|
||||
Reference in New Issue
Block a user