From 8a0422aa12520d4a286c69a1a309c32cbc03eee5 Mon Sep 17 00:00:00 2001 From: Marvin Wendt Date: Thu, 27 Jun 2024 09:35:33 +0200 Subject: [PATCH 1/4] fixed linting --- .golangci.yml | 1 + examples_test.go | 1 + schedule.go | 9 ++++++--- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 743bfac..89a230b 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -163,6 +163,7 @@ linters-settings: - d any - data any - n any + - t time.Time - f func() - cb func() - t testing.T diff --git a/examples_test.go b/examples_test.go index 1e2dba0..bc764b9 100644 --- a/examples_test.go +++ b/examples_test.go @@ -30,6 +30,7 @@ func ExampleAt() { func ExampleEvery() { task := schedule.Every(time.Second, func() bool { fmt.Println("1 second is over!") + return true // return false to stop the task }) diff --git a/schedule.go b/schedule.go index 635d29d..b332691 100644 --- a/schedule.go +++ b/schedule.go @@ -55,13 +55,13 @@ func (s *Task) Stop() { // After executes the task after the given duration. // The function is non-blocking. If you want to wait for the task to be executed, use the Task.Wait method. -func After(d time.Duration, task func()) *Task { +func After(duration time.Duration, task func()) *Task { scheduler := newTask() - scheduler.nextExecution = time.Now().Add(d) + scheduler.nextExecution = time.Now().Add(duration) go func() { select { - case <-time.After(d): + case <-time.After(duration): task() scheduler.Stop() case <-scheduler.stop: @@ -104,9 +104,12 @@ func Every(interval time.Duration, task func() bool) *Task { select { case <-ticker.C: task() + scheduler.nextExecution = time.Now().Add(interval) + case <-scheduler.stop: ticker.Stop() + return } } From 1bb9f28a708567d93a171afd54a312105f877d37 Mon Sep 17 00:00:00 2001 From: Marvin Wendt Date: Thu, 27 Feb 2025 09:43:57 +0100 Subject: [PATCH 2/4] fix: fixed `Every` not stopping --- schedule.go | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/schedule.go b/schedule.go index b332691..591f267 100644 --- a/schedule.go +++ b/schedule.go @@ -7,6 +7,7 @@ type Task struct { stop chan struct{} nextExecution time.Time startedAt time.Time + stopped bool } // newTask creates a new Task. @@ -34,12 +35,7 @@ func (s *Task) ExecutesIn() time.Duration { // IsActive returns true if the scheduler is active. func (s *Task) IsActive() bool { - select { - case <-s.stop: - return false - default: - return true - } + return !s.stopped } // Wait blocks until the scheduler is stopped. @@ -50,7 +46,11 @@ func (s *Task) Wait() { // Stop stops the scheduler. func (s *Task) Stop() { - close(s.stop) + if !s.stopped { + close(s.stop) + } + + s.stopped = true } // After executes the task after the given duration. @@ -103,10 +103,12 @@ func Every(interval time.Duration, task func() bool) *Task { for { select { case <-ticker.C: - task() + res := task() + if !res { + scheduler.Stop() + } scheduler.nextExecution = time.Now().Add(interval) - case <-scheduler.stop: ticker.Stop() From 1a071cd1226aa24c610e8957abe572ef49d2d98a Mon Sep 17 00:00:00 2001 From: Marvin Wendt Date: Thu, 27 Feb 2025 09:45:55 +0100 Subject: [PATCH 3/4] fix: fixed `Every` not stopping --- schedule.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/schedule.go b/schedule.go index 591f267..548d13a 100644 --- a/schedule.go +++ b/schedule.go @@ -46,11 +46,12 @@ func (s *Task) Wait() { // Stop stops the scheduler. func (s *Task) Stop() { - if !s.stopped { - close(s.stop) + if s.stopped { + return } s.stopped = true + close(s.stop) } // After executes the task after the given duration. From e80c9ff745b7ee7dcbc1750b7e95ad9db1f328da Mon Sep 17 00:00:00 2001 From: Marvin Wendt Date: Thu, 27 Feb 2025 09:48:52 +0100 Subject: [PATCH 4/4] fixed some linting --- schedule.go | 1 + 1 file changed, 1 insertion(+) diff --git a/schedule.go b/schedule.go index 548d13a..b12c18a 100644 --- a/schedule.go +++ b/schedule.go @@ -110,6 +110,7 @@ func Every(interval time.Duration, task func() bool) *Task { } scheduler.nextExecution = time.Now().Add(interval) + case <-scheduler.stop: ticker.Stop()