Skip to content

Commit 48f27fe

Browse files
committed
Merge remote-tracking branch 'origin/main'
2 parents 485159a + aa8a35d commit 48f27fe

1 file changed

Lines changed: 11 additions & 7 deletions

File tree

schedule.go

Lines changed: 11 additions & 7 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,6 +46,11 @@ func (s *Task) Wait() {
5046

5147
// Stop stops the scheduler.
5248
func (s *Task) Stop() {
49+
if s.stopped {
50+
return
51+
}
52+
53+
s.stopped = true
5354
close(s.stop)
5455
}
5556

@@ -103,7 +104,10 @@ func Every(interval time.Duration, task func() bool) *Task {
103104
for {
104105
select {
105106
case <-ticker.C:
106-
task()
107+
res := task()
108+
if !res {
109+
scheduler.Stop()
110+
}
107111

108112
scheduler.nextExecution = time.Now().Add(interval)
109113

0 commit comments

Comments
 (0)