2020-04-07 10:55:41 +00:00
|
|
|
package hunter
|
|
|
|
|
2020-04-08 07:18:29 +00:00
|
|
|
import "github.com/474420502/requests"
|
|
|
|
|
2020-04-10 10:54:19 +00:00
|
|
|
type IHunt interface {
|
2020-04-10 17:44:31 +00:00
|
|
|
Hunt() (requests.IResponse, error)
|
2020-04-10 10:54:19 +00:00
|
|
|
}
|
|
|
|
|
2020-04-07 10:55:41 +00:00
|
|
|
// TaskContext 上下文
|
|
|
|
type TaskContext struct {
|
2020-04-09 10:41:30 +00:00
|
|
|
hunter *Hunter
|
2020-04-08 07:18:29 +00:00
|
|
|
workflow *requests.Workflow
|
2020-04-08 09:40:02 +00:00
|
|
|
|
|
|
|
parent ITaskNode
|
|
|
|
current ITaskNode
|
2020-04-17 10:04:11 +00:00
|
|
|
autoid int
|
2020-04-07 10:55:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewContext 任务上下文
|
|
|
|
func NewContext() *TaskContext {
|
|
|
|
return &TaskContext{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddTask 添加到当前子任务队列
|
|
|
|
func (cxt *TaskContext) AddTask(itask ITask) {
|
2020-04-08 09:40:02 +00:00
|
|
|
if children := cxt.current.Children(); children == nil {
|
|
|
|
cxt.current.SetChildren(cxt.hunter.createQueue())
|
2020-04-07 10:55:41 +00:00
|
|
|
}
|
2020-04-08 07:18:29 +00:00
|
|
|
bt := &BaseTask{task: itask}
|
2020-04-08 09:40:02 +00:00
|
|
|
cxt.current.Children().Push(bt)
|
2020-04-07 10:55:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// AddParentTask 添加到当前任务队列
|
|
|
|
func (cxt *TaskContext) AddParentTask(itask ITask) {
|
2020-04-08 07:18:29 +00:00
|
|
|
|
|
|
|
bt := &BaseTask{task: itask}
|
2020-04-08 09:40:02 +00:00
|
|
|
cxt.current.Parent().Children().Push(bt)
|
2020-04-07 17:18:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetShare 获取share的数据, 存储用的
|
|
|
|
func (cxt *TaskContext) GetShare(key string) interface{} {
|
2020-04-08 09:40:02 +00:00
|
|
|
if v, ok := cxt.hunter.share[key]; ok {
|
2020-04-07 17:18:33 +00:00
|
|
|
return v
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetShare 设置share的数据, 存储用的
|
|
|
|
func (cxt *TaskContext) SetShare(key string, value interface{}) {
|
2020-04-08 09:40:02 +00:00
|
|
|
cxt.hunter.share[key] = value
|
2020-04-07 10:55:41 +00:00
|
|
|
}
|
2020-04-08 07:18:29 +00:00
|
|
|
|
|
|
|
// Session Get return session *requests.Session
|
|
|
|
func (cxt *TaskContext) Session() *requests.Session {
|
2020-04-08 09:40:02 +00:00
|
|
|
if cxt.hunter.Session() == nil {
|
|
|
|
cxt.hunter.SetSession(requests.NewSession())
|
2020-04-08 07:18:29 +00:00
|
|
|
}
|
2020-04-08 09:40:02 +00:00
|
|
|
return cxt.hunter.Session()
|
2020-04-08 07:18:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Workflow Get return Workflow *requests.Workflow. not exists, return nil
|
|
|
|
func (cxt *TaskContext) Workflow() *requests.Workflow {
|
|
|
|
return cxt.workflow
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetWorkflow Set Workflow *requests.Workflow
|
|
|
|
func (cxt *TaskContext) SetWorkflow(workflow *requests.Workflow) {
|
|
|
|
cxt.workflow = workflow
|
|
|
|
}
|
|
|
|
|
2020-04-08 09:40:02 +00:00
|
|
|
// TaskID Get Task ID
|
|
|
|
func (cxt *TaskContext) TaskID() string {
|
2020-04-09 10:41:30 +00:00
|
|
|
return cxt.current.TaskID()
|
2020-04-08 09:40:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Path curren Task tree path.
|
|
|
|
func (cxt *TaskContext) Path() string {
|
2020-04-09 10:41:30 +00:00
|
|
|
return cxt.current.Path()
|
2020-04-08 09:40:02 +00:00
|
|
|
}
|
|
|
|
|
2020-04-10 08:39:48 +00:00
|
|
|
// GetHunter 获取share的数据, 存储用的
|
|
|
|
func (cxt *TaskContext) GetHunter() *Hunter {
|
|
|
|
return cxt.hunter
|
|
|
|
}
|
|
|
|
|
2020-04-08 07:18:29 +00:00
|
|
|
// Hunt Hunt() = cxt.Workflow().Execute()
|
2020-04-10 17:44:31 +00:00
|
|
|
func (cxt *TaskContext) Hunt() (requests.IResponse, error) {
|
|
|
|
if ihunt, ok := cxt.current.Task().(IHunt); ok {
|
2020-04-10 10:54:19 +00:00
|
|
|
return ihunt.Hunt()
|
|
|
|
}
|
2020-04-08 07:18:29 +00:00
|
|
|
return cxt.workflow.Execute()
|
|
|
|
}
|