scheduler
This commit is contained in:
parent
d34505dd62
commit
be7c36013f
16
scheduler.go
16
scheduler.go
@ -83,6 +83,7 @@ func (t *Task) addNextAt() {
|
||||
type Scheduler struct {
|
||||
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) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -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) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user