imitater/task.go

116 lines
2.3 KiB
Go
Raw Normal View History

2018-11-27 09:46:29 +00:00
package imitate
import (
2018-11-27 10:32:55 +00:00
"log"
2018-11-27 09:46:29 +00:00
"474420502.top/eson/curl2info"
"474420502.top/eson/requests"
)
// Person 以人为单位
type Person struct {
Tasks []*Task
2018-11-27 09:46:29 +00:00
Conf *Config
}
// NewPerson 创建一个人实例
func NewPerson(conf string) *Person {
person := &Person{}
person.Conf = NewConfig(conf)
for _, scurl := range person.Conf.Curls {
curl, err := curl2info.ParseRawCURL(scurl)
if err != nil {
panic(err)
}
at := *&person.Conf.ExecuteAt
interval := *&person.Conf.ExecuteInterval
task := NewTask(curl, &at, &interval)
switch person.Conf.Mode {
case 0:
task.Proxies.Append(person.Conf.Proxies)
person.Tasks = append(person.Tasks, task)
2018-11-27 09:46:29 +00:00
case 1:
for _, proxy := range person.Conf.Proxies {
ncurl, err := curl2info.ParseRawCURL(scurl)
if err != nil {
panic(err)
}
ptask := NewTask(ncurl)
for _, exec := range task.Plan.GetLoopValues() {
switch v := exec.GetValue().(type) {
case *ExecuteAt:
clone := &*v
ptask.Plan.Append(clone)
case *ExecuteInterval:
clone := &*v
ptask.Plan.Append(clone)
}
}
ptask.Proxies.Append(proxy)
person.Tasks = append(person.Tasks, task)
2018-11-27 09:46:29 +00:00
}
}
}
return person
}
2018-11-27 10:32:55 +00:00
// Execute 人的执行所有任务
func (person *Person) Execute() {
for _, task := range person.Tasks {
task.ExecuteOnPlan(task)
}
}
2018-11-27 10:32:55 +00:00
type ITask interface {
Execute()
2018-11-27 10:32:55 +00:00
}
2018-11-27 09:46:29 +00:00
// Task 任务
type Task struct {
Curl *curl2info.CURL
Workflow *requests.Workflow
Plan *CircularLinked
Proxies *CircularLinked
}
// NewTask 生成一个新任务
func NewTask(Curl *curl2info.CURL, Plans ...IExecute) *Task {
task := &Task{}
// task.Conf = NewConfig(conf)
task.Curl = Curl
task.Plan = NewCircularLinked()
task.Proxies = NewCircularLinked()
if len(Plans) != 0 {
for _, plan := range Plans {
task.Plan.Append(plan)
}
}
return task
}
// ExecuteOnPlan 按照计划执行任务并返回结果
func (task *Task) ExecuteOnPlan(itask ITask) {
2018-11-27 09:46:29 +00:00
for _, exec := range task.Plan.GetLoopValues() {
iexec := exec.GetValue().(IExecute)
if iexec.TimeTo() >= 0 {
itask.Execute() // 事件 在这里变化
2018-11-27 18:39:59 +00:00
iexec.CalculateTrigger()
2018-11-27 09:46:29 +00:00
}
}
}
2018-11-27 10:32:55 +00:00
// Execute 根据curl信息执行, TODO: 通用方法设置, 多太实现
func (task *Task) Execute() {
resp, err := task.Curl.CreateWorkflow(nil).Execute()
log.Println(resp, err)
2018-11-27 09:46:29 +00:00
}