go 的设计上的问题, 需要改task和person结构

This commit is contained in:
huangsimin 2018-11-29 18:41:45 +08:00
parent 4877177010
commit a0c18f4736
2 changed files with 19 additions and 6 deletions

17
task.go
View File

@ -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()
}
}

View File

@ -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")