scheduler

This commit is contained in:
insanity@loafle.com 2017-04-29 16:22:51 +09:00
parent d34505dd62
commit be7c36013f
2 changed files with 11 additions and 12 deletions

View File

@ -81,8 +81,9 @@ func (t *Task) addNextAt() {
} }
type Scheduler struct { type Scheduler struct {
Tasks [MAX_TASKS]*Task Tasks [MAX_TASKS]*Task
size int size int
stop chan bool
} }
func (s *Scheduler) Len() int { func (s *Scheduler) Len() int {
@ -129,7 +130,6 @@ func (s *Scheduler) addTask(id string, intervalSec uint64) *Task {
return Task return Task
} }
func (s *Scheduler) runAll() { func (s *Scheduler) runAll() {
runnableTasks, n := s.RunnableTasks() runnableTasks, n := s.RunnableTasks()
@ -161,10 +161,8 @@ func (s *Scheduler) remove(id string) error {
return nil return nil
} }
func (s *Scheduler) Start() {
s.stop = make(chan bool, 1)
func (s *Scheduler) Start() chan bool {
stopped := make(chan bool, 1)
ticker := time.NewTicker(time.Second * 1) ticker := time.NewTicker(time.Second * 1)
go func() { go func() {
@ -172,13 +170,15 @@ func (s *Scheduler) Start() chan bool {
select { select {
case <-ticker.C: case <-ticker.C:
s.runAll() s.runAll()
case <-stopped: case <-s.stop:
return return
} }
} }
}() }()
}
return stopped func (s *Scheduler) Stop() {
s.stop <- true
} }
func (s *Scheduler) NewSchedule(id string, interval uint64, fn interface{}) error { func (s *Scheduler) NewSchedule(id string, interval uint64, fn interface{}) error {
@ -223,5 +223,3 @@ func (s *Scheduler) UpdateInterval(id string, interval uint64) {
} }
} }
} }

View File

@ -13,7 +13,8 @@ func TestScheduler(t *testing.T) {
time.Sleep(time.Second * 10) time.Sleep(time.Second * 10)
s.RemoveSchedule("1111") s.RemoveSchedule("1111")
time.Sleep(time.Second * 100) time.Sleep(time.Second * 3)
s.Stop()
} }
func TestMassiveSchedule(t *testing.T) { func TestMassiveSchedule(t *testing.T) {