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 上下文 // TaskContext 上下文
type TaskContext struct { type TaskContext struct {
hunter *Hunter hunter *Hunter
curPath string
curTaskID string
workflow *requests.Workflow workflow *requests.Workflow
parent ITaskNode parent ITaskNode
@ -69,12 +65,12 @@ func (cxt *TaskContext) SetWorkflow(workflow *requests.Workflow) {
// TaskID Get Task ID // TaskID Get Task ID
func (cxt *TaskContext) TaskID() string { func (cxt *TaskContext) TaskID() string {
return cxt.curTaskID return cxt.current.TaskID()
} }
// Path curren Task tree path. // Path curren Task tree path.
func (cxt *TaskContext) Path() string { func (cxt *TaskContext) Path() string {
return cxt.curPath return cxt.current.Path()
} }
// Hunt Hunt() = cxt.Workflow().Execute() // Hunt Hunt() = cxt.Workflow().Execute()

View File

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

View File

@ -67,7 +67,8 @@ func TestCasePostForm(t *testing.T) {
} }
if iform, ok := data["form"]; ok { 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) t.Error(iform)
} }
} }
@ -79,17 +80,34 @@ type WebSub struct {
func (web *WebSub) Execute(cxt *TaskContext) { func (web *WebSub) Execute(cxt *TaskContext) {
wf := cxt.Workflow() wf := cxt.Workflow()
wf.SetBodyAuto(`{"a": "1"}`) wf.SetBodyAuto(`{"a": "1","url":["http://httpbin.org/post","http://httpbin.org/get"]}`)
resp, err := wf.Execute() resp, err := wf.Execute()
if err != nil { if err != nil {
panic(err) panic(err)
} }
cxt.SetShare("test", resp.Content()) 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) { func TestCaseWebSub(t *testing.T) {
hunter := NewHunter() hunter := NewHunter()
hunter.AddTask(&WebSub{PrePostUrl: "http://httpbin.org/post"}) hunter.AddTask(&WebSub{"http://httpbin.org/post"})
hunter.Execute() hunter.Execute()
data := make(map[string]interface{}) data := make(map[string]interface{})

18
task.go
View File

@ -42,6 +42,10 @@ type ITaskNode interface {
SetPath(path string) SetPath(path string)
Path() string Path() string
TaskID() string
SetID(tid string)
} }
// BaseTask 任务,必须包含子任务. 执行了第一个 // BaseTask 任务,必须包含子任务. 执行了第一个
@ -50,6 +54,7 @@ type BaseTask struct {
children *pqueue.PriorityQueue // ITask类型 children *pqueue.PriorityQueue // ITask类型
path string // id 联合体, 禁用.命名 path string // id 联合体, 禁用.命名
task ITask task ITask
tid string
} }
// Parent 父 // Parent 父
@ -92,6 +97,19 @@ func (task *BaseTask) SetPath(path string) {
task.path = path 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 孩子节点 // // Task 孩子节点
// func (task *BaseTask) Task() ITask { // func (task *BaseTask) Task() ITask {
// return *task // return *task