-
Notifications
You must be signed in to change notification settings - Fork 66
/
processor.go
181 lines (144 loc) · 4.53 KB
/
processor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package agentExec
import (
"encoding/json"
"fmt"
agentDomain "github.com/aaronchen2k/deeptest/internal/agent/exec/domain"
execUtils "github.com/aaronchen2k/deeptest/internal/agent/exec/utils/exec"
"github.com/aaronchen2k/deeptest/internal/pkg/consts"
commonUtils "github.com/aaronchen2k/deeptest/pkg/lib/comm"
"github.com/aaronchen2k/deeptest/pkg/lib/log"
"github.com/kataras/iris/v12"
"runtime/debug"
"time"
)
type Processor struct {
ProcessorBase
Entity IProcessorEntity `json:"entity"`
Disable bool `json:"disable"`
}
type ProcessorMsg struct {
ProcessorBase
}
type ProcessorBase struct {
ID uint `json:"id"`
Name string `json:"name"`
Comments string `json:"comments"`
Method consts.HttpMethod `json:"method" yaml:"method"`
ParentId uint `json:"parentId"`
ScenarioId uint `json:"scenarioId"`
ProjectId uint `json:"projectId"`
UseID uint `json:"useId"`
EntityCategory consts.ProcessorCategory `json:"entityCategory"`
EntityType consts.ProcessorType `json:"entityType"`
EntityId uint `json:"entityId"`
EndpointInterfaceId uint `json:"endpointInterfaceId"`
Ordr int `json:"ordr"`
Children []*Processor `json:"children"`
Slots iris.Map `json:"slots"`
IsDir bool `json:"isDir"`
EntityRaw json.RawMessage `json:"entityRaw"`
Parent *Processor `json:"-"`
Result *agentDomain.ScenarioExecResult `json:"result"`
ProcessorInterfaceSrc consts.ProcessorInterfaceSrc `json:"processorInterfaceSrc"`
Round string `json:"round"`
Session Session `json:"-"`
}
func (p *Processor) Run(s *Session) (err error) {
_logUtils.Infof("%d - %s %s", p.ID, p.Name, p.EntityType)
SetCurrScenarioProcessorId(s.ExecUuid, p.ID)
SetCurrScenarioProcessor(s.ExecUuid, p)
//每个执行器延迟0.1秒,防止发送ws消息过快,导致前端消息错误
time.Sleep(100 * time.Microsecond)
if !p.Disable && p.Entity != nil && !GetForceStopExec(s.ExecUuid) {
p.Entity.Run(p, s)
}
return
}
func (p *Processor) Error(s *Session, err interface{}) {
var detail map[string]interface{}
commonUtils.JsonDecode(p.Result.Detail, &detail)
if detail == nil {
detail = map[string]interface{}{}
}
detail["exception"] = fmt.Sprintf("错误:%v", err)
p.Result.Detail = commonUtils.JsonEncode(detail)
fmt.Printf("err=%v, stack=%s\n", err, string(debug.Stack()))
p.AddResultToParent()
execUtils.SendExecMsg(p.Result, consts.Processor, s.WsMsg)
//execUtils.SendExceptionMsg(s.WsMsg)
}
func (p *Processor) AddResultToParent() (err error) {
p.Parent.Result.Children = append(p.Parent.Result.Children, p.Result)
return
}
func (p *Processor) RestoreEntity() (err error) {
bytes, err := p.EntityRaw.MarshalJSON()
switch p.EntityCategory {
case consts.ProcessorInterface:
ret := ProcessorInterface{}
json.Unmarshal(bytes, &ret)
p.Entity = ret
case consts.ProcessorRoot:
ret := ProcessorRoot{}
json.Unmarshal(bytes, &ret)
p.Entity = ret
case consts.ProcessorGroup:
ret := ProcessorGroup{}
json.Unmarshal(bytes, &ret)
p.Entity = ret
case consts.ProcessorLogic:
ret := ProcessorLogic{}
json.Unmarshal(bytes, &ret)
p.Entity = ret
case consts.ProcessorLoop:
ret := ProcessorLoop{}
json.Unmarshal(bytes, &ret)
p.Entity = ret
case consts.ProcessorVariable:
ret := ProcessorVariable{}
json.Unmarshal(bytes, &ret)
p.Entity = ret
case consts.ProcessorTimer:
ret := ProcessorTimer{}
json.Unmarshal(bytes, &ret)
p.Entity = ret
case consts.ProcessorPrint:
ret := ProcessorPrint{}
json.Unmarshal(bytes, &ret)
p.Entity = ret
case consts.ProcessorCookie:
ret := ProcessorCookie{}
json.Unmarshal(bytes, &ret)
p.Entity = ret
case consts.ProcessorAssertion:
ret := ProcessorAssertion{}
json.Unmarshal(bytes, &ret)
p.Entity = ret
case consts.ProcessorData:
ret := ProcessorData{}
json.Unmarshal(bytes, &ret)
p.Entity = ret
case consts.ProcessorCustomCode:
ret := ProcessorCustomCode{}
json.Unmarshal(bytes, &ret)
p.Entity = ret
default:
}
return
}
func (p *Processor) AppendNewChildProcessor(category consts.ProcessorCategory, typ consts.ProcessorType) (child Processor) {
child = Processor{
ProcessorBase: ProcessorBase{
EntityCategory: category,
EntityType: typ,
Parent: p,
ParentId: p.ID,
},
}
child.Result = &agentDomain.ScenarioExecResult{
ProcessorCategory: child.EntityCategory,
ProcessorType: child.EntityType,
ParentId: int(p.ID),
}
return
}