chore: 并入 iotlcm-worker-svr 历史到 worker-svr/(原提交 96f0f4e)

This commit is contained in:
MiFaZhan
2026-11-02 13:28:03 +08:00
422 changed files with 80993 additions and 0 deletions
@@ -0,0 +1,241 @@
package lifecycle
import (
"context"
"sort"
"strings"
"sync"
"github.com/gogf/gf/v2/errors/gerror"
"git.5iot.com/iotlcm-public/iotlcm-worker-svr/internal/consts"
"git.5iot.com/iotlcm-public/iotlcm-worker-svr/internal/model"
)
type executor struct {
repository Repository
upstream Upstream
}
func NewExecutor(repository Repository, upstream Upstream) Executor {
return &executor{repository: repository, upstream: upstream}
}
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 lifecycle batch")
}
if !validateTask(task) {
if err = e.repository.SaveValidationFailure(ctx, task); err != nil {
return gerror.Wrap(err, "save lifecycle validation failure")
}
return gerror.Wrap(e.repository.Finalize(ctx, task), "finalize lifecycle validation failure")
}
groups := groupByChanKey(task.Details)
keys := make([]string, 0, len(groups))
for chanKey := range groups {
keys = append(keys, chanKey)
}
sort.Strings(keys)
for _, chanKey := range keys {
for index, outcome := range e.changeGroup(ctx, task, groups[chanKey]) {
detail := groups[chanKey][index]
resolved, saveErr := e.repository.SaveResult(ctx, task, detail, outcome)
if saveErr != nil {
return gerror.Wrapf(saveErr, "save lifecycle detail %d result", detail.ID)
}
detail.ApplyOutcome(resolved)
}
}
return gerror.Wrap(e.repository.Finalize(ctx, task), "finalize lifecycle batch")
}
func validateTask(task *Task) bool {
if task == nil || len(task.Details) == 0 {
return false
}
hasFailure := false
for _, detail := range task.Details {
if detail == nil {
hasFailure = true
continue
}
validateDetail(task.Operation, detail)
if detail.Reason != "" {
hasFailure = true
}
}
if !hasFailure {
task.ValidationOK = true
return true
}
for _, detail := range task.Details {
if detail != nil && detail.Reason == "" {
detail.Reason = "同批次存在校验失败卡,本卡未请求上游"
}
}
task.FailureReason = "批次校验未通过,未发起上游请求"
return false
}
func validateDetail(operation Operation, detail *Detail) {
if detail.Reason != "" {
return
}
if detail.Card == nil {
detail.Reason = "ICCID不存在或不属于当前租户"
return
}
detail.MSISDN = strings.TrimSpace(detail.Card.MSISDN)
detail.IMSI = strings.TrimSpace(detail.Card.IMSI)
detail.ChanKey = strings.TrimSpace(detail.Card.ChanKey)
detail.StatusBefore = detail.Card.BossCardStatus
if !detail.Authorized {
detail.Reason = "ICCID不在当前操作员授权范围内"
return
}
var reasons []string
if detail.ChanKey == "" {
reasons = append(reasons, "ICCID缺少通道标识")
}
if detail.InputMSISDN != "" && detail.InputMSISDN != detail.MSISDN {
reasons = append(reasons, "MSISDN与卡档案不一致")
}
if detail.InputIMSI != "" && detail.InputIMSI != detail.IMSI {
reasons = append(reasons, "IMSI与卡档案不一致")
}
detail.Reason = strings.Join(reasons, ";")
// TODO: 正常系统开发完成后恢复卡状态限制
// 注释原因:开发环境经常出现卡状态不在预期可操作范围内的情况,先放行
// if detail.Card.BossCardStatus != operation.ExpectedStatus {
// detail.Reason = "SIM card status does not allow this lifecycle operation"
// return
// }
}
func groupByChanKey(details []*Detail) map[string][]*Detail {
groups := make(map[string][]*Detail)
for _, detail := range details {
groups[detail.ChanKey] = append(groups[detail.ChanKey], detail)
}
return groups
}
func (e *executor) changeGroup(ctx context.Context, task *Task, details []*Detail) []Outcome {
subBatches := splitDetails(details, consts.BatchSubBatchSize)
subOutcomes := make([][]Outcome, len(subBatches))
sem := make(chan struct{}, consts.BatchSubBatchConcurrency)
var wg sync.WaitGroup
for i, sub := range subBatches {
wg.Add(1)
sem <- struct{}{}
go func(index int, batch []*Detail) {
defer wg.Done()
defer func() { <-sem }()
subOutcomes[index] = e.changeSubBatch(ctx, task, batch)
}(i, sub)
}
wg.Wait()
outcomes := make([]Outcome, 0, len(details))
for _, subOutcome := range subOutcomes {
outcomes = append(outcomes, subOutcome...)
}
return outcomes
}
// changeSubBatch 处理单个子批次:构建请求并调用上游,返回与该子批次明细等长的 Outcome 切片。
func (e *executor) changeSubBatch(ctx context.Context, task *Task, details []*Detail) []Outcome {
input := &model.SimCardStatusChangeInput{
SimKeys: make([]*model.SimKey, 0, len(details)),
ReqData: model.SimCardStatusChangeInputItem{OperType: task.Operation.OperType},
}
for _, detail := range details {
input.SimKeys = append(input.SimKeys, &model.SimKey{ChanKey: detail.ChanKey, ICCID: detail.ICCID, MSISDN: detail.MSISDN, IMSI: detail.IMSI})
}
response, err := e.upstream.ChangeSimStatus(ctx, input)
if err != nil {
outcomes := make([]Outcome, len(details))
for index := range outcomes {
outcomes[index].Message = "请求上游失败:" + err.Error()
}
return outcomes
}
return mapOutcomes(details, response)
}
// splitDetails 按固定大小把明细切成有序的子批次;空输入返回空切片。
func splitDetails(details []*Detail, size int) [][]*Detail {
if size <= 0 {
size = 1
}
batches := make([][]*Detail, 0, (len(details)+size-1)/size)
for i := 0; i < len(details); i += size {
end := i + size
if end > len(details) {
end = len(details)
}
batches = append(batches, details[i:end])
}
return batches
}
func mapOutcomes(details []*Detail, response []model.SimBaseRes[model.SimCardStatusChangeOutputItem]) []Outcome {
outcomes := make([]Outcome, len(details))
used := make([]bool, len(response))
for detailIndex, detail := range details {
matched := -1
for responseIndex, item := range response {
if !used[responseIndex] && sameSim(detail, item.SimKey) {
matched = responseIndex
break
}
}
if matched < 0 && detailIndex < len(response) && !used[detailIndex] && response[detailIndex].SimKey == nil {
matched = detailIndex
}
if matched < 0 {
outcomes[detailIndex] = Outcome{Message: "upstream did not return a result for this SIM card"}
continue
}
used[matched] = true
item := response[matched]
message := strings.TrimSpace(item.Message)
if item.Code != 0 && message == "" {
message = "upstream processing failed"
}
outcomes[detailIndex] = Outcome{Success: item.Code == 0, Code: item.Code, Message: message, OrderNum: strings.TrimSpace(item.ResData.OrderNum)}
}
return outcomes
}
func sameSim(detail *Detail, key *model.SimKey) bool {
if detail == nil || key == nil {
return false
}
if value := strings.TrimSpace(key.ICCID); value != "" {
return value == detail.ICCID
}
if value := strings.TrimSpace(key.MSISDN); value != "" {
return value == detail.MSISDN
}
if value := strings.TrimSpace(key.IMSI); value != "" {
return value == detail.IMSI
}
return false
}
func OperationByBatchType(batchType int) (Operation, error) {
switch batchType {
case consts.BatchTypeSimCardStart:
return Operation{BatchType: batchType, OperType: consts.SimCardStatusOperTypeStart, ExpectedStatus: consts.SimBossCardStatusStopped, TargetStatus: consts.SimBossCardStatusActivated}, nil
case consts.BatchTypeSimCardStop:
return Operation{BatchType: batchType, OperType: consts.SimCardStatusOperTypeStop, ExpectedStatus: consts.SimBossCardStatusActivated, TargetStatus: consts.SimBossCardStatusStopped}, nil
default:
return Operation{}, gerror.Newf("unsupported lifecycle batch type %d", batchType)
}
}
@@ -0,0 +1,211 @@
package lifecycle
import (
"context"
"errors"
"testing"
"git.5iot.com/iotlcm-public/iotlcm-worker-svr/internal/consts"
"git.5iot.com/iotlcm-public/iotlcm-worker-svr/internal/model"
)
func TestExecuteValidationFailureSkipsUpstream(t *testing.T) {
operation, err := OperationByBatchType(consts.BatchTypeSimCardStop)
if err != nil {
t.Fatal(err)
}
task := &Task{
Batch: Batch{BatchNo: "B-1"},
Operation: operation,
Details: []*Detail{
{ID: 1, ICCID: "iccid-1", Authorized: true, Card: &Card{ID: 11, ChanKey: "channel", BossCardStatus: consts.SimBossCardStatusActivated}},
{ID: 2, ICCID: "iccid-2", Authorized: false, Card: &Card{ID: 12, ChanKey: "channel", BossCardStatus: consts.SimBossCardStatusActivated}},
},
}
repo := &fakeRepository{task: task}
upstream := &fakeUpstream{responseError: errors.New("must not be called")}
if err = NewExecutor(repo, upstream).Execute(context.Background(), "B-1"); err != nil {
t.Fatal(err)
}
if upstream.calls != 0 {
t.Fatalf("upstream calls = %d, want 0", upstream.calls)
}
if !repo.validationSaved || !repo.finalized {
t.Fatal("validation failure was not persisted and finalized")
}
if task.Details[0].Reason == "" || task.Details[1].Reason == "" {
t.Fatal("every detail must receive a validation failure reason")
}
}
func TestExecuteMapsMixedResponseAndPreservesOrderNumber(t *testing.T) {
operation, err := OperationByBatchType(consts.BatchTypeSimCardStart)
if err != nil {
t.Fatal(err)
}
task := &Task{
Batch: Batch{BatchNo: "B-2"},
Operation: operation,
Details: []*Detail{
{ID: 1, ICCID: "iccid-1", MSISDN: "msisdn-1", IMSI: "imsi-1", Authorized: true, Card: &Card{ID: 11, ChanKey: "channel", MSISDN: "msisdn-1", IMSI: "imsi-1", BossCardStatus: consts.SimBossCardStatusStopped}},
{ID: 2, ICCID: "iccid-2", MSISDN: "msisdn-2", IMSI: "imsi-2", Authorized: true, Card: &Card{ID: 12, ChanKey: "channel", MSISDN: "msisdn-2", IMSI: "imsi-2", BossCardStatus: consts.SimBossCardStatusStopped}},
},
}
repo := &fakeRepository{task: task}
upstream := &fakeUpstream{response: []model.SimBaseRes[model.SimCardStatusChangeOutputItem]{
{Code: 0, Message: "ok", SimKey: nil, ResData: model.SimCardStatusChangeOutputItem{OrderNum: "order-1"}},
{Code: 9001, Message: "denied", SimKey: &model.SimKey{ICCID: "iccid-2"}, ResData: model.SimCardStatusChangeOutputItem{OrderNum: "order-2"}},
}}
if err = NewExecutor(repo, upstream).Execute(context.Background(), "B-2"); err != nil {
t.Fatal(err)
}
if upstream.input.ReqData.OperType != consts.SimCardStatusOperTypeStart {
t.Fatalf("operType = %q", upstream.input.ReqData.OperType)
}
if len(repo.saved) != 2 || !repo.saved[0].Success || repo.saved[0].OrderNum != "order-1" {
t.Fatalf("first result = %+v", repo.saved)
}
if repo.saved[1].Success || repo.saved[1].Code != 9001 || repo.saved[1].Message != "denied" || repo.saved[1].OrderNum != "order-2" {
t.Fatalf("second result = %+v", repo.saved[1])
}
if !repo.finalized {
t.Fatal("batch was not finalized")
}
}
func TestOperationByBatchTypeOperType(t *testing.T) {
start, err := OperationByBatchType(consts.BatchTypeSimCardStart)
if err != nil || start.OperType != "1" || start.TargetStatus != consts.SimBossCardStatusActivated {
t.Fatalf("start operation = %+v, err = %v", start, err)
}
stop, err := OperationByBatchType(consts.BatchTypeSimCardStop)
if err != nil || stop.OperType != "0" || stop.TargetStatus != consts.SimBossCardStatusStopped {
t.Fatalf("stop operation = %+v, err = %v", stop, err)
}
}
func TestValidateDetailPopulatesCardFieldsOnStatusFailure(t *testing.T) {
t.Skip("TODO: 恢复卡状态限制后启用此测试")
operation, err := OperationByBatchType(consts.BatchTypeSimCardStart)
if err != nil {
t.Fatal(err)
}
detail := &Detail{
ID: 1,
RowNo: 1,
ICCID: "iccid-1",
Card: &Card{
ID: 11,
ICCID: "iccid-1",
MSISDN: "msisdn-1",
IMSI: "imsi-1",
ChanKey: "channel",
BossCardStatus: consts.SimBossCardStatusActivated,
},
}
validateDetail(operation, detail)
if detail.Reason == "" {
t.Fatal("expected validation failure reason")
}
if detail.MSISDN != "msisdn-1" || detail.IMSI != "imsi-1" || detail.ChanKey != "channel" {
t.Fatalf("card fields not populated on status failure: %+v", detail)
}
if detail.StatusBefore != consts.SimBossCardStatusActivated {
t.Fatalf("StatusBefore = %d, want %d", detail.StatusBefore, consts.SimBossCardStatusActivated)
}
snapshot := detail.Snapshot()
if snapshot.Get("MSISDN").String() != "msisdn-1" || snapshot.Get("IMSI").String() != "imsi-1" || snapshot.Get("chanKey").String() != "channel" {
t.Fatalf("snapshot missing card fields: %s", snapshot.MustToJsonString())
}
}
func TestValidateDetailDetectsMSISDNMismatch(t *testing.T) {
operation, err := OperationByBatchType(consts.BatchTypeSimCardStart)
if err != nil {
t.Fatal(err)
}
detail := &Detail{
ID: 1,
ICCID: "iccid-1",
Authorized: true,
InputMSISDN: "msisdn-input",
Card: &Card{
ID: 11,
ICCID: "iccid-1",
MSISDN: "msisdn-card",
IMSI: "imsi-1",
ChanKey: "channel",
BossCardStatus: consts.SimBossCardStatusStopped,
},
}
validateDetail(operation, detail)
if detail.Reason != "MSISDN与卡档案不一致" {
t.Fatalf("unexpected reason: %q", detail.Reason)
}
if detail.MSISDN != "msisdn-card" || detail.InputMSISDN != "msisdn-input" {
t.Fatalf("mismatch not preserved: MSISDN=%q InputMSISDN=%q", detail.MSISDN, detail.InputMSISDN)
}
snapshot := detail.Snapshot()
if snapshot.Get("inputMsisdn").String() != "msisdn-input" || snapshot.Get("MSISDN").String() != "msisdn-card" {
t.Fatalf("snapshot mismatch fields wrong: %s", snapshot.MustToJsonString())
}
}
type fakeRepository struct {
task *Task
validationSaved bool
finalized bool
saved []Outcome
}
func (r *fakeRepository) Load(context.Context, string) (*Task, error) { return r.task, nil }
func (r *fakeRepository) SaveValidationFailure(_ context.Context, task *Task) error {
r.validationSaved = true
return nil
}
func (r *fakeRepository) SaveResult(_ context.Context, _ *Task, detail *Detail, outcome Outcome) (Outcome, error) {
detail.ApplyOutcome(outcome)
r.saved = append(r.saved, outcome)
return outcome, nil
}
func (r *fakeRepository) Finalize(context.Context, *Task) error {
r.finalized = true
return nil
}
type fakeUpstream struct {
calls int
input *model.SimCardStatusChangeInput
response []model.SimBaseRes[model.SimCardStatusChangeOutputItem]
responseError error
}
func (u *fakeUpstream) ChangeSimStatus(_ context.Context, input *model.SimCardStatusChangeInput) ([]model.SimBaseRes[model.SimCardStatusChangeOutputItem], error) {
u.calls++
u.input = input
return u.response, u.responseError
}
func TestValidateDetailCollectsIndependentDiagnostics(t *testing.T) {
detail := &Detail{ICCID: "card", Authorized: true, InputMSISDN: "wrong-phone", InputIMSI: "wrong-imsi", Card: &Card{MSISDN: "phone", IMSI: "imsi"}}
validateDetail(Operation{}, detail)
want := "ICCID缺少通道标识;MSISDN与卡档案不一致;IMSI与卡档案不一致"
if detail.Reason != want {
t.Fatalf("独立校验原因丢失: got=%q want=%q", detail.Reason, want)
}
unauthorized := &Detail{ICCID: "card", InputMSISDN: "wrong", Card: &Card{MSISDN: "phone"}}
validateDetail(Operation{}, unauthorized)
if unauthorized.Reason != "ICCID不在当前操作员授权范围内" {
t.Fatalf("越权卡不应返回其他数据差异: %q", unauthorized.Reason)
}
existing := &Detail{Reason: "原始输入格式错误", Authorized: true, Card: &Card{}}
validateDetail(Operation{}, existing)
if existing.Reason != "原始输入格式错误" {
t.Fatal("原始原因被覆盖")
}
}
@@ -0,0 +1,242 @@
package lifecycle
import (
"context"
"encoding/json"
"sort"
"strings"
"time"
"github.com/gogf/gf/v2/encoding/gjson"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gtime"
"git.5iot.com/iotlcm-public/iotlcm-worker-svr/internal/batch"
"git.5iot.com/iotlcm-public/iotlcm-worker-svr/internal/consts"
mq "git.5iot.com/iotlcm-public/iotlcm-worker-svr/internal/library/rabbitmq"
"git.5iot.com/iotlcm-public/iotlcm-worker-svr/internal/model/entity"
)
const (
restartStageStop = "stop"
restartStageResume = "resume"
)
type RestartRepository interface {
FindRestartMain(ctx context.Context, batchNo string) (*entity.BatchMain, error)
LoadRestartStop(ctx context.Context, batchNo string) (*Task, error)
LoadRestartResume(ctx context.Context, batchNo string) (*Task, error)
SaveRestartValidationFailure(ctx context.Context, task *Task, stage string) error
SaveRestartResult(ctx context.Context, task *Task, detail *Detail, outcome Outcome, stage string) (Outcome, error)
WaitRestartResume(ctx context.Context, task *Task, resumeAt *gtime.Time) error
ClaimRestartResume(ctx context.Context, batchNo string, now *gtime.Time) (bool, error)
FinalizeRestart(ctx context.Context, batchNo string) error
}
type RestartPublisher func(ctx context.Context, delayQueue, targetQueue string, delay time.Duration, data any) error
type restartExecutor struct {
repository RestartRepository
upstream Upstream
publishDelayed RestartPublisher
now func() *gtime.Time
}
func NewRestartExecutor(repository RestartRepository, upstream Upstream) Executor {
return NewRestartExecutorWithPublisher(repository, upstream, nil)
}
func NewRestartExecutorWithPublisher(repository RestartRepository, upstream Upstream, publisher RestartPublisher) Executor {
if publisher == nil {
publisher = publishRestartDelayed
}
return &restartExecutor{
repository: repository,
upstream: upstream,
publishDelayed: publisher,
now: gtime.Now,
}
}
func publishRestartDelayed(ctx context.Context, delayQueue, targetQueue string, delay time.Duration, data any) error {
body, err := json.Marshal(data)
if err != nil {
return gerror.Wrap(err, "marshal restart resume message")
}
publishCtx, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()
return mq.PublishDelayedBytesWithHeaders(publishCtx, delayQueue, targetQueue, delay, body, nil)
}
func (e *restartExecutor) CanExecuteProcessing(ctx context.Context, main *entity.BatchMain) (bool, error) {
if main == nil || main.BatchType != consts.BatchTypeSimCardRestart || main.BatchAsyncStatus != consts.BatchAsyncStatusProcessing {
return false, nil
}
if restartPhase(main.BatchInfo) != consts.RestartPhaseWaitingResume {
return false, nil
}
resumeAt := restartResumeAt(main.BatchInfo)
if resumeAt == nil {
g.Log().Warningf(ctx, "restart batch waiting without resumeAt, batchNo:%s", main.BatchNo)
return false, nil
}
now := e.currentTime()
if resumeAt.After(now) {
delay := resumeAt.Sub(now)
if delay < time.Millisecond {
delay = time.Millisecond
}
message := batch.Message{BatchNo: main.BatchNo, BatchType: main.BatchType}
if err := e.publishDelayed(ctx, consts.QueueBatchRestartResumeDelay, consts.QueueBatchExecute, delay, message); err != nil {
return false, gerror.Wrap(err, "reschedule early restart resume message")
}
g.Log().Infof(ctx, "restart batch resume message rescheduled before resumeAt, batchNo:%s, resumeAt:%s, delay:%s", main.BatchNo, resumeAt.String(), delay)
return false, nil
}
return true, nil
}
func (e *restartExecutor) ClaimsProcessingReentry() bool {
return true
}
func (e *restartExecutor) Execute(ctx context.Context, batchNo string) error {
batchNo = strings.TrimSpace(batchNo)
if batchNo == "" {
return gerror.New("restart batch number is empty")
}
main, err := e.repository.FindRestartMain(ctx, batchNo)
if err != nil {
return gerror.Wrap(err, "load restart batch main")
}
if main == nil {
return gerror.New("restart batch does not exist")
}
if restartPhase(main.BatchInfo) == consts.RestartPhaseWaitingResume {
claimed, claimErr := e.repository.ClaimRestartResume(ctx, batchNo, e.currentTime())
if claimErr != nil {
return gerror.Wrap(claimErr, "claim restart resume phase")
}
if !claimed {
g.Log().Infof(ctx, "restart resume message ignored after CAS lost, batchNo:%s", batchNo)
return nil
}
return e.executeResume(ctx, batchNo)
}
return e.executeStop(ctx, batchNo)
}
func (e *restartExecutor) executeStop(ctx context.Context, batchNo string) error {
task, err := e.repository.LoadRestartStop(ctx, batchNo)
if err != nil {
return gerror.Wrap(err, "load restart stop phase")
}
if !validateTask(task) {
if err = e.repository.SaveRestartValidationFailure(ctx, task, restartStageStop); err != nil {
return gerror.Wrap(err, "save restart stop validation failure")
}
return gerror.Wrap(e.repository.FinalizeRestart(ctx, batchNo), "finalize restart stop validation failure")
}
successCount, err := e.changeRestartGroups(ctx, task, restartStageStop)
if err != nil {
return err
}
if successCount == 0 {
return gerror.Wrap(e.repository.FinalizeRestart(ctx, batchNo), "finalize restart with no stop success")
}
delay := restartDelay(task.Batch.BatchInfo)
resumeAt := e.currentTime().Add(delay)
if err = e.repository.WaitRestartResume(ctx, task, resumeAt); err != nil {
return gerror.Wrap(err, "mark restart waiting resume")
}
message := batch.Message{BatchNo: task.Batch.BatchNo, BatchType: consts.BatchTypeSimCardRestart}
if err = e.publishDelayed(ctx, consts.QueueBatchRestartResumeDelay, consts.QueueBatchExecute, delay, message); err != nil {
return gerror.Wrap(err, "publish restart resume delayed message")
}
return nil
}
func (e *restartExecutor) executeResume(ctx context.Context, batchNo string) error {
task, err := e.repository.LoadRestartResume(ctx, batchNo)
if err != nil {
return gerror.Wrap(err, "load restart resume phase")
}
if !validateTask(task) {
if err = e.repository.SaveRestartValidationFailure(ctx, task, restartStageResume); err != nil {
return gerror.Wrap(err, "save restart resume validation failure")
}
return gerror.Wrap(e.repository.FinalizeRestart(ctx, batchNo), "finalize restart resume validation failure")
}
if _, err = e.changeRestartGroups(ctx, task, restartStageResume); err != nil {
return err
}
return gerror.Wrap(e.repository.FinalizeRestart(ctx, batchNo), "finalize restart batch")
}
func (e *restartExecutor) changeRestartGroups(ctx context.Context, task *Task, stage string) (int, error) {
groups := groupByChanKey(task.Details)
keys := make([]string, 0, len(groups))
for chanKey := range groups {
keys = append(keys, chanKey)
}
sort.Strings(keys)
successCount := 0
for _, chanKey := range keys {
outcomes := (&executor{upstream: e.upstream}).changeGroup(ctx, task, groups[chanKey])
for index, outcome := range outcomes {
detail := groups[chanKey][index]
resolved, saveErr := e.repository.SaveRestartResult(ctx, task, detail, outcome, stage)
if saveErr != nil {
return successCount, gerror.Wrapf(saveErr, "save restart %s detail %d result", stage, detail.ID)
}
detail.ApplyOutcome(resolved)
if resolved.Success {
successCount++
}
}
}
return successCount, nil
}
func (e *restartExecutor) currentTime() *gtime.Time {
if e.now != nil {
return e.now()
}
return gtime.Now()
}
func restartDelay(info *gjson.Json) time.Duration {
seconds := 300
if info != nil {
if configured := info.Get("resumeDelaySeconds").Int(); configured > 0 {
seconds = configured
}
}
return time.Duration(seconds) * time.Second
}
func restartPhase(info *gjson.Json) string {
if info == nil {
return ""
}
return strings.TrimSpace(info.Get("restart.phase").String())
}
func restartResumeAt(info *gjson.Json) *gtime.Time {
if info == nil {
return nil
}
value := strings.TrimSpace(info.Get("restart.resumeAt").String())
if value == "" {
return nil
}
parsed := gtime.NewFromStr(value)
if parsed == nil || parsed.IsZero() {
return nil
}
return parsed
}
@@ -0,0 +1,303 @@
package lifecycle
import (
"context"
"errors"
"testing"
"time"
"github.com/gogf/gf/v2/encoding/gjson"
"github.com/gogf/gf/v2/os/gtime"
"git.5iot.com/iotlcm-public/iotlcm-worker-svr/internal/batch"
"git.5iot.com/iotlcm-public/iotlcm-worker-svr/internal/consts"
"git.5iot.com/iotlcm-public/iotlcm-worker-svr/internal/model"
"git.5iot.com/iotlcm-public/iotlcm-worker-svr/internal/model/entity"
)
func TestRestartResumeBeforeScheduledTimeIsRescheduled(t *testing.T) {
now := gtime.New("2026-10-10 13:46:42")
repo := &fakeRestartRepository{
main: restartMain(consts.RestartPhaseWaitingResume, gjson.New(map[string]any{
"restart": map[string]any{"resumeAt": "2026-10-10 13:46:44"},
})),
}
publisher := &recordingRestartPublisher{}
executor := NewRestartExecutorWithPublisher(repo, &fakeUpstream{}, publisher.PublishDelayed).(*restartExecutor)
executor.now = func() *gtime.Time { return now }
ok, err := executor.CanExecuteProcessing(context.Background(), repo.main)
if ok || err != nil {
t.Fatalf("CanExecuteProcessing() = %t, %v; want false, nil", ok, err)
}
if publisher.calls != 1 || publisher.delay != 2*time.Second {
t.Fatalf("publisher calls=%d delay=%s; want one call with 2s delay", publisher.calls, publisher.delay)
}
if publisher.message != (batch.Message{BatchNo: repo.main.BatchNo, BatchType: repo.main.BatchType}) {
t.Fatalf("rescheduled message = %+v", publisher.message)
}
if repo.claimed || repo.finalized {
t.Fatal("early message must not claim or finalize the batch")
}
}
func TestRestartResumeRescheduleFailureIsReturned(t *testing.T) {
now := gtime.New("2026-10-10 13:46:42")
main := restartMain(consts.RestartPhaseWaitingResume, gjson.New(map[string]any{
"restart": map[string]any{"resumeAt": "2026-10-10 13:46:44"},
}))
publishErr := errors.New("publish failed")
publisher := func(context.Context, string, string, time.Duration, any) error { return publishErr }
executor := NewRestartExecutorWithPublisher(&fakeRestartRepository{main: main}, &fakeUpstream{}, publisher).(*restartExecutor)
executor.now = func() *gtime.Time { return now }
ok, err := executor.CanExecuteProcessing(context.Background(), main)
if ok || !errors.Is(err, publishErr) || batch.IsRetryable(err) {
t.Fatalf("CanExecuteProcessing() = %t, %v; want publish error without execution retry flag", ok, err)
}
}
func TestRestartStopValidationFailureSkipsUpstream(t *testing.T) {
task := restartStopTask()
task.Details = append(task.Details, &Detail{
ID: 2,
ICCID: "iccid-2",
Authorized: false,
Card: &Card{ID: 12, ChanKey: "channel", BossCardStatus: consts.SimBossCardStatusActivated},
})
repo := &fakeRestartRepository{
main: restartMain(consts.RestartPhaseCreated, nil),
stopTask: task,
}
upstream := &fakeUpstream{}
executor := NewRestartExecutorWithPublisher(repo, upstream, fakeRestartPublisher(t)).(*restartExecutor)
if err := executor.Execute(context.Background(), "RST-1"); err != nil {
t.Fatal(err)
}
if upstream.calls != 0 {
t.Fatalf("upstream calls = %d, want 0", upstream.calls)
}
if repo.validationStage != restartStageStop || !repo.finalized {
t.Fatalf("validation/finalize state = stage:%q finalized:%t", repo.validationStage, repo.finalized)
}
if len(repo.saved) != 0 || repo.waited {
t.Fatalf("unexpected stop result or wait: saved=%d waited=%t", len(repo.saved), repo.waited)
}
}
func TestRestartStopSuccessSchedulesDelayedResume(t *testing.T) {
now := gtime.NewFromStr("2026-07-20 10:00:00")
task := restartStopTask()
task.Batch.BatchInfo = gjson.New(map[string]any{
"resumeDelaySeconds": 1,
"restart": map[string]any{"phase": consts.RestartPhaseCreated},
})
repo := &fakeRestartRepository{
main: restartMain(consts.RestartPhaseCreated, gjson.New(map[string]any{"resumeDelaySeconds": 1})),
stopTask: task,
}
upstream := &fakeUpstream{response: []model.SimBaseRes[model.SimCardStatusChangeOutputItem]{
{Code: 0, Message: "ok", ResData: model.SimCardStatusChangeOutputItem{OrderNum: "stop-order"}},
}}
publisher := &recordingRestartPublisher{}
executor := NewRestartExecutorWithPublisher(repo, upstream, publisher.PublishDelayed).(*restartExecutor)
executor.now = func() *gtime.Time { return now }
if err := executor.Execute(context.Background(), "RST-2"); err != nil {
t.Fatal(err)
}
if upstream.calls != 1 || upstream.input.ReqData.OperType != consts.SimCardStatusOperTypeStop {
t.Fatalf("upstream stop input = %+v calls=%d", upstream.input, upstream.calls)
}
if len(repo.saved) != 1 || repo.saved[0].stage != restartStageStop || !repo.saved[0].outcome.Success {
t.Fatalf("saved stop results = %+v", repo.saved)
}
if !repo.waited || repo.resumeAt.String() != now.Add(time.Second).String() {
t.Fatalf("resume wait = waited:%t resumeAt:%v", repo.waited, repo.resumeAt)
}
message, ok := publisher.message.(batch.Message)
if !ok || publisher.calls != 1 || publisher.delay != time.Second || message.BatchType != consts.BatchTypeSimCardRestart {
t.Fatalf("publisher = %+v", publisher)
}
if repo.finalized {
t.Fatal("batch must not finalize before resume when stop succeeded")
}
}
func TestRestartResumeClaimLostSkipsDuplicateUpstream(t *testing.T) {
now := gtime.NewFromStr("2026-07-20 10:05:00")
repo := &fakeRestartRepository{
main: restartMain(consts.RestartPhaseWaitingResume, gjson.New(map[string]any{"restart": map[string]any{"resumeAt": now.String()}})),
claimResult: false,
resumeTask: restartResumeTask(),
}
upstream := &fakeUpstream{}
executor := NewRestartExecutorWithPublisher(repo, upstream, fakeRestartPublisher(t)).(*restartExecutor)
executor.now = func() *gtime.Time { return now }
if err := executor.Execute(context.Background(), "RST-3"); err != nil {
t.Fatal(err)
}
if !repo.claimed {
t.Fatal("resume CAS was not attempted")
}
if upstream.calls != 0 || repo.loadedResume || repo.finalized {
t.Fatalf("duplicate was not skipped: upstream=%d loadedResume=%t finalized=%t", upstream.calls, repo.loadedResume, repo.finalized)
}
}
func TestRestartResumeExecutesAfterClaim(t *testing.T) {
now := gtime.NewFromStr("2026-07-20 10:05:00")
repo := &fakeRestartRepository{
main: restartMain(consts.RestartPhaseWaitingResume, gjson.New(map[string]any{"restart": map[string]any{"resumeAt": now.String()}})),
claimResult: true,
resumeTask: restartResumeTask(),
}
upstream := &fakeUpstream{response: []model.SimBaseRes[model.SimCardStatusChangeOutputItem]{
{Code: 0, Message: "ok", ResData: model.SimCardStatusChangeOutputItem{OrderNum: "resume-order"}},
}}
executor := NewRestartExecutorWithPublisher(repo, upstream, fakeRestartPublisher(t)).(*restartExecutor)
executor.now = func() *gtime.Time { return now }
if ok, err := executor.CanExecuteProcessing(context.Background(), repo.main); err != nil || !ok {
t.Fatalf("CanExecuteProcessing() = %t, %v; want true, nil", ok, err)
}
if err := executor.Execute(context.Background(), "RST-4"); err != nil {
t.Fatal(err)
}
if upstream.calls != 1 || upstream.input.ReqData.OperType != consts.SimCardStatusOperTypeStart {
t.Fatalf("upstream resume input = %+v calls=%d", upstream.input, upstream.calls)
}
if len(repo.saved) != 1 || repo.saved[0].stage != restartStageResume || !repo.saved[0].outcome.Success {
t.Fatalf("saved resume results = %+v", repo.saved)
}
if !repo.finalized {
t.Fatal("resume phase did not finalize the RST batch")
}
}
func restartMain(phase string, info *gjson.Json) *entity.BatchMain {
data := map[string]any{"restart": map[string]any{"phase": phase}}
if info != nil {
for key, value := range info.Map() {
if key == "restart" {
restart := data["restart"].(map[string]any)
for restartKey, restartValue := range gjson.New(value).Map() {
restart[restartKey] = restartValue
}
continue
}
data[key] = value
}
}
return &entity.BatchMain{
BatchNo: "RST",
BatchType: consts.BatchTypeSimCardRestart,
BatchAsyncStatus: consts.BatchAsyncStatusProcessing,
BatchInfo: gjson.New(data),
}
}
func restartStopTask() *Task {
return &Task{
Batch: Batch{
BatchNo: "RST",
BatchType: consts.BatchTypeSimCardRestart,
BatchInfo: gjson.New(map[string]any{"restart": map[string]any{"phase": consts.RestartPhaseCreated}}),
},
Operation: Operation{OperType: consts.SimCardStatusOperTypeStop, ExpectedStatus: consts.SimBossCardStatusActivated, TargetStatus: consts.SimBossCardStatusStopped},
Details: []*Detail{
{ID: 1, ICCID: "iccid-1", Authorized: true, Card: &Card{ID: 11, ChanKey: "channel", BossCardStatus: consts.SimBossCardStatusActivated}},
},
}
}
func restartResumeTask() *Task {
return &Task{
Batch: Batch{BatchNo: "RST", BatchType: consts.BatchTypeSimCardRestart},
Operation: Operation{OperType: consts.SimCardStatusOperTypeStart, ExpectedStatus: consts.SimBossCardStatusStopped, TargetStatus: consts.SimBossCardStatusActivated},
Details: []*Detail{
{ID: 1, ICCID: "iccid-1", Authorized: true, Card: &Card{ID: 11, ChanKey: "channel", BossCardStatus: consts.SimBossCardStatusStopped}},
},
}
}
type fakeRestartRepository struct {
main *entity.BatchMain
stopTask *Task
resumeTask *Task
validationStage string
saved []restartSavedResult
waited bool
resumeAt *gtime.Time
claimed bool
claimResult bool
loadedResume bool
finalized bool
}
type restartSavedResult struct {
stage string
outcome Outcome
}
func (r *fakeRestartRepository) FindRestartMain(context.Context, string) (*entity.BatchMain, error) {
return r.main, nil
}
func (r *fakeRestartRepository) LoadRestartStop(context.Context, string) (*Task, error) {
return r.stopTask, nil
}
func (r *fakeRestartRepository) LoadRestartResume(context.Context, string) (*Task, error) {
r.loadedResume = true
return r.resumeTask, nil
}
func (r *fakeRestartRepository) SaveRestartValidationFailure(_ context.Context, _ *Task, stage string) error {
r.validationStage = stage
return nil
}
func (r *fakeRestartRepository) SaveRestartResult(_ context.Context, _ *Task, detail *Detail, outcome Outcome, stage string) (Outcome, error) {
detail.ApplyOutcome(outcome)
r.saved = append(r.saved, restartSavedResult{stage: stage, outcome: outcome})
return outcome, nil
}
func (r *fakeRestartRepository) WaitRestartResume(_ context.Context, _ *Task, resumeAt *gtime.Time) error {
r.waited = true
r.resumeAt = resumeAt
return nil
}
func (r *fakeRestartRepository) ClaimRestartResume(context.Context, string, *gtime.Time) (bool, error) {
r.claimed = true
return r.claimResult, nil
}
func (r *fakeRestartRepository) FinalizeRestart(context.Context, string) error {
r.finalized = true
return nil
}
type recordingRestartPublisher struct {
calls int
delay time.Duration
message any
}
func (p *recordingRestartPublisher) PublishDelayed(_ context.Context, _, _ string, delay time.Duration, data any) error {
p.calls++
p.delay = delay
p.message = data
return nil
}
func fakeRestartPublisher(t *testing.T) RestartPublisher {
t.Helper()
return func(context.Context, string, string, time.Duration, any) error {
t.Fatal("publisher must not be called")
return nil
}
}
@@ -0,0 +1,134 @@
package lifecycle
import (
"context"
"strings"
"github.com/gogf/gf/v2/encoding/gjson"
"github.com/gogf/gf/v2/os/gtime"
"git.5iot.com/iotlcm-public/iotlcm-worker-svr/internal/model"
)
type Operation struct {
BatchType int
OperType string
ExpectedStatus int
TargetStatus int
}
type Batch struct {
ID int64
BatchNo string
BatchType int
TenantID string
CreatedBy int64
CreatedDept int64
StartTime *gtime.Time
BatchInfo *gjson.Json
}
type Card struct {
ID int64
ICCID string
MSISDN string
IMSI string
ChanKey string
BossCardStatus int
}
type Detail struct {
ID int64
RowNo int
ICCID string
InputMSISDN string
InputIMSI string
MSISDN string
IMSI string
Authorized bool
ChanKey string
StatusBefore int
Reason string
OrderNum string
UpstreamCode int
UpstreamReason string
Card *Card
OriginalSnapshot *gjson.Json
}
type Outcome struct {
Success bool
Code int
Message string
OrderNum string
}
type Task struct {
Batch Batch
Operation Operation
Details []*Detail
ValidationOK bool
FailureReason string
}
func (d *Detail) ApplyOutcome(outcome Outcome) {
d.OrderNum = strings.TrimSpace(outcome.OrderNum)
d.UpstreamCode = outcome.Code
d.UpstreamReason = strings.TrimSpace(outcome.Message)
if outcome.Success {
d.Reason = ""
return
}
if d.UpstreamReason == "" {
d.UpstreamReason = "upstream processing failed"
}
d.Reason = d.UpstreamReason
}
func (d *Detail) Snapshot() *gjson.Json {
if d == nil {
return gjson.New(nil)
}
return gjson.New(detailSnapshot{
ICCID: d.ICCID,
InputMSISDN: d.InputMSISDN,
InputIMSI: d.InputIMSI,
MSISDN: d.MSISDN,
IMSI: d.IMSI,
Authorized: d.Authorized,
ChanKey: d.ChanKey,
StatusBefore: d.StatusBefore,
OrderNum: d.OrderNum,
UpstreamCode: d.UpstreamCode,
UpstreamMessage: d.UpstreamReason,
})
}
type detailSnapshot struct {
ICCID string `json:"ICCID"`
InputMSISDN string `json:"inputMsisdn"`
InputIMSI string `json:"inputImsi"`
MSISDN string `json:"MSISDN"`
IMSI string `json:"IMSI"`
Authorized bool `json:"authorized"`
ChanKey string `json:"chanKey"`
StatusBefore int `json:"statusBefore"`
OrderNum string `json:"orderNum"`
UpstreamCode int `json:"upstreamCode"`
UpstreamMessage string `json:"upstreamMessage"`
}
type Repository interface {
Load(ctx context.Context, batchNo string) (*Task, error)
SaveValidationFailure(ctx context.Context, task *Task) error
SaveResult(ctx context.Context, task *Task, detail *Detail, outcome Outcome) (Outcome, error)
Finalize(ctx context.Context, task *Task) error
}
type Upstream interface {
ChangeSimStatus(ctx context.Context, input *model.SimCardStatusChangeInput) ([]model.SimBaseRes[model.SimCardStatusChangeOutputItem], error)
}
type Executor interface {
Execute(ctx context.Context, batchNo string) error
}