sync: 同步 v1.0.4 开源代码并脱敏
This commit is contained in:
@@ -9,13 +9,34 @@ import (
|
||||
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
|
||||
"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"
|
||||
)
|
||||
|
||||
type executor struct {
|
||||
repository Repository
|
||||
upstream Upstream
|
||||
repository Repository
|
||||
upstream Upstream
|
||||
channelSlots sync.Map
|
||||
}
|
||||
|
||||
func (e *executor) ClaimsProcessingReentry() bool { return true }
|
||||
func (e *executor) CanExecuteProcessing(ctx context.Context, main *entity.BatchMain) (bool, error) {
|
||||
repo, durable := e.repository.(SubBatchRepository)
|
||||
_, async := e.upstream.(AsyncUpstream)
|
||||
if !durable || !async {
|
||||
return false, nil
|
||||
}
|
||||
return repo.HasDurableParent(ctx, strings.TrimSpace(main.BatchNo))
|
||||
}
|
||||
func (e *executor) ProtectExecutionFailure(ctx context.Context, batchNo string, batchType int, reason string) (bool, error) {
|
||||
repo, durable := e.repository.(SubBatchRepository)
|
||||
_, async := e.upstream.(AsyncUpstream)
|
||||
if !durable || !async {
|
||||
return false, nil
|
||||
}
|
||||
return true, repo.ProtectExecutionFailure(ctx, batchNo, reason)
|
||||
}
|
||||
|
||||
func NewExecutor(repository Repository, upstream Upstream) Executor {
|
||||
@@ -23,6 +44,18 @@ func NewExecutor(repository Repository, upstream Upstream) Executor {
|
||||
}
|
||||
|
||||
func (e *executor) Execute(ctx context.Context, batchNo string) error {
|
||||
if durable, ok := e.repository.(SubBatchRepository); ok {
|
||||
if _, ok := e.upstream.(AsyncUpstream); ok {
|
||||
// Earlier child intents were durably frozen: a re-delivered parent cannot re-split running rows.
|
||||
return batch.Retryable(durable.ResumeOrPrepare(ctx, strings.TrimSpace(batchNo), func(prepareCtx context.Context) error {
|
||||
return e.executeFresh(prepareCtx, batchNo)
|
||||
}))
|
||||
}
|
||||
}
|
||||
return e.executeFresh(ctx, batchNo)
|
||||
}
|
||||
|
||||
func (e *executor) executeFresh(ctx context.Context, batchNo string) error {
|
||||
task, err := e.repository.Load(ctx, strings.TrimSpace(batchNo))
|
||||
if err != nil {
|
||||
return gerror.Wrap(err, "load data service batch")
|
||||
@@ -34,6 +67,11 @@ func (e *executor) Execute(ctx context.Context, batchNo string) error {
|
||||
if err = e.repository.SaveValidationFailure(ctx, task); err != nil {
|
||||
return gerror.Wrap(err, "save data service validation failure")
|
||||
}
|
||||
if _, durable := e.repository.(SubBatchRepository); durable {
|
||||
if _, async := e.upstream.(AsyncUpstream); async {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return gerror.Wrap(e.repository.Finalize(ctx, task), "finalize data service validation failure")
|
||||
}
|
||||
|
||||
@@ -45,6 +83,11 @@ func (e *executor) executeTrafficCutoff(ctx context.Context, task *Task) error {
|
||||
if err := e.repository.SaveValidationFailure(ctx, task); err != nil {
|
||||
return gerror.Wrap(err, "save traffic cutoff validation failure")
|
||||
}
|
||||
if _, durable := e.repository.(SubBatchRepository); durable {
|
||||
if _, async := e.upstream.(AsyncUpstream); async {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return gerror.Wrap(e.repository.Finalize(ctx, task), "finalize traffic cutoff validation failure")
|
||||
}
|
||||
if err := e.refreshDataServiceState(ctx, task); err != nil {
|
||||
@@ -53,6 +96,12 @@ func (e *executor) executeTrafficCutoff(ctx context.Context, task *Task) error {
|
||||
if err := e.saveInvalidTrafficCutoffRows(ctx, task); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, durable := e.repository.(SubBatchRepository); durable {
|
||||
if _, async := e.upstream.(AsyncUpstream); async {
|
||||
// 本地disabled不能证明前序开通已终结;持久化路径统一经过卡级提交屏障。
|
||||
return e.executeValidated(ctx, task)
|
||||
}
|
||||
}
|
||||
if err := e.saveIdempotentCutoffSkips(ctx, task); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -60,6 +109,26 @@ func (e *executor) executeTrafficCutoff(ctx context.Context, task *Task) error {
|
||||
}
|
||||
|
||||
func (e *executor) executeValidated(ctx context.Context, task *Task) error {
|
||||
if durable, ok := e.repository.(SubBatchRepository); ok {
|
||||
if async, ok := e.upstream.(AsyncUpstream); ok {
|
||||
seen := map[string]bool{}
|
||||
for _, detail := range task.Details {
|
||||
if detail == nil || detail.Reason != "" || (detail.DetailStatus != consts.BatchDetailStatusPending && detail.DetailStatus != consts.BatchDetailStatusRunning) {
|
||||
continue
|
||||
}
|
||||
if seen[detail.ICCID] {
|
||||
resolved, err := e.repository.SaveResult(ctx, task, detail, Outcome{Message: "重复ICCID,未提交上游"})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
detail.ApplyOutcome(resolved)
|
||||
continue
|
||||
}
|
||||
seen[detail.ICCID] = true
|
||||
}
|
||||
return e.prepareAndSubmit(ctx, task, durable, async)
|
||||
}
|
||||
}
|
||||
groups := groupByChanKey(task.Operation, task.Details)
|
||||
keys := make([]string, 0, len(groups))
|
||||
for chanKey := range groups {
|
||||
@@ -96,7 +165,7 @@ func (e *executor) saveIdempotentCutoffSkips(ctx context.Context, task *Task) er
|
||||
}
|
||||
resolved, saveErr := e.repository.SaveResult(ctx, task, detail, Outcome{
|
||||
Success: true,
|
||||
Message: "data service is already disabled; upstream request was skipped",
|
||||
Message: "数据服务已关闭,未发起上游请求",
|
||||
})
|
||||
if saveErr != nil {
|
||||
return gerror.Wrapf(saveErr, "save data service detail %d idempotent skip result", detail.ID)
|
||||
@@ -108,6 +177,9 @@ func (e *executor) saveIdempotentCutoffSkips(ctx context.Context, task *Task) er
|
||||
|
||||
func validateTask(task *Task) bool {
|
||||
if task == nil || len(task.Details) == 0 {
|
||||
if task != nil && task.FailureReason == "" {
|
||||
task.FailureReason = "数据开停批次没有有效明细"
|
||||
}
|
||||
return false
|
||||
}
|
||||
hasFailure := false
|
||||
@@ -132,17 +204,17 @@ func validateTask(task *Task) bool {
|
||||
}
|
||||
for _, detail := range task.Details {
|
||||
if detail != nil && detail.Reason == "" {
|
||||
detail.Reason = "same batch contains validation failure; upstream request was skipped"
|
||||
detail.Reason = "同批次存在校验失败卡,本卡未请求上游"
|
||||
}
|
||||
}
|
||||
task.FailureReason = "batch validation failed; upstream request was skipped"
|
||||
task.FailureReason = "批次校验未通过,未发起上游请求"
|
||||
return false
|
||||
}
|
||||
|
||||
func validateTrafficCutoffTask(task *Task) bool {
|
||||
if task == nil || len(task.Details) == 0 {
|
||||
if task != nil {
|
||||
task.FailureReason = "traffic cutoff batch has no valid details"
|
||||
task.FailureReason = "流量断网批次无有效明细"
|
||||
}
|
||||
return false
|
||||
}
|
||||
@@ -160,13 +232,13 @@ func validateTrafficCutoffTask(task *Task) bool {
|
||||
if hasSuccess {
|
||||
task.ValidationOK = true
|
||||
if failed := countFailedDetails(task); failed > 0 {
|
||||
task.FailureReason = strconv.Itoa(failed) + " SIM cards failed validation"
|
||||
task.FailureReason = strconv.Itoa(failed) + " 张卡未通过校验"
|
||||
} else {
|
||||
task.FailureReason = ""
|
||||
}
|
||||
return true
|
||||
}
|
||||
task.FailureReason = "traffic cutoff batch validation failed; upstream request was skipped"
|
||||
task.FailureReason = "流量断网批次校验未通过,未发起上游请求"
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -188,7 +260,7 @@ func validateDetail(_ Operation, detail *Detail) {
|
||||
return
|
||||
}
|
||||
if detail.Card == nil {
|
||||
detail.Reason = "ICCID does not exist in SIM card records"
|
||||
detail.Reason = "ICCID不存在或不属于当前租户"
|
||||
return
|
||||
}
|
||||
detail.MSISDN = strings.TrimSpace(detail.Card.MSISDN)
|
||||
@@ -198,26 +270,28 @@ func validateDetail(_ Operation, detail *Detail) {
|
||||
detail.StatusAfter = detail.Card.DataServiceStatus
|
||||
|
||||
if !detail.Authorized {
|
||||
detail.Reason = "ICCID is outside the current operator authorization scope"
|
||||
detail.Reason = "ICCID不在当前操作员授权范围内"
|
||||
return
|
||||
}
|
||||
var reasons []string
|
||||
if detail.ChanKey == "" {
|
||||
detail.Reason = "ICCID is missing chanKey"
|
||||
return
|
||||
reasons = append(reasons, "ICCID缺少通道标识")
|
||||
}
|
||||
if detail.InputMSISDN != "" && detail.InputMSISDN != detail.MSISDN {
|
||||
detail.Reason = "MSISDN does not match the SIM card record"
|
||||
return
|
||||
reasons = append(reasons, "MSISDN与卡档案不一致")
|
||||
}
|
||||
if detail.InputIMSI != "" && detail.InputIMSI != detail.IMSI {
|
||||
detail.Reason = "IMSI does not match the SIM card record"
|
||||
return
|
||||
reasons = append(reasons, "IMSI与卡档案不一致")
|
||||
}
|
||||
detail.Reason = strings.Join(reasons, ";")
|
||||
}
|
||||
|
||||
func groupByChanKey(operation Operation, details []*Detail) map[string][]*Detail {
|
||||
groups := make(map[string][]*Detail)
|
||||
for _, detail := range details {
|
||||
if detail == nil || detail.Reason != "" {
|
||||
continue
|
||||
}
|
||||
if operation.BatchType == consts.BatchTypeDataCut && (detail == nil || !detail.ValidationPassed || detail.Reason != "") {
|
||||
continue
|
||||
}
|
||||
@@ -316,7 +390,7 @@ func (e *executor) changeSubBatch(ctx context.Context, task *Task, details []*De
|
||||
if err != nil {
|
||||
outcomes := make([]Outcome, len(details))
|
||||
for index := range outcomes {
|
||||
outcomes[index].Message = "upstream request failed: " + err.Error()
|
||||
outcomes[index].Message = "请求上游失败:" + err.Error()
|
||||
}
|
||||
return outcomes
|
||||
}
|
||||
@@ -354,14 +428,14 @@ func mapOutcomes(details []*Detail, response []model.SimBaseRes[model.SimCardApn
|
||||
matched = detailIndex
|
||||
}
|
||||
if matched < 0 {
|
||||
outcomes[detailIndex] = Outcome{Message: "upstream did not return a result for this SIM card"}
|
||||
outcomes[detailIndex] = Outcome{Message: "上游未返回该卡处理结果"}
|
||||
continue
|
||||
}
|
||||
used[matched] = true
|
||||
item := response[matched]
|
||||
message := strings.TrimSpace(item.Message)
|
||||
if item.Code != 0 && message == "" {
|
||||
message = "upstream processing failed"
|
||||
message = "上游处理失败"
|
||||
}
|
||||
outcomes[detailIndex] = Outcome{Success: item.Code == 0, Code: item.Code, Message: message, OrderNum: strings.TrimSpace(item.ResData.OrderNum)}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user