imitater/task.go
2018-12-07 04:47:22 +08:00

194 lines
3.6 KiB
Go

package imitate
import (
"log"
"reflect"
"time"
crontab "474420502.top/eson/crontabex"
"474420502.top/eson/curl2info"
"474420502.top/eson/requests"
)
var register = make(map[string]reflect.Type)
func init() {
}
// Register 注册 类型 ITask为样本的类型
func Register(name string, itype interface{}) {
register[name] = reflect.TypeOf(itype)
}
func makeRegisterType(name string) interface{} {
result := reflect.New(register[name]).Interface()
return result
}
// Person 以人为单位
type Person struct {
Tasks []ITask
Conf *Config
}
// NewPerson 创建一个人实例
func NewPerson() *Person {
person := &Person{}
// person.Conf = NewConfig(conf)
// person.Tasks = SplitTasks(person.Conf)
return person
}
// LoadConfig 加载配置
func (person *Person) LoadConfig(conf string) {
person.Conf = NewConfig(conf)
person.Tasks = splitTasks(person.Conf)
}
// NewPersonWithConfig 创建一个person
func NewPersonWithConfig(conf string) *Person {
person := NewPerson()
person.LoadConfig(conf)
return person
}
// SplitTasks 拆开出需求的任务
func splitTasks(conf *Config) []ITask {
var tasks []ITask
for _, scurl := range conf.Curls {
curl, err := curl2info.ParseRawCURL(scurl)
if err != nil {
panic(err)
}
task := makeRegisterType(curl.ITask).(ITask)
switch conf.Mode {
case 0:
if curl.Crontab != "" {
task.SetCrontab(curl.Crontab)
} else {
log.Println(conf.Crontab)
task.SetCrontab(conf.Crontab)
}
task.SetCurl(curl)
task.AppendProxies(conf.Proxies...)
tasks = append(tasks, task)
case 1:
for _, proxy := range conf.Proxies {
ncurl, err := curl2info.ParseRawCURL(scurl)
if err != nil {
panic(err)
}
ptask := makeRegisterType(ncurl.ITask).(ITask)
if ncurl.Crontab != "" {
ptask.SetCrontab(ncurl.Crontab)
} else {
ptask.SetCrontab(conf.Crontab)
}
InitTask(ptask, ncurl)
ptask.AppendProxies(proxy)
tasks = append(tasks, task)
}
}
}
return tasks
}
// Execute 人的执行所有任务
func (person *Person) Execute() {
for _, task := range person.Tasks {
ExecuteOnPlan(task)
}
}
// ITask 继承这个接口的类
type ITask interface {
Execute()
SetCrontab(cron string)
SetCurl(Curl *curl2info.CURL)
GetCurl() *curl2info.CURL
GetProxies() []string
AppendProxies(proxies ...string)
TimeUp() bool
NextTime() time.Time
}
// Task 任务
type Task struct {
ITask
crontab *crontab.Crontab
curl *curl2info.CURL
workflow *requests.Workflow
proxies YamlProxies
}
func (task *Task) SetCurl(curl *curl2info.CURL) {
task.curl = curl
}
func (task *Task) GetCurl() *curl2info.CURL {
return task.curl
}
func (task *Task) AppendProxies(proxies ...string) {
for _, proxy := range proxies {
task.proxies = append(task.proxies, proxy)
}
}
func (task *Task) GetProxies() []string {
return task.proxies
}
func (task *Task) SetCrontab(cron string) {
task.crontab = crontab.NewCrontab(cron)
}
func (task *Task) TimeUp() bool {
return task.crontab.TimeUp()
}
func (task *Task) NextTime() time.Time {
return task.crontab.NextTime()
}
// InitTask 生成一个新任务
func InitTask(task ITask, Curl *curl2info.CURL) {
task.SetCurl(Curl)
}
// ExecuteOnPlan 按照计划执行任务并返回结果
func ExecuteOnPlan(task ITask) {
log.Println(task)
for i := 0; i < 2; i++ {
if task.TimeUp() {
task.Execute() // 事件 在这里变化
}
nextTime := task.NextTime()
log.Println(nextTime)
time.Sleep(time.Second * 2)
}
}
// Request 根据curl信息执行, TODO: 通用方法设置, 多太实现
func (task *Task) Request() (*requests.Response, error) {
return task.curl.CreateWorkflow(nil).Execute()
}