From be7c36013fdf859431084176ad5e94a4b742464d Mon Sep 17 00:00:00 2001 From: "insanity@loafle.com" Date: Sat, 29 Apr 2017 16:22:51 +0900 Subject: [PATCH] scheduler --- scheduler.go | 20 +++++++++----------- scheduler_test.go | 3 ++- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/scheduler.go b/scheduler.go index 73e6015..8a0acbb 100644 --- a/scheduler.go +++ b/scheduler.go @@ -81,8 +81,9 @@ func (t *Task) addNextAt() { } type Scheduler struct { - Tasks [MAX_TASKS]*Task - size int + Tasks [MAX_TASKS]*Task + size int + stop chan bool } func (s *Scheduler) Len() int { @@ -129,7 +130,6 @@ func (s *Scheduler) addTask(id string, intervalSec uint64) *Task { return Task } - func (s *Scheduler) runAll() { runnableTasks, n := s.RunnableTasks() @@ -161,10 +161,8 @@ func (s *Scheduler) remove(id string) error { return nil } - - -func (s *Scheduler) Start() chan bool { - stopped := make(chan bool, 1) +func (s *Scheduler) Start() { + s.stop = make(chan bool, 1) ticker := time.NewTicker(time.Second * 1) go func() { @@ -172,13 +170,15 @@ func (s *Scheduler) Start() chan bool { select { case <-ticker.C: s.runAll() - case <-stopped: + case <-s.stop: return } } }() +} - return stopped +func (s *Scheduler) Stop() { + s.stop <- true } func (s *Scheduler) NewSchedule(id string, interval uint64, fn interface{}) error { @@ -223,5 +223,3 @@ func (s *Scheduler) UpdateInterval(id string, interval uint64) { } } } - - diff --git a/scheduler_test.go b/scheduler_test.go index 379a5be..7691533 100644 --- a/scheduler_test.go +++ b/scheduler_test.go @@ -13,7 +13,8 @@ func TestScheduler(t *testing.T) { time.Sleep(time.Second * 10) s.RemoveSchedule("1111") - time.Sleep(time.Second * 100) + time.Sleep(time.Second * 3) + s.Stop() } func TestMassiveSchedule(t *testing.T) {