TODO: ITask Method

This commit is contained in:
huangsimin 2018-12-06 18:34:10 +08:00
parent 88303b9e51
commit 1e7a3cdab3
2 changed files with 48 additions and 27 deletions

View File

@ -38,7 +38,7 @@ func (curls *YamlCurls) UnmarshalYAML(unmarshal func(interface{}) error) error {
return nil
}
// MarshalYAML 序列化函数
// MarshalYAML YamlCurls序列化函数
func (curls *YamlCurls) MarshalYAML() (interface{}, error) {
content := "["
for _, curl := range []string(*curls) {
@ -49,9 +49,10 @@ func (curls *YamlCurls) MarshalYAML() (interface{}, error) {
return content, nil
}
// YamlProxies 为了自定义序列化函数
type YamlProxies []string
// UnmarshalYAML YamlCurls反序列化函数
// UnmarshalYAML YamlProxies反序列化函数
func (proxies *YamlProxies) UnmarshalYAML(unmarshal func(interface{}) error) error {
var buf interface{}
@ -75,7 +76,7 @@ func (proxies *YamlProxies) UnmarshalYAML(unmarshal func(interface{}) error) err
return nil
}
// MarshalYAML 序列化函数
// MarshalYAML YamlProxies 序列化函数
func (proxies *YamlProxies) MarshalYAML() (interface{}, error) {
content := "["
for _, curl := range []string(*proxies) {

68
task.go
View File

@ -2,6 +2,9 @@ package imitate
import (
"reflect"
"time"
"474420502.top/eson/crontabex"
"474420502.top/eson/curl2info"
"474420502.top/eson/requests"
@ -58,6 +61,12 @@ func splitTasks(conf *Config) []ITask {
}
task := makeRegisterType(curl.ITask).Elem().(ITask)
if curl.Crontab != "" {
task.SetCrontab(curl.Crontab)
} else {
task.SetCrontab(conf.Crontab)
}
switch conf.Mode {
case 0:
copy(task.GetProxies(), conf.Proxies)
@ -70,9 +79,11 @@ func splitTasks(conf *Config) []ITask {
}
ptask := makeRegisterType(ncurl.ITask).Elem().(ITask)
// for _, exec := range task {
// }
if curl.Crontab != "" {
ptask.SetCrontab(curl.Crontab)
} else {
ptask.SetCrontab(conf.Crontab)
}
ptask.AppendProxies(proxy)
tasks = append(tasks, task)
@ -93,32 +104,51 @@ func (person *Person) Execute() {
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 []string
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) SetCurl(curl *curl2info.CURL) {
// task.curl = curl
// }
// func (task *Task) GetCurl() *curl2info.CURL {
// return task.curl
// }
// //
// func (task *Task) AppendPlans(plans ...IExecute) {
// if len(plans) != 0 {
// for _, plan := range plans {
@ -131,18 +161,8 @@ type Task struct {
// return task.plans
// }
// func (task *Task) AppendProxies(proxies ...string) {
// for _, proxy := range proxies {
// task.proxies = append(task.proxies, proxy)
// }
// }
// func (task *Task) GetProxies() []string {
// return task.proxies
// }
// // InitTask 生成一个新任务
// func InitTask(task ITask, Curl *curl2info.CURL, Plans ...IExecute) {
// func InitTask(task ITask, Curl *curl2info.CURL) {
// // task.Conf = NewConfig(conf)
// task.SetCurl(Curl)