stop/start schedule

This commit is contained in:
insanity@loafle.com 2017-05-15 20:17:49 +09:00
parent be7c36013f
commit dc4fd7182e
2 changed files with 61 additions and 1 deletions

View File

@ -21,6 +21,7 @@ type Task struct {
funcParams map[string]([]interface{})
lastAt time.Time
nextAt time.Time
running bool
}
func NewTask(id string, intervel uint64) *Task {
@ -33,6 +34,7 @@ func NewTask(id string, intervel uint64) *Task {
make(map[string]([]interface{})),
time.Unix(0, 0),
time.Unix(0, 0),
true,
}
}
@ -41,6 +43,9 @@ func (t *Task) runnable() bool {
}
func (t *Task) run() {
if !t.running {
return
}
taskFunc := reflect.ValueOf(t.taskFunc[t.TaskFunc])
taskParams := t.funcParams[t.TaskFunc]
in := make([]reflect.Value, len(taskParams))
@ -223,3 +228,45 @@ func (s *Scheduler) UpdateInterval(id string, interval uint64) {
}
}
}
func (s *Scheduler) StartSchedule(id string) error {
i := 0
var ex bool
for ; i < s.size; i++ {
if s.Tasks[i].id == id {
ex = true
break
}
}
if !ex {
return errors.New("Cannot find the task. ID : " + id)
}
if s.Tasks[i].running {
return errors.New("Already started task. ID : " + id)
}
s.Tasks[i].running = true
return nil
}
func (s *Scheduler) StopSchedule(id string) error {
i := 0
var ex bool
for ; i < s.size; i++ {
if s.Tasks[i].id == id {
ex = true
break
}
}
if s.Tasks[i].running {
s.Tasks[i].running = false
}
if !ex {
return errors.New("Cannot find the task. ID : " + id)
}
if !s.Tasks[i].running {
return errors.New("Already stopped task. ID : " + id)
}
s.Tasks[i].running = false
return nil
}

View File

@ -39,4 +39,17 @@ func test(id string) {
if id == "#######################" {
log.Println(id)
}
}
}
func TestSchedulerStopStart(t *testing.T) {
s := Scheduler{}
s.Start()
s.NewSchedule("1111", 3, callback)
time.Sleep(time.Second * 10)
s.StopSchedule("1111")
time.Sleep(time.Second * 5)
s.StartSchedule("1111")
time.Sleep(time.Second * 10)
s.Stop()
}