cxt.parent.Children 变次要队列. executes优先执行再到children

This commit is contained in:
huangsimin 2020-04-17 18:04:11 +08:00
parent 44ea755873
commit d7bf902bc8
4 changed files with 47 additions and 28 deletions

View File

@ -13,6 +13,7 @@ type TaskContext struct {
parent ITaskNode parent ITaskNode
current ITaskNode current ITaskNode
autoid int
} }
// NewContext 任务上下文 // NewContext 任务上下文

2
go.mod
View File

@ -3,7 +3,7 @@ module github.com/474420502/hunter
go 1.14 go 1.14
require ( 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/gcurl v0.0.4
github.com/474420502/requests v1.5.0 github.com/474420502/requests v1.5.0
github.com/Pallinder/go-randomdata v1.1.0 github.com/Pallinder/go-randomdata v1.1.0

1
go.sum
View File

@ -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= 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 h1:PZwCgzcnxwx7ZZCWc/XKLVaZPH9e4YX9cP4ckyT2HDA=
github.com/474420502/focus v0.8.1/go.mod h1:jrDXvK1CnUJ3PCR3ZJVYinbS2Yz5kM8OoAbCLe6AF7Y= 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 h1:gbvJ9JiiiaCkLispDZ9tlJPFo8K4e7t4JTFze3UlHHc=
github.com/474420502/gcurl v0.0.3/go.mod h1:qtCzAZZbVRIsBt0lNUh2I0qDniU9T3E21aSsVUYo7Hc= github.com/474420502/gcurl v0.0.3/go.mod h1:qtCzAZZbVRIsBt0lNUh2I0qDniU9T3E21aSsVUYo7Hc=
github.com/474420502/gcurl v0.0.4 h1:eR1BNXvQ4T245dotWpjDzAMWch+FTTfScqzsdq93JK0= github.com/474420502/gcurl v0.0.4 h1:eR1BNXvQ4T245dotWpjDzAMWch+FTTfScqzsdq93JK0=

View File

@ -4,6 +4,8 @@ import (
"log" "log"
"strconv" "strconv"
astack "github.com/474420502/focus/stack/arraystack"
pqueue "github.com/474420502/focus/priority_queue" pqueue "github.com/474420502/focus/priority_queue"
"github.com/474420502/requests" "github.com/474420502/requests"
) )
@ -26,6 +28,8 @@ type Hunter struct {
tasks []ITask tasks []ITask
createQueue func() *pqueue.PriorityQueue createQueue func() *pqueue.PriorityQueue
executes *astack.Stack // *TaskContext
} }
// NewHunter 默认最大优先, tasks为预先需要addtask // NewHunter 默认最大优先, tasks为预先需要addtask
@ -50,6 +54,7 @@ func NewHunter(tasks ...ITask) *Hunter {
func NewPriorityHunter(queueCreator func() *pqueue.PriorityQueue) *Hunter { func NewPriorityHunter(queueCreator func() *pqueue.PriorityQueue) *Hunter {
hunter := &Hunter{} hunter := &Hunter{}
hunter.createQueue = queueCreator hunter.createQueue = queueCreator
hunter.executes = astack.New()
hunter.share = make(map[string]interface{}) hunter.share = make(map[string]interface{})
return hunter return hunter
@ -108,42 +113,54 @@ func (hunter *Hunter) execute(task ITask) {
cxt.parent.Children().Push(btask) cxt.parent.Children().Push(btask)
cxt.hunter = hunter 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; { for icxt, ok := hunter.executes.Pop(); ok; icxt, ok = hunter.executes.Pop() {
if itask, ok := children.Pop(); ok {
sautoid := strconv.Itoa(autoid)
ncxt := NewContext()
tasknode := itask.(ITaskNode) cxt := icxt.(*TaskContext)
tasknode.SetID(sautoid) for children := cxt.parent.Children(); children != nil && children.Size() > 0; {
if itask, ok := children.Pop(); ok {
sautoid := strconv.Itoa(cxt.autoid)
cxt.current = tasknode tasknode := itask.(ITaskNode)
cxt.current.SetPath(cxt.parent.Path()) // 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++
} }
} }