2bec257289
iotlcm-worker-svr:异步执行中枢(RabbitMQ 消费者 + 全量同步编排)。 - Go module:iotlcm-worker-svr → git.5iot.com/iotlcm-public/iotlcm-worker-svr(214 文件) - 凭据与内部标识脱敏:MySQL/Redis 凭据、私网与生产 IP、内网域名、 库名 xiujie_admin → iotlcm、哨兵名占位 - 159 处真实手机号 → 合法 11 位编造号(保留号段与后 4 位,维持互异, 不破坏「校验 MSISDN 格式」等测试断言) - 移除内部资产:CI 触发器、Jenkins 触发测试文档、预演门禁与镜像 digest 记录 - Dockerfile 改用公共基础镜像(golang:1.25-alpine → alpine:3.23), 移除写死的 git 凭据 - README 重写(架构/全量同步模型/配置/已知失败),补 Apache-2.0 LICENSE,LF 行尾 + .gitattributes 已知:3 个测试包在源仓同一提交上同样失败(client/upstream、 service/allocate、service/inbound),属既有缺陷,本仓未引入亦未修复。
215 lines
5.8 KiB
Go
215 lines
5.8 KiB
Go
package warningconfig
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/gogf/gf/v2/errors/gerror"
|
|
"github.com/gogf/gf/v2/os/gtime"
|
|
|
|
"git.5iot.com/iotlcm-public/iotlcm-worker-svr/internal/consts"
|
|
"git.5iot.com/iotlcm-public/iotlcm-worker-svr/internal/model/entity"
|
|
)
|
|
|
|
const warningConfigProcessingRetryAfter = 2 * time.Minute
|
|
|
|
type executor struct {
|
|
repository *Repository
|
|
}
|
|
|
|
func NewExecutor() *executor {
|
|
return &executor{repository: NewRepository()}
|
|
}
|
|
|
|
func (e *executor) CanExecuteProcessing(_ context.Context, main *entity.BatchMain) (bool, error) {
|
|
if main == nil || main.BatchType != consts.BatchTypeSimWarningConfig || main.BatchAsyncStatus != consts.BatchAsyncStatusProcessing {
|
|
return false, nil
|
|
}
|
|
if main.StartTime == nil || main.StartTime.IsZero() {
|
|
return true, nil
|
|
}
|
|
return gtime.Now().Sub(main.StartTime) >= warningConfigProcessingRetryAfter, nil
|
|
}
|
|
|
|
func (e *executor) Execute(ctx context.Context, batchNo string) error {
|
|
task, err := e.repository.Load(ctx, strings.TrimSpace(batchNo))
|
|
if err != nil {
|
|
return gerror.Wrap(err, "load warning config batch failed")
|
|
}
|
|
validateTask(task)
|
|
return gerror.Wrap(e.repository.Commit(ctx, task), "commit warning config batch failed")
|
|
}
|
|
|
|
func validateTask(task *Task) {
|
|
if task == nil || len(task.Details) == 0 {
|
|
if task != nil {
|
|
task.FailureReason = "预警配置批次没有可处理明细"
|
|
}
|
|
return
|
|
}
|
|
for _, detail := range task.Details {
|
|
validateDetail(task, detail)
|
|
}
|
|
_, failCount := countResult(task)
|
|
if failCount > 0 {
|
|
markAtomicFailure(task, "预警配置批次存在失败明细,整批未执行")
|
|
}
|
|
}
|
|
|
|
func markAtomicFailure(task *Task, reason string) {
|
|
if task == nil {
|
|
return
|
|
}
|
|
reason = strings.TrimSpace(reason)
|
|
if reason == "" {
|
|
reason = "本批次存在失败明细,整批未执行"
|
|
}
|
|
task.FailureReason = reason
|
|
for _, detail := range task.Details {
|
|
if detail != nil && strings.TrimSpace(detail.Reason) == "" {
|
|
detail.Reason = "本批次存在失败明细,整批未执行"
|
|
}
|
|
}
|
|
}
|
|
|
|
func validateDetail(task *Task, detail *Detail) {
|
|
if detail == nil || detail.Reason != "" {
|
|
return
|
|
}
|
|
config := detail.Config
|
|
if config.ItemType == warnItemTypeFlowPool {
|
|
validateFlowPoolDetail(config, detail)
|
|
return
|
|
}
|
|
validateCardDetail(config, detail)
|
|
}
|
|
|
|
func validateCardDetail(config Config, detail *Detail) {
|
|
detail.ICCID = strings.TrimSpace(detail.ICCID)
|
|
detail.MSISDN = strings.TrimSpace(detail.MSISDN)
|
|
detail.IMSI = strings.TrimSpace(detail.IMSI)
|
|
if detail.ICCID == "" {
|
|
detail.Reason = "ICCID不能为空"
|
|
return
|
|
}
|
|
if !detail.Authorized {
|
|
detail.Reason = "ICCID不在当前操作员授权范围内"
|
|
return
|
|
}
|
|
if detail.Card == nil {
|
|
detail.Reason = "ICCID不存在或不属于当前租户"
|
|
return
|
|
}
|
|
if detail.MSISDN != "" && detail.MSISDN != detail.Card.MSISDN {
|
|
detail.Reason = "MSISDN与卡档案不一致"
|
|
return
|
|
}
|
|
if detail.IMSI != "" && detail.IMSI != detail.Card.IMSI {
|
|
detail.Reason = "IMSI与卡档案不一致"
|
|
return
|
|
}
|
|
if detail.Card.BossCardStatus == consts.SimBossCardStatusStopped {
|
|
detail.Reason = "卡已停机,不能配置预警"
|
|
return
|
|
}
|
|
if detail.Card.BossCardStatus != consts.SimBossCardStatusActivated {
|
|
detail.Reason = "当前卡状态为" + bossCardStatusText(detail.Card.BossCardStatus) + ",只有已激活卡可以配置预警"
|
|
return
|
|
}
|
|
if detail.Card.PackageStatus != packageStatusEffective {
|
|
detail.Reason = "套餐未生效,不能配置预警"
|
|
return
|
|
}
|
|
if detail.Card.IsFlowPool != 0 {
|
|
detail.Reason = "流量池卡暂不支持单卡预警配置"
|
|
return
|
|
}
|
|
if strings.TrimSpace(detail.Card.CurrentPackageCode) == "" {
|
|
detail.Reason = "当前套餐编号为空,不能配置预警"
|
|
return
|
|
}
|
|
switch config.WarnType {
|
|
case warnTypeCardTraffic:
|
|
validateTrafficDetail(config, detail)
|
|
case warnTypeExpireDay, warnTypeExpireMonth:
|
|
if detail.Card.FlowPkgEndAt == nil || detail.Card.FlowPkgEndAt.IsZero() {
|
|
detail.Reason = "套餐到期时间为空,不能配置到期预警"
|
|
}
|
|
default:
|
|
detail.Reason = "预警类型无效"
|
|
}
|
|
}
|
|
|
|
func validateFlowPoolDetail(config Config, detail *Detail) {
|
|
detail.FlowPoolCode = strings.TrimSpace(detail.FlowPoolCode)
|
|
if detail.FlowPoolCode == "" {
|
|
detail.Reason = "流量池编号不能为空"
|
|
return
|
|
}
|
|
if !detail.Authorized {
|
|
detail.Reason = "流量池不在当前操作员授权范围内"
|
|
return
|
|
}
|
|
if detail.Pool == nil {
|
|
detail.Reason = "流量池不存在或不属于当前租户"
|
|
return
|
|
}
|
|
if detail.Pool.PoolStatus != flowPoolStatusNormal {
|
|
detail.Reason = "流量池状态不是正常,不能配置预警"
|
|
return
|
|
}
|
|
if detail.Pool.TotalFlowMb <= 0 {
|
|
detail.Reason = "流量池总流量为空,不能配置预警"
|
|
return
|
|
}
|
|
if config.WarnType != warnTypeFlowPoolPercent || config.WarnUnit != warnUnitPercent {
|
|
detail.Reason = "流量池预警仅支持百分比配置"
|
|
return
|
|
}
|
|
if config.AutoAction != 0 {
|
|
detail.Reason = "流量池预警暂不支持第三次关停"
|
|
}
|
|
}
|
|
|
|
func validateTrafficDetail(config Config, detail *Detail) {
|
|
if detail.Card.PackageTotalFlowMb <= 0 {
|
|
detail.Reason = "套餐总流量为空,不能配置流量预警"
|
|
return
|
|
}
|
|
if config.WarnUnit != warnUnitMb {
|
|
return
|
|
}
|
|
total := detail.Card.PackageTotalFlowMb
|
|
if config.FirstWarn > total || config.SecondWarn > total || config.ThirdWarn > total {
|
|
detail.Reason = "流量阈值不能大于套餐总流量"
|
|
}
|
|
}
|
|
|
|
func countResult(task *Task) (successCount, failCount int) {
|
|
if task == nil {
|
|
return 0, 0
|
|
}
|
|
for _, detail := range task.Details {
|
|
if detail != nil && detail.Success() {
|
|
successCount++
|
|
} else {
|
|
failCount++
|
|
}
|
|
}
|
|
return successCount, failCount
|
|
}
|
|
|
|
func bossCardStatusText(status int) string {
|
|
switch status {
|
|
case consts.SimBossCardStatusActivated:
|
|
return "已激活"
|
|
case consts.SimBossCardStatusStopped:
|
|
return "已停机"
|
|
case 1:
|
|
return "待激活"
|
|
default:
|
|
return "未知状态"
|
|
}
|
|
}
|