diff --git a/context.go b/context.go index 1cabb04..fd5d749 100644 --- a/context.go +++ b/context.go @@ -13,6 +13,7 @@ type TaskContext struct { parent ITaskNode current ITaskNode + autoid int } // NewContext 任务上下文 diff --git a/go.mod b/go.mod index 172b3f0..e887e08 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/474420502/hunter go 1.14 require ( - github.com/474420502/focus v0.8.1 + github.com/474420502/focus v0.9.0 github.com/474420502/gcurl v0.0.4 github.com/474420502/requests v1.5.0 github.com/Pallinder/go-randomdata v1.1.0 diff --git a/go.sum b/go.sum index 79e5e2e..69f51b4 100644 --- a/go.sum +++ b/go.sum @@ -4,6 +4,7 @@ cloud.google.com/go v0.38.0/go.mod h1:990N+gfupTy94rShfmMCWGDn0LpTmnzTp2qbd1dvSR cloud.google.com/go v0.41.0/go.mod h1:OauMR7DV8fzvZIl2qg6rkaIhD/vmgk4iwEw/h6ercmg= github.com/474420502/focus v0.8.1 h1:PZwCgzcnxwx7ZZCWc/XKLVaZPH9e4YX9cP4ckyT2HDA= github.com/474420502/focus v0.8.1/go.mod h1:jrDXvK1CnUJ3PCR3ZJVYinbS2Yz5kM8OoAbCLe6AF7Y= +github.com/474420502/focus v0.9.0/go.mod h1:jrDXvK1CnUJ3PCR3ZJVYinbS2Yz5kM8OoAbCLe6AF7Y= github.com/474420502/gcurl v0.0.3 h1:gbvJ9JiiiaCkLispDZ9tlJPFo8K4e7t4JTFze3UlHHc= github.com/474420502/gcurl v0.0.3/go.mod h1:qtCzAZZbVRIsBt0lNUh2I0qDniU9T3E21aSsVUYo7Hc= github.com/474420502/gcurl v0.0.4 h1:eR1BNXvQ4T245dotWpjDzAMWch+FTTfScqzsdq93JK0= diff --git a/hunter.go b/hunter.go index 83b7e23..78a1da1 100644 --- a/hunter.go +++ b/hunter.go @@ -4,6 +4,8 @@ import ( "log" "strconv" + astack "github.com/474420502/focus/stack/arraystack" + pqueue "github.com/474420502/focus/priority_queue" "github.com/474420502/requests" ) @@ -26,6 +28,8 @@ type Hunter struct { tasks []ITask createQueue func() *pqueue.PriorityQueue + + executes *astack.Stack // *TaskContext } // NewHunter 默认最大优先, tasks为预先需要addtask @@ -50,6 +54,7 @@ func NewHunter(tasks ...ITask) *Hunter { func NewPriorityHunter(queueCreator func() *pqueue.PriorityQueue) *Hunter { hunter := &Hunter{} hunter.createQueue = queueCreator + hunter.executes = astack.New() hunter.share = make(map[string]interface{}) return hunter @@ -108,42 +113,54 @@ func (hunter *Hunter) execute(task ITask) { cxt.parent.Children().Push(btask) cxt.hunter = hunter - hunter.recursionTasks(cxt) + + // hunter.recursionTasks(cxt) + + hunter.executes.Push(cxt) + hunter.recursionTasks() } -func (hunter *Hunter) recursionTasks(cxt *TaskContext) { +func (hunter *Hunter) recursionTasks() { - autoid := 0 + // 这层再加一个 Children 提取 - for children := cxt.parent.Children(); children != nil && children.Size() > 0; { - if itask, ok := children.Pop(); ok { - sautoid := strconv.Itoa(autoid) - ncxt := NewContext() + for icxt, ok := hunter.executes.Pop(); ok; icxt, ok = hunter.executes.Pop() { - tasknode := itask.(ITaskNode) - tasknode.SetID(sautoid) + cxt := icxt.(*TaskContext) + for children := cxt.parent.Children(); children != nil && children.Size() > 0; { + if itask, ok := children.Pop(); ok { + sautoid := strconv.Itoa(cxt.autoid) - cxt.current = tasknode - cxt.current.SetPath(cxt.parent.Path()) // + tasknode := itask.(ITaskNode) + tasknode.SetID(sautoid) - task := tasknode.Task() + cxt.current = tasknode + cxt.current.SetPath(cxt.parent.Path()) // + + task := tasknode.Task() + + if before, ok := task.(IBefore); ok { + before.Before(cxt) + } + + tasknode.Task().Execute(cxt) + + if after, ok := task.(IAfter); ok { + after.After(cxt) + } + + cxt.autoid++ + + ncxt := NewContext() + ncxt.autoid = 0 + ncxt.parent = cxt.current + ncxt.parent.SetPath(ncxt.parent.Path() + "/" + ncxt.parent.TaskID()) //补正ncxt的路径 + ncxt.hunter = cxt.hunter + + // hunter.recursionTasks(ncxt) + hunter.executes.Push(ncxt) - if before, ok := task.(IBefore); ok { - before.Before(cxt) } - - tasknode.Task().Execute(cxt) - - if after, ok := task.(IAfter); ok { - after.After(cxt) - } - - ncxt.parent = cxt.current - ncxt.parent.SetPath(ncxt.parent.Path() + "/" + ncxt.parent.TaskID()) //补正ncxt的路径 - ncxt.hunter = cxt.hunter - hunter.recursionTasks(ncxt) - - autoid++ } }