File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -7,6 +7,7 @@ type Task struct {
77 stop chan struct {}
88 nextExecution time.Time
99 startedAt time.Time
10+ stopped bool
1011}
1112
1213// newTask creates a new Task.
@@ -34,12 +35,7 @@ func (s *Task) ExecutesIn() time.Duration {
3435
3536// IsActive returns true if the scheduler is active.
3637func (s * Task ) IsActive () bool {
37- select {
38- case <- s .stop :
39- return false
40- default :
41- return true
42- }
38+ return ! s .stopped
4339}
4440
4541// Wait blocks until the scheduler is stopped.
@@ -50,7 +46,11 @@ func (s *Task) Wait() {
5046
5147// Stop stops the scheduler.
5248func (s * Task ) Stop () {
53- close (s .stop )
49+ if ! s .stopped {
50+ close (s .stop )
51+ }
52+
53+ s .stopped = true
5454}
5555
5656// After executes the task after the given duration.
@@ -103,10 +103,12 @@ func Every(interval time.Duration, task func() bool) *Task {
103103 for {
104104 select {
105105 case <- ticker .C :
106- task ()
106+ res := task ()
107+ if ! res {
108+ scheduler .Stop ()
109+ }
107110
108111 scheduler .nextExecution = time .Now ().Add (interval )
109-
110112 case <- scheduler .stop :
111113 ticker .Stop ()
112114
You can’t perform that action at this time.
0 commit comments