imitater/person.go

194 lines
3.7 KiB
Go
Raw Permalink Normal View History

2018-12-07 10:21:23 +00:00
package imitater
import (
2018-12-07 10:21:23 +00:00
"fmt"
"log"
"reflect"
"time"
"474420502.top/eson/crontabex"
2018-12-07 11:09:38 +00:00
"474420502.top/eson/structure/circular_linked"
"474420502.top/eson/curl2info"
)
// ITask 继承这个接口的类
type ITask interface {
2018-12-21 15:41:49 +00:00
Execute(data map[string]interface{}) ITask
2018-12-18 06:38:59 +00:00
Init()
SetCrontab(cron string)
GetCrontab() *crontab.Crontab
2018-12-07 10:21:23 +00:00
SetName(name string)
GetName() string
SetCurl(Curl *curl2info.CURL)
2018-12-07 10:21:23 +00:00
GetCurl() *curl2info.CURL
2018-12-07 11:09:38 +00:00
GetProxies() *clinked.CircularLinked
AddProxies(proxy string)
2018-12-18 07:33:57 +00:00
SetADInfo(adinfo ADInfo)
GetADInfo() *ADInfo
TimeUp() bool
NextTime() time.Time
}
var register = make(map[string]reflect.Type)
func init() {
2018-12-07 10:21:23 +00:00
log.SetFlags(log.Llongfile)
}
// Register 注册 类型 ITask为样本的类型
2018-12-07 10:21:23 +00:00
func Register(name string, itask ITask) {
register[name] = reflect.TypeOf(itask).Elem()
}
2018-12-07 10:21:23 +00:00
func makeRegisterType(name string) ITask {
result := reflect.New(register[name]).Interface().(ITask)
result.SetName(name)
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
}
2018-12-07 11:09:38 +00:00
// Config 加载配置
func (person *Person) Config(conf string) {
person.Conf = NewConfig(conf)
person.Tasks = splitTasks(person.Conf)
}
// NewPersonWithConfig 创建一个person
func NewPersonWithConfig(conf string) *Person {
person := NewPerson()
2018-12-07 11:09:38 +00:00
person.Config(conf)
return person
}
// SplitTasks 拆开出需求的任务
func splitTasks(conf *Config) []ITask {
var tasks []ITask
2018-12-07 11:09:38 +00:00
proxies := (*clinked.CircularLinked)(conf.Proxies)
for _, scurl := range conf.Curls {
curl := curl2info.ParseRawCURL(scurl)
2018-12-07 10:21:23 +00:00
if curl.ITask == "" {
curl.ITask = conf.ITask
}
task := makeRegisterType(curl.ITask)
switch conf.Mode {
case 0:
2018-12-07 10:21:23 +00:00
initTask(conf, task, curl)
2018-12-18 07:33:57 +00:00
// 初始化代理
2018-12-09 14:25:48 +00:00
if proxies != nil {
for _, cnode := range proxies.GetLoopValues() {
proxy := cnode.GetValue().(string)
task.AddProxies(proxy)
}
2018-12-07 11:09:38 +00:00
}
2018-12-09 14:25:48 +00:00
2018-12-07 11:09:38 +00:00
tasks = append(tasks, task)
case 1:
2018-12-09 14:25:48 +00:00
if proxies != nil {
for _, cnode := range proxies.GetLoopValues() {
proxy := cnode.GetValue().(string)
ncurl := curl2info.ParseRawCURL(scurl)
2018-12-09 14:25:48 +00:00
if curl.ITask == "" {
curl.ITask = conf.ITask
}
ptask := makeRegisterType(ncurl.ITask).(ITask)
initTask(conf, ptask, ncurl)
ptask.AddProxies(proxy)
tasks = append(tasks, ptask)
}
}
}
}
return tasks
}
// Execute 人的执行所有任务
func (person *Person) Execute() {
2018-12-07 10:21:23 +00:00
taskLen := len(person.Tasks)
result := make(chan string, 1)
for _, task := range person.Tasks {
2018-12-09 14:25:48 +00:00
2018-12-07 10:21:23 +00:00
go ExecuteOnPlan(task, result)
2018-12-09 14:25:48 +00:00
2018-12-07 10:21:23 +00:00
}
for t := range result {
log.Println(t)
taskLen--
if taskLen <= 0 {
close(result)
}
}
// engine := gin.Default()
// engine.Run()
}
// initTask 生成一个新任务
func initTask(conf *Config, task ITask, curl *curl2info.CURL) {
2018-12-18 06:38:59 +00:00
2018-12-07 10:21:23 +00:00
if curl.Crontab != "" {
task.SetCrontab(curl.Crontab)
} else {
task.SetCrontab(conf.Crontab)
}
2018-12-18 07:33:57 +00:00
task.SetADInfo(conf.ADInfo)
2018-12-07 10:21:23 +00:00
task.SetCurl(curl)
2018-12-18 07:33:57 +00:00
task.Init()
2018-12-07 10:21:23 +00:00
}
// ExecuteOnPlan 按照计划执行任务并返回结果
func ExecuteOnPlan(task ITask, result chan string) {
data := make(map[string]interface{})
var rtask ITask
taskname := task.GetName()
urlname := task.GetCurl().Name
task.NextTime() // 必须调用NextTime 才能触发TimeUp
2018-12-07 10:21:23 +00:00
for {
2018-12-09 13:39:44 +00:00
interval := time.Second * 2
2018-12-07 10:21:23 +00:00
if task.TimeUp() {
rtask = task.Execute(data) // 事件 在这里变化
2018-12-09 13:39:44 +00:00
ntime := task.NextTime()
interval = ntime.Sub(time.Now())
2018-12-07 10:21:23 +00:00
if rtask == nil {
// log.Println("rtask is nil")
result <- fmt.Sprintf("rtask is nil, the first task = %s, name = %s", taskname, urlname)
return
}
2018-12-09 13:39:44 +00:00
task = rtask
2018-12-07 10:21:23 +00:00
}
time.Sleep(interval)
}
}