From 9853a68f664299c4aaf9961359bde3b46fcaf36d Mon Sep 17 00:00:00 2001 From: eson <474420502@qq.com> Date: Mon, 3 Dec 2018 03:17:24 +0800 Subject: [PATCH] crontab upgrade --- go.mod | 8 ++++++++ task.go | 34 ++++++++++++++++++++++++---------- 2 files changed, 32 insertions(+), 10 deletions(-) create mode 100644 go.mod diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..b4d9e89 --- /dev/null +++ b/go.mod @@ -0,0 +1,8 @@ +module 474420502.top/eson/imitater + +require ( + gopkg.in/yaml.v2 v2.2.2 + github.com/davecgh/go-spew/spew v1.1.1 + 474420502.top/eson/curl2info v1.0.0 + 474420502.top/eson/requests v1.0.0 +) diff --git a/task.go b/task.go index 256f441..b7e3a3b 100644 --- a/task.go +++ b/task.go @@ -63,7 +63,7 @@ func splitTasks(conf *Config) []ITask { task := makeRegisterType(curl.ITask).Elem().(ITask) switch conf.Mode { case 0: - copy(task.Proxies, conf.Proxies) + copy(task.GetProxies(), conf.Proxies) tasks = append(tasks, task) case 1: for _, proxy := range conf.Proxies { @@ -72,19 +72,19 @@ func splitTasks(conf *Config) []ITask { panic(err) } - ptask := NewTask(ncurl) - for _, exec := range task.getPlans() { + ptask := makeRegisterType(ncurl.ITask).Elem().(ITask) + for _, exec := range task.GetPlans() { switch v := exec.(type) { case *ExecuteAt: clone := &*v - ptask.Plan = append(ptask.Plan, clone) + ptask.AppendPlans(clone) case *ExecuteInterval: clone := &*v - ptask.Plan = append(ptask.Plan, clone) + ptask.AppendPlans(clone) } } - ptask.Proxies = append(ptask.Proxies, proxy) + ptask.AppendProxies(proxy) tasks = append(tasks, task) } } @@ -110,7 +110,7 @@ type ITask interface { GetPlans() []IExecute GetProxies() []string - SetProxies(proxies string) + AppendProxies(proxies ...string) } // Task 任务 @@ -141,17 +141,31 @@ func (task *Task) AppendPlans(plans ...IExecute) { } } +func (task *Task) GetPlans() []IExecute { + 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) { // task.Conf = NewConfig(conf) - task.setCurl(Curl) - task.appendPlans(Plans...) + task.SetCurl(Curl) + task.AppendPlans(Plans...) } // ExecuteOnPlan 按照计划执行任务并返回结果 func ExecuteOnPlan(task ITask) { - for _, exec := range task.getPlans() { + for _, exec := range task.GetPlans() { if exec.TimeTo() >= 0 { task.Execute() // 事件 在这里变化 exec.CalculateTrigger()