fix: 补全三处轮询覆盖缺口
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 7m11s
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 7m11s
问题一:protect 轮询任务从未被调度器初始化入队 PollingConfig 新增 protect_check_interval 字段(NULL=不参与), 调度器 initCardsBatch/initCardPolling/requeueCard 补全 protect 队列 初始化逻辑,IotCard 新增 last_protect_check_at 记录上次检查时间。 迁移文件:000102_add_polling_protect_fields 问题二:设备套餐流量耗尽时绑定卡未被停机 UsageService.checkAndTriggerSuspension 新增 carrier_type=device 分支, 通过 DeviceSimBindingStore.ListByDeviceID 查询绑定卡,对每张卡异步 触发 CheckAndStopCard,同步注入 Bootstrap 依赖。 问题三:主套餐过期后不立即停机,依赖下次轮询兜底 PackageActivationHandler.processExpiredPackage 在 updateCarrierSuspended Status 后,若载体无后续生效套餐,立即异步调用 CheckAndStopCard(iot_card 类型直接触发,device 类型遍历绑定卡),消除停机延迟窗口。 Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
This commit is contained in:
@@ -25,6 +25,7 @@ type UsageService struct {
|
||||
redis *redis.Client
|
||||
packageUsageStore *postgres.PackageUsageStore
|
||||
packageUsageDailyRecord *postgres.PackageUsageDailyRecordStore
|
||||
deviceSimBindingStore *postgres.DeviceSimBindingStore
|
||||
logger *zap.Logger
|
||||
stopResumeCallback StopResumeCallback // 停复机回调,可选
|
||||
}
|
||||
@@ -34,6 +35,7 @@ func NewUsageService(
|
||||
redis *redis.Client,
|
||||
packageUsageStore *postgres.PackageUsageStore,
|
||||
packageUsageDailyRecord *postgres.PackageUsageDailyRecordStore,
|
||||
deviceSimBindingStore *postgres.DeviceSimBindingStore,
|
||||
logger *zap.Logger,
|
||||
) *UsageService {
|
||||
return &UsageService{
|
||||
@@ -41,6 +43,7 @@ func NewUsageService(
|
||||
redis: redis,
|
||||
packageUsageStore: packageUsageStore,
|
||||
packageUsageDailyRecord: packageUsageDailyRecord,
|
||||
deviceSimBindingStore: deviceSimBindingStore,
|
||||
logger: logger,
|
||||
}
|
||||
}
|
||||
@@ -220,17 +223,44 @@ func (s *UsageService) checkAndTriggerSuspension(ctx context.Context, tx *gorm.D
|
||||
zap.String("carrier_type", carrierType),
|
||||
zap.Uint("carrier_id", carrierID))
|
||||
|
||||
// 任务 24.6: 调用停复机服务执行停机
|
||||
if s.stopResumeCallback != nil && carrierType == "iot_card" {
|
||||
// 在事务外异步执行停机,避免长事务
|
||||
go func() {
|
||||
stopCtx := context.Background()
|
||||
if err := s.stopResumeCallback.CheckAndStopCard(stopCtx, carrierID); err != nil {
|
||||
s.logger.Error("调用停机服务失败",
|
||||
zap.Uint("card_id", carrierID),
|
||||
if carrierType == "iot_card" {
|
||||
if s.stopResumeCallback != nil {
|
||||
go func() {
|
||||
stopCtx := context.Background()
|
||||
if err := s.stopResumeCallback.CheckAndStopCard(stopCtx, carrierID); err != nil {
|
||||
s.logger.Error("调用停机服务失败",
|
||||
zap.Uint("card_id", carrierID),
|
||||
zap.Error(err))
|
||||
}
|
||||
}()
|
||||
}
|
||||
} else if carrierType == "device" {
|
||||
if s.stopResumeCallback == nil {
|
||||
s.logger.Warn("停复机回调未注入,跳过设备绑定卡停机",
|
||||
zap.Uint("device_id", carrierID))
|
||||
} else {
|
||||
bindings, err := s.deviceSimBindingStore.ListByDeviceID(context.Background(), carrierID)
|
||||
if err != nil {
|
||||
s.logger.Error("查询设备绑定卡失败",
|
||||
zap.Uint("device_id", carrierID),
|
||||
zap.Error(err))
|
||||
} else if len(bindings) == 0 {
|
||||
s.logger.Debug("设备无绑定卡,跳过停机",
|
||||
zap.Uint("device_id", carrierID))
|
||||
} else {
|
||||
for _, b := range bindings {
|
||||
cardID := b.IotCardID
|
||||
go func(cID uint) {
|
||||
stopCtx := context.Background()
|
||||
if err := s.stopResumeCallback.CheckAndStopCard(stopCtx, cID); err != nil {
|
||||
s.logger.Error("调用停机服务失败",
|
||||
zap.Uint("card_id", cID),
|
||||
zap.Error(err))
|
||||
}
|
||||
}(cardID)
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user