From a0c18f47364de2701ad0aeff1e38f7cc2f1ae4a5 Mon Sep 17 00:00:00 2001 From: huangsimin Date: Thu, 29 Nov 2018 18:41:45 +0800 Subject: [PATCH] =?UTF-8?q?go=20=E7=9A=84=E8=AE=BE=E8=AE=A1=E4=B8=8A?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98,=20=E9=9C=80=E8=A6=81=E6=94=B9task?= =?UTF-8?q?=E5=92=8Cperson=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- task.go | 17 +++++++++++------ task_test.go | 8 ++++++++ 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/task.go b/task.go index fbf180a..f4b240e 100644 --- a/task.go +++ b/task.go @@ -9,14 +9,13 @@ import ( // Person 以人为单位 type Person struct { - Tasks *CircularLinked + Tasks []*Task Conf *Config } // NewPerson 创建一个人实例 func NewPerson(conf string) *Person { person := &Person{} - person.Tasks = NewCircularLinked() person.Conf = NewConfig(conf) for _, scurl := range person.Conf.Curls { @@ -32,7 +31,7 @@ func NewPerson(conf string) *Person { switch person.Conf.Mode { case 0: task.Proxies.Append(person.Conf.Proxies) - person.Tasks.Append(task) + person.Tasks = append(person.Tasks, task) case 1: for _, proxy := range person.Conf.Proxies { ncurl, err := curl2info.ParseRawCURL(scurl) @@ -53,7 +52,7 @@ func NewPerson(conf string) *Person { } ptask.Proxies.Append(proxy) - person.Tasks.Append(ptask) + person.Tasks = append(person.Tasks, task) } } } @@ -63,7 +62,13 @@ func NewPerson(conf string) *Person { // Execute 人的执行所有任务 func (person *Person) Execute() { + for _, task := range person.Tasks { + task.ExecuteOnPlan(task) + } +} +type ITask interface { + Execute() } // Task 任务 @@ -93,11 +98,11 @@ func NewTask(Curl *curl2info.CURL, Plans ...IExecute) *Task { } // ExecuteOnPlan 按照计划执行任务并返回结果 -func (task *Task) ExecuteOnPlan() { +func (task *Task) ExecuteOnPlan(itask ITask) { for _, exec := range task.Plan.GetLoopValues() { iexec := exec.GetValue().(IExecute) if iexec.TimeTo() >= 0 { - task.Execute() + itask.Execute() // 事件 在这里变化 iexec.CalculateTrigger() } } diff --git a/task_test.go b/task_test.go index 0888206..28672bb 100644 --- a/task_test.go +++ b/task_test.go @@ -24,6 +24,14 @@ func TestExecute(t *testing.T) { } } +type Toutiao struct { + Task +} + +func (tt *Toutiao) Execute() { + +} + func TestExecutePlan(t *testing.T) { person := NewPerson("test.yaml")