change code to interface mode
This commit is contained in:
parent
a0c18f4736
commit
2f7b2c151a
143
task.go
143
task.go
|
@ -1,115 +1,166 @@
|
|||
package imitate
|
||||
|
||||
import (
|
||||
"log"
|
||||
"reflect"
|
||||
|
||||
"474420502.top/eson/curl2info"
|
||||
"474420502.top/eson/requests"
|
||||
)
|
||||
|
||||
var register map[string]reflect.Type
|
||||
|
||||
// Register 注册 类型 ITask为样本的类型
|
||||
func Register(name string, itype interface{}) {
|
||||
register[name] = reflect.TypeOf(itype)
|
||||
}
|
||||
|
||||
func makeRegisterType(name string) reflect.Type {
|
||||
return reflect.New(register[name]).Type().Elem()
|
||||
}
|
||||
|
||||
// Person 以人为单位
|
||||
type Person struct {
|
||||
Tasks []*Task
|
||||
Tasks []ITask
|
||||
Conf *Config
|
||||
}
|
||||
|
||||
// NewPerson 创建一个人实例
|
||||
func NewPerson(conf string) *Person {
|
||||
func NewPerson() *Person {
|
||||
person := &Person{}
|
||||
person.Conf = NewConfig(conf)
|
||||
|
||||
for _, scurl := range person.Conf.Curls {
|
||||
// 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)
|
||||
}
|
||||
|
||||
at := *&person.Conf.ExecuteAt
|
||||
interval := *&person.Conf.ExecuteInterval
|
||||
at := *&conf.ExecuteAt
|
||||
interval := *&conf.ExecuteInterval
|
||||
|
||||
task := NewTask(curl, &at, &interval)
|
||||
switch person.Conf.Mode {
|
||||
task := makeRegisterType(curl.ITask).Elem().(ITask)
|
||||
switch conf.Mode {
|
||||
case 0:
|
||||
task.Proxies.Append(person.Conf.Proxies)
|
||||
person.Tasks = append(person.Tasks, task)
|
||||
copy(task.Proxies, conf.Proxies)
|
||||
tasks = append(tasks, task)
|
||||
case 1:
|
||||
for _, proxy := range person.Conf.Proxies {
|
||||
for _, proxy := range 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) {
|
||||
for _, exec := range task.getPlans() {
|
||||
switch v := exec.(type) {
|
||||
case *ExecuteAt:
|
||||
clone := &*v
|
||||
ptask.Plan.Append(clone)
|
||||
ptask.Plan = append(ptask.Plan, clone)
|
||||
case *ExecuteInterval:
|
||||
clone := &*v
|
||||
ptask.Plan.Append(clone)
|
||||
ptask.Plan = append(ptask.Plan, clone)
|
||||
}
|
||||
}
|
||||
|
||||
ptask.Proxies.Append(proxy)
|
||||
person.Tasks = append(person.Tasks, task)
|
||||
ptask.Proxies = append(ptask.Proxies, proxy)
|
||||
tasks = append(tasks, task)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return person
|
||||
return tasks
|
||||
}
|
||||
|
||||
// Execute 人的执行所有任务
|
||||
func (person *Person) Execute() {
|
||||
for _, task := range person.Tasks {
|
||||
task.ExecuteOnPlan(task)
|
||||
ExecuteOnPlan(task)
|
||||
}
|
||||
}
|
||||
|
||||
// ITask 继承这个接口的类
|
||||
type ITask interface {
|
||||
Execute()
|
||||
|
||||
SetCurl(Curl *curl2info.CURL)
|
||||
GetCurl() *curl2info.CURL
|
||||
|
||||
AppendPlans(Plans ...IExecute)
|
||||
GetPlans() []IExecute
|
||||
|
||||
GetProxies() []string
|
||||
SetProxies(proxies string)
|
||||
}
|
||||
|
||||
// Task 任务
|
||||
type Task struct {
|
||||
Curl *curl2info.CURL
|
||||
Workflow *requests.Workflow
|
||||
Plan *CircularLinked
|
||||
Proxies *CircularLinked
|
||||
ITask
|
||||
|
||||
curl *curl2info.CURL
|
||||
workflow *requests.Workflow
|
||||
plans []IExecute
|
||||
proxies []string
|
||||
}
|
||||
|
||||
// NewTask 生成一个新任务
|
||||
func NewTask(Curl *curl2info.CURL, Plans ...IExecute) *Task {
|
||||
//
|
||||
func (task *Task) SetCurl(curl *curl2info.CURL) {
|
||||
task.curl = curl
|
||||
}
|
||||
|
||||
task := &Task{}
|
||||
// task.Conf = NewConfig(conf)
|
||||
task.Curl = Curl
|
||||
task.Plan = NewCircularLinked()
|
||||
task.Proxies = NewCircularLinked()
|
||||
func (task *Task) GetCurl() *curl2info.CURL {
|
||||
return task.curl
|
||||
}
|
||||
|
||||
if len(Plans) != 0 {
|
||||
for _, plan := range Plans {
|
||||
task.Plan.Append(plan)
|
||||
//
|
||||
func (task *Task) AppendPlans(plans ...IExecute) {
|
||||
if len(plans) != 0 {
|
||||
for _, plan := range plans {
|
||||
task.plans = append(task.plans, plan)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return task
|
||||
// InitTask 生成一个新任务
|
||||
func InitTask(task ITask, Curl *curl2info.CURL, Plans ...IExecute) {
|
||||
|
||||
// task.Conf = NewConfig(conf)
|
||||
task.setCurl(Curl)
|
||||
task.appendPlans(Plans...)
|
||||
}
|
||||
|
||||
// ExecuteOnPlan 按照计划执行任务并返回结果
|
||||
func (task *Task) ExecuteOnPlan(itask ITask) {
|
||||
for _, exec := range task.Plan.GetLoopValues() {
|
||||
iexec := exec.GetValue().(IExecute)
|
||||
if iexec.TimeTo() >= 0 {
|
||||
itask.Execute() // 事件 在这里变化
|
||||
iexec.CalculateTrigger()
|
||||
func ExecuteOnPlan(task ITask) {
|
||||
for _, exec := range task.getPlans() {
|
||||
if exec.TimeTo() >= 0 {
|
||||
task.Execute() // 事件 在这里变化
|
||||
exec.CalculateTrigger()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Execute 根据curl信息执行, TODO: 通用方法设置, 多太实现
|
||||
func (task *Task) Execute() {
|
||||
resp, err := task.Curl.CreateWorkflow(nil).Execute()
|
||||
log.Println(resp, err)
|
||||
}
|
||||
// func (task *Task) Execute() {
|
||||
// resp, err := task.Curl.CreateWorkflow(nil).Execute()
|
||||
// log.Println(resp, err)
|
||||
// }
|
||||
|
|
30
task_test.go
30
task_test.go
|
@ -42,33 +42,3 @@ func TestExecutePlan(t *testing.T) {
|
|||
}
|
||||
|
||||
}
|
||||
|
||||
type C interface {
|
||||
Execute()
|
||||
}
|
||||
|
||||
type A struct {
|
||||
C
|
||||
}
|
||||
|
||||
func (a *A) Execute() {
|
||||
log.Println("A")
|
||||
}
|
||||
|
||||
func (a *A) EE() {
|
||||
a.Execute()
|
||||
a.Execute()
|
||||
}
|
||||
|
||||
type B struct {
|
||||
A
|
||||
}
|
||||
|
||||
func (b *B) Execute() {
|
||||
log.Println("B")
|
||||
}
|
||||
|
||||
func TestCase1(t *testing.T) {
|
||||
b := B{}
|
||||
b.EE()
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user