2 * Copyright 2020 Huawei Technologies Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
23 type PlanIf interface {
24 SetErrorStep(stepName string)
25 UsingSpace(spaceName string)
26 RunBackground(task ...interface{})
27 RunParallel(task ...interface{})
28 RunSerial(task ...interface{})
31 type SpaceIf interface {
39 TaskContinue ErrCode = -1
44 const FINALLY string = "finally"
51 StepObjs []interface{}
54 func (s *SubGrp) Install(task []interface{}) bool {
55 for _, stepObj := range task {
59 s.StepObjs = append(s.StepObjs, stepObj)
64 type SerErrInfo struct {
71 type PlanBase struct {
81 func (b *PlanBase) SetErrorStep(stepName string) {
85 func (b *PlanBase) UsingSpace(spaceName string) {
86 b.SpaceName = spaceName
89 func (b *PlanBase) RunBackground(task ...interface{}) {
90 b.LoadTask(GoBackground, task)
93 func (b *PlanBase) RunParallel(task ...interface{}) {
94 b.LoadTask(GoParallel, task)
97 func (b *PlanBase) RunSerial(task ...interface{}) {
98 b.LoadTask(GoSerial, task)
101 func (b *PlanBase) RunSerialName(task interface{}, name string) {
103 subGrp.Policy = GoSerial
104 subGrp.Install([]interface{}{task})
105 b.LoadData([]interface{}{task})
106 subGrp.StepNames = []string{name}
107 b.PlanGrp = append(b.PlanGrp, subGrp)
110 func (b *PlanBase) Try(task ...interface{}) {
111 b.SetErrorStep(FINALLY)
112 b.LoadTask(GoSerial, task)
115 func (b *PlanBase) Finally(task interface{}) {
117 subGrp.Policy = GoSerial
118 subGrp.Install([]interface{}{task})
119 b.LoadData([]interface{}{task})
120 subGrp.StepNames = []string{FINALLY}
121 b.PlanGrp = append(b.PlanGrp, subGrp)
124 func (b *PlanBase) LoadTask(policy GoPolicy, task []interface{}) {
126 subGrp.Policy = policy
129 b.PlanGrp = append(b.PlanGrp, subGrp)
132 func (b *PlanBase) LoadData(task []interface{}) bool {
133 for _, stepObj := range task {
137 stepIf, ok := stepObj.(TaskBaseIf)
141 stepIf.SetSerErrInfo(b.SerError)
146 type SpaceBase struct {
150 func (s *SpaceBase) Init() {
151 s.SerError = &SerErrInfo{}
154 func (s *SpaceBase) getPlan() *PlanBase {
158 func GotoErrorStep(curPlan *PlanBase, grpNum int) bool {
159 for idx, stepName := range curPlan.PlanGrp[grpNum].StepNames {
160 if stepName == curPlan.ErrStep {
161 if curPlan.CurGrpIdx > grpNum {
164 curPlan.CurGrpIdx = grpNum
165 if curPlan.PlanGrp[grpNum].CurStepIdx < idx {
166 curPlan.PlanGrp[grpNum].CurStepIdx = idx
174 func RecordErrInfo(curPlan *PlanBase, stepIdx int) {
175 if curPlan.CurGrpIdx >= len(curPlan.PlanGrp) {
178 curGrp := curPlan.PlanGrp[curPlan.CurGrpIdx]
179 if stepIdx < 0 || stepIdx >= len(curGrp.StepObjs) {
183 curPlan.SerError.GrpIdx = curPlan.CurGrpIdx
184 curPlan.SerError.TaskIdx = stepIdx
185 curStep := curGrp.StepObjs[stepIdx]
186 stepIf, ok := curStep.(TaskBaseIf)
190 errCode, msg := stepIf.GetErrCode()
191 curPlan.SerError.ErrCode = int(errCode)
192 curPlan.SerError.Message = msg
195 func GotoErrorProc(curPlan *PlanBase) {
197 for grpNum := 0; grpNum < len(curPlan.PlanGrp); grpNum++ {
198 done := GotoErrorStep(curPlan, grpNum)