Skip to content

Commit 1bb9f28

Browse files
committed
fix: fixed Every not stopping
1 parent 485159a commit 1bb9f28

1 file changed

Lines changed: 11 additions & 9 deletions

File tree

schedule.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff 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.
3637
func (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.
5248
func (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

0 commit comments

Comments
 (0)