2020-04-07 10:55:41 +00:00
|
|
|
package hunter
|
|
|
|
|
|
|
|
import (
|
2020-04-13 10:04:13 +00:00
|
|
|
"log"
|
2020-04-08 09:40:02 +00:00
|
|
|
"strconv"
|
|
|
|
|
2020-04-17 10:04:11 +00:00
|
|
|
astack "github.com/474420502/focus/stack/arraystack"
|
|
|
|
|
2020-04-07 10:55:41 +00:00
|
|
|
pqueue "github.com/474420502/focus/priority_queue"
|
2020-04-08 09:40:02 +00:00
|
|
|
"github.com/474420502/requests"
|
2020-04-07 10:55:41 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// IGobalBefore 全局任务执行之前
|
|
|
|
type IGobalBefore interface {
|
2020-04-08 09:40:02 +00:00
|
|
|
GobalBefore(cxt *TaskContext)
|
2020-04-07 10:55:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// IGobalAfter 全局任务执行之后
|
|
|
|
type IGobalAfter interface {
|
2020-04-08 09:40:02 +00:00
|
|
|
GobalAfter(cxt *TaskContext)
|
2020-04-07 10:55:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Hunter 任务相关 必须有序
|
|
|
|
type Hunter struct {
|
2020-04-08 09:40:02 +00:00
|
|
|
share map[string]interface{}
|
|
|
|
|
|
|
|
session *requests.Session
|
|
|
|
|
|
|
|
tasks []ITask
|
2020-04-07 10:55:41 +00:00
|
|
|
createQueue func() *pqueue.PriorityQueue
|
2020-04-17 10:04:11 +00:00
|
|
|
|
|
|
|
executes *astack.Stack // *TaskContext
|
2020-04-07 10:55:41 +00:00
|
|
|
}
|
|
|
|
|
2020-04-10 08:50:49 +00:00
|
|
|
// NewHunter 默认最大优先, tasks为预先需要addtask
|
|
|
|
func NewHunter(tasks ...ITask) *Hunter {
|
|
|
|
hunter := NewPriorityMaxHunter()
|
|
|
|
for _, task := range tasks {
|
|
|
|
hunter.AddTask(task)
|
|
|
|
}
|
|
|
|
return hunter
|
2020-04-07 10:55:41 +00:00
|
|
|
}
|
|
|
|
|
2020-04-10 08:50:49 +00:00
|
|
|
// NewHunterFromTasks 默认最大优先
|
|
|
|
// func NewHunterFromTasks(tasks ...ITask) *Hunter {
|
|
|
|
// hunter := NewPriorityMaxHunter()
|
|
|
|
// for _, task := range tasks {
|
|
|
|
// hunter.AddTask(task)
|
|
|
|
// }
|
|
|
|
// return hunter
|
|
|
|
// }
|
|
|
|
|
2020-04-07 10:55:41 +00:00
|
|
|
// NewPriorityHunter 自定义优先处理队列
|
|
|
|
func NewPriorityHunter(queueCreator func() *pqueue.PriorityQueue) *Hunter {
|
|
|
|
hunter := &Hunter{}
|
|
|
|
hunter.createQueue = queueCreator
|
2020-04-17 10:04:11 +00:00
|
|
|
hunter.executes = astack.New()
|
2020-04-07 10:55:41 +00:00
|
|
|
|
2020-04-08 09:40:02 +00:00
|
|
|
hunter.share = make(map[string]interface{})
|
2020-04-07 10:55:41 +00:00
|
|
|
return hunter
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewPriorityMaxHunter 最大优先
|
|
|
|
func NewPriorityMaxHunter() *Hunter {
|
|
|
|
return NewPriorityHunter(CreatePriorityMaxQueue)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewPriorityMinHunter 最小优先
|
|
|
|
func NewPriorityMinHunter() *Hunter {
|
|
|
|
return NewPriorityHunter(CreatePriorityMinQueue)
|
|
|
|
}
|
|
|
|
|
2020-04-08 09:40:02 +00:00
|
|
|
// Session Get session *requests.Session
|
|
|
|
func (hunter *Hunter) Session() *requests.Session {
|
|
|
|
return hunter.session
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetSession Set session *requests.Session
|
|
|
|
func (hunter *Hunter) SetSession(session *requests.Session) {
|
|
|
|
hunter.session = session
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetShare 获取share的数据, 存储用的
|
|
|
|
func (hunter *Hunter) GetShare(key string) interface{} {
|
|
|
|
if v, ok := hunter.share[key]; ok {
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetShare 设置share的数据, 存储用的
|
|
|
|
func (hunter *Hunter) SetShare(key string, value interface{}) {
|
|
|
|
hunter.share[key] = value
|
|
|
|
}
|
|
|
|
|
2020-04-07 10:55:41 +00:00
|
|
|
// Execute 执行任务
|
|
|
|
func (hunter *Hunter) Execute() {
|
2020-04-21 10:05:11 +00:00
|
|
|
|
|
|
|
if hunter.executes.Size() != 0 {
|
|
|
|
hunter.recursionTasks()
|
|
|
|
}
|
|
|
|
|
2020-04-08 09:40:02 +00:00
|
|
|
for _, task := range hunter.tasks {
|
|
|
|
hunter.execute(task)
|
|
|
|
}
|
2020-04-21 10:05:11 +00:00
|
|
|
|
2020-04-07 10:55:41 +00:00
|
|
|
}
|
|
|
|
|
2020-04-08 09:40:02 +00:00
|
|
|
// Execute 执行任务
|
|
|
|
func (hunter *Hunter) execute(task ITask) {
|
|
|
|
cxt := NewContext()
|
|
|
|
|
|
|
|
btask := &BaseTask{}
|
2020-04-09 10:41:30 +00:00
|
|
|
btask.SetTask(task)
|
2020-04-08 09:40:02 +00:00
|
|
|
btask.SetParent(nil)
|
|
|
|
btask.SetChildren(hunter.createQueue())
|
|
|
|
|
2020-04-09 10:41:30 +00:00
|
|
|
cxt.parent = btask
|
|
|
|
cxt.parent.Children().Push(btask)
|
2020-04-08 09:40:02 +00:00
|
|
|
|
2020-04-09 10:41:30 +00:00
|
|
|
cxt.hunter = hunter
|
2020-04-17 10:04:11 +00:00
|
|
|
|
|
|
|
// hunter.recursionTasks(cxt)
|
|
|
|
|
|
|
|
hunter.executes.Push(cxt)
|
|
|
|
hunter.recursionTasks()
|
2020-04-08 09:40:02 +00:00
|
|
|
}
|
|
|
|
|
2020-04-17 10:04:11 +00:00
|
|
|
func (hunter *Hunter) recursionTasks() {
|
|
|
|
// 这层再加一个 Children 提取
|
2020-04-20 02:43:04 +00:00
|
|
|
for icxt, ok := hunter.executes.Peek(); ok; icxt, ok = hunter.executes.Peek() {
|
2020-04-08 09:40:02 +00:00
|
|
|
|
2020-04-17 10:04:11 +00:00
|
|
|
cxt := icxt.(*TaskContext)
|
2020-04-20 02:43:04 +00:00
|
|
|
if children := cxt.parent.Children(); children != nil && children.Size() > 0 {
|
2020-04-17 10:04:11 +00:00
|
|
|
if itask, ok := children.Pop(); ok {
|
|
|
|
sautoid := strconv.Itoa(cxt.autoid)
|
2020-04-09 10:41:30 +00:00
|
|
|
|
2020-04-17 10:04:11 +00:00
|
|
|
tasknode := itask.(ITaskNode)
|
|
|
|
tasknode.SetID(sautoid)
|
2020-04-08 09:40:02 +00:00
|
|
|
|
2020-04-17 10:04:11 +00:00
|
|
|
cxt.current = tasknode
|
|
|
|
cxt.current.SetPath(cxt.parent.Path()) //
|
2020-04-08 09:40:02 +00:00
|
|
|
|
2020-04-17 10:04:11 +00:00
|
|
|
task := tasknode.Task()
|
2020-04-08 07:18:29 +00:00
|
|
|
|
2020-04-17 10:04:11 +00:00
|
|
|
if before, ok := task.(IBefore); ok {
|
|
|
|
before.Before(cxt)
|
|
|
|
}
|
2020-04-08 07:18:29 +00:00
|
|
|
|
2020-04-17 10:04:11 +00:00
|
|
|
tasknode.Task().Execute(cxt)
|
|
|
|
|
|
|
|
if after, ok := task.(IAfter); ok {
|
|
|
|
after.After(cxt)
|
|
|
|
}
|
|
|
|
|
|
|
|
cxt.autoid++
|
2020-04-08 09:40:02 +00:00
|
|
|
|
2020-04-17 10:04:11 +00:00
|
|
|
ncxt := NewContext()
|
|
|
|
ncxt.autoid = 0
|
|
|
|
ncxt.parent = cxt.current
|
|
|
|
ncxt.parent.SetPath(ncxt.parent.Path() + "/" + ncxt.parent.TaskID()) //补正ncxt的路径
|
|
|
|
ncxt.hunter = cxt.hunter
|
2020-04-09 10:41:30 +00:00
|
|
|
|
2020-04-17 10:04:11 +00:00
|
|
|
// hunter.recursionTasks(ncxt)
|
|
|
|
hunter.executes.Push(ncxt)
|
|
|
|
}
|
2020-04-20 02:43:04 +00:00
|
|
|
} else {
|
|
|
|
hunter.executes.Pop()
|
2020-04-07 10:55:41 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-08 07:18:29 +00:00
|
|
|
|
2020-04-07 10:55:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Stop 停止任务
|
2020-04-13 10:04:13 +00:00
|
|
|
func (hunter *Hunter) close(itask ITask) {
|
|
|
|
if iclose, ok := itask.(IClose); ok {
|
|
|
|
if err := iclose.Close(); err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
}
|
2020-04-07 10:55:41 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-04-13 10:04:13 +00:00
|
|
|
// Stop 停止任务
|
|
|
|
func (hunter *Hunter) Stop() {
|
|
|
|
for _, task := range hunter.tasks {
|
|
|
|
hunter.close(task)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-07 10:55:41 +00:00
|
|
|
// AddTask 执行任务
|
|
|
|
func (hunter *Hunter) AddTask(task ITask) {
|
2020-04-08 09:40:02 +00:00
|
|
|
hunter.tasks = append(hunter.tasks, task)
|
2020-04-07 10:55:41 +00:00
|
|
|
}
|
|
|
|
|
2020-04-21 10:05:11 +00:00
|
|
|
// SavePoint 持续
|
|
|
|
func (hunter *Hunter) savePoint() {
|
|
|
|
// f, err := os.OpenFile("./gob.log", os.O_CREATE|os.O_TRUNC|os.O_RDWR, os.ModePerm)
|
|
|
|
// if err != nil {
|
|
|
|
// panic(err)
|
|
|
|
// }
|
|
|
|
// enc := gob.NewEncoder(f)
|
|
|
|
// err = enc.Encode(hunter.executes.Values())
|
|
|
|
// if err != nil {
|
|
|
|
// panic(err)
|
|
|
|
// }
|
|
|
|
// for _, iv := range hunter.executes.Values() {
|
|
|
|
// tc := iv.(*TaskContext)
|
|
|
|
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
|
2020-04-07 10:55:41 +00:00
|
|
|
// Execute 执行
|
|
|
|
// func (hunter *Hunter) Execute() {
|
|
|
|
// if itask, ok := hunter.task.Children().Top(); ok {
|
|
|
|
// task := itask.(ITask)
|
|
|
|
// }
|
|
|
|
// }
|