From 1e7a3cdab35acd1fd719cf17d17f095f2e2e3471 Mon Sep 17 00:00:00 2001 From: huangsimin Date: Thu, 6 Dec 2018 18:34:10 +0800 Subject: [PATCH] TODO: ITask Method --- config.go | 7 +++--- task.go | 68 +++++++++++++++++++++++++++++++++++-------------------- 2 files changed, 48 insertions(+), 27 deletions(-) diff --git a/config.go b/config.go index 4dc237d..a2c9284 100644 --- a/config.go +++ b/config.go @@ -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) { diff --git a/task.go b/task.go index 1175dcb..bfbcf18 100644 --- a/task.go +++ b/task.go @@ -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)