imitater/task.go
2018-11-28 02:39:59 +08:00

111 lines
2.2 KiB
Go

package imitate
import (
"log"
"474420502.top/eson/curl2info"
"474420502.top/eson/requests"
)
// Person 以人为单位
type Person struct {
Tasks *CircularLinked
Conf *Config
}
// NewPerson 创建一个人实例
func NewPerson(conf string) *Person {
person := &Person{}
person.Tasks = NewCircularLinked()
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(task)
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(ptask)
}
}
}
return person
}
// Execute 人的执行所有任务
func (person *Person) Execute() {
}
// 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() {
for _, exec := range task.Plan.GetLoopValues() {
iexec := exec.GetValue().(IExecute)
if iexec.TimeTo() >= 0 {
task.Execute()
iexec.CalculateTrigger()
}
}
}
// Execute 根据curl信息执行, TODO: 通用方法设置, 多太实现
func (task *Task) Execute() {
resp, err := task.Curl.CreateWorkflow(nil).Execute()
log.Println(resp, err)
}