This commit is contained in:
huangsimin 2020-04-09 18:41:30 +08:00
parent 05e14a3f7c
commit 5600e845da
4 changed files with 60 additions and 25 deletions

View File

@ -4,11 +4,7 @@ import "github.com/474420502/requests"
// TaskContext 上下文
type TaskContext struct {
hunter *Hunter
curPath string
curTaskID string
hunter *Hunter
workflow *requests.Workflow
parent ITaskNode
@ -69,12 +65,12 @@ func (cxt *TaskContext) SetWorkflow(workflow *requests.Workflow) {
// TaskID Get Task ID
func (cxt *TaskContext) TaskID() string {
return cxt.curTaskID
return cxt.current.TaskID()
}
// Path curren Task tree path.
func (cxt *TaskContext) Path() string {
return cxt.curPath
return cxt.current.Path()
}
// Hunt Hunt() = cxt.Workflow().Execute()

View File

@ -93,14 +93,14 @@ func (hunter *Hunter) execute(task ITask) {
cxt := NewContext()
btask := &BaseTask{}
// btask.SetTask(task)
btask.SetTask(task)
btask.SetParent(nil)
btask.SetChildren(hunter.createQueue())
cxt.current = btask
cxt.hunter = hunter
cxt.AddTask(task)
cxt.parent = btask
cxt.parent.Children().Push(btask)
cxt.hunter = hunter
hunter.recursionTasks(cxt)
}
@ -108,21 +108,22 @@ func (hunter *Hunter) recursionTasks(cxt *TaskContext) {
autoid := 0
for children := cxt.current.Children(); children != nil && children.Size() > 0; {
for children := cxt.parent.Children(); children != nil && children.Size() > 0; {
if itask, ok := children.Pop(); ok {
sautoid := strconv.Itoa(autoid)
ncxt := NewContext()
tasknode := itask.(ITaskNode)
tasknode.SetID(sautoid)
cxt.current = tasknode
task := tasknode.Task()
ncxt.curPath = cxt.current.Path()
if itid, ok := task.(IIdentity); ok {
ncxt.curTaskID = itid.GetID()
} else {
ncxt.curTaskID = strconv.Itoa(autoid)
autoid++
}
// if itid, ok := task.(IIdentity); ok {
// ncxt.curTaskID = itid.GetID()
// } else {
// ncxt.curTaskID = sautoid
// }
if before, ok := task.(IBefore); ok {
before.Before(cxt)
@ -134,10 +135,12 @@ func (hunter *Hunter) recursionTasks(cxt *TaskContext) {
after.After(cxt)
}
tasknode.SetPath(cxt.current.Path() + "." + ncxt.curTaskID)
tasknode.SetPath(cxt.parent.Path() + "." + cxt.TaskID())
ncxt.parent = cxt.current
ncxt.current = tasknode
ncxt.hunter = cxt.hunter
hunter.recursionTasks(ncxt)
autoid++
}
}

View File

@ -67,7 +67,8 @@ func TestCasePostForm(t *testing.T) {
}
if iform, ok := data["form"]; ok {
if iform.(string) != "hello form" {
form := iform.(map[string]interface{})
if form["param"].(string) != "hello form" {
t.Error(iform)
}
}
@ -79,17 +80,34 @@ type WebSub struct {
func (web *WebSub) Execute(cxt *TaskContext) {
wf := cxt.Workflow()
wf.SetBodyAuto(`{"a": "1"}`)
wf.SetBodyAuto(`{"a": "1","url":["http://httpbin.org/post","http://httpbin.org/get"]}`)
resp, err := wf.Execute()
if err != nil {
panic(err)
}
cxt.SetShare("test", resp.Content())
data := make(map[string]interface{})
json.Unmarshal([]byte(resp.ContentBytes()), &data)
if urlList, ok := data["json"].(map[string]interface{})["url"].([]interface{}); ok {
for _, is := range urlList {
s := is.(string)
cxt.AddTask(&WebSub1{PrePostUrl(s)})
}
}
}
type WebSub1 struct {
PrePostUrl
}
func (web *WebSub1) Execute(cxt *TaskContext) {
log.Panic(cxt.Path() + "." + cxt.TaskID())
}
func TestCaseWebSub(t *testing.T) {
hunter := NewHunter()
hunter.AddTask(&WebSub{PrePostUrl: "http://httpbin.org/post"})
hunter.AddTask(&WebSub{"http://httpbin.org/post"})
hunter.Execute()
data := make(map[string]interface{})

18
task.go
View File

@ -42,6 +42,10 @@ type ITaskNode interface {
SetPath(path string)
Path() string
TaskID() string
SetID(tid string)
}
// BaseTask 任务,必须包含子任务. 执行了第一个
@ -50,6 +54,7 @@ type BaseTask struct {
children *pqueue.PriorityQueue // ITask类型
path string // id 联合体, 禁用.命名
task ITask
tid string
}
// Parent 父
@ -92,6 +97,19 @@ func (task *BaseTask) SetPath(path string) {
task.path = path
}
// TaskID 如果存在任务id就返回任务id, 否则生成自动生成的序列id
func (task *BaseTask) TaskID() string {
if itid, ok := task.task.(IIdentity); ok {
return itid.GetID()
}
return task.tid
}
// SetID 设置自动生成的序列id
func (task *BaseTask) SetID(tid string) {
task.tid = tid
}
// // Task 孩子节点
// func (task *BaseTask) Task() ITask {
// return *task