From 5600e845da7e0af9a0c6b1956c3281fceb4b08a4 Mon Sep 17 00:00:00 2001 From: huangsimin Date: Thu, 9 Apr 2020 18:41:30 +0800 Subject: [PATCH] TaskID --- context.go | 10 +++------- hunter.go | 33 ++++++++++++++++++--------------- hunter_test.go | 24 +++++++++++++++++++++--- task.go | 18 ++++++++++++++++++ 4 files changed, 60 insertions(+), 25 deletions(-) diff --git a/context.go b/context.go index 62eb3f6..4a4f1d9 100644 --- a/context.go +++ b/context.go @@ -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() diff --git a/hunter.go b/hunter.go index 2e99fa6..017f3b5 100644 --- a/hunter.go +++ b/hunter.go @@ -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++ } } diff --git a/hunter_test.go b/hunter_test.go index 218dba2..2affb6c 100644 --- a/hunter_test.go +++ b/hunter_test.go @@ -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{}) diff --git a/task.go b/task.go index 601293d..3dd54a3 100644 --- a/task.go +++ b/task.go @@ -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