From 98ba0039214b39488a4a1aea44c15ef880b49e9e Mon Sep 17 00:00:00 2001 From: Gani Georgiev Date: Wed, 20 Mar 2024 23:55:32 +0200 Subject: [PATCH] added done channel for the cron ticker --- tools/cron/cron.go | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/tools/cron/cron.go b/tools/cron/cron.go index 24c66e5e..39d3627c 100644 --- a/tools/cron/cron.go +++ b/tools/cron/cron.go @@ -27,6 +27,7 @@ type Cron struct { startTimer *time.Timer jobs map[string]*job interval time.Duration + tickerDone chan bool sync.RWMutex } @@ -38,9 +39,10 @@ type Cron struct { // You can change the default timezone with Cron.SetTimezone(). func New() *Cron { return &Cron{ - interval: 1 * time.Minute, - timezone: time.UTC, - jobs: map[string]*job{}, + interval: 1 * time.Minute, + timezone: time.UTC, + jobs: map[string]*job{}, + tickerDone: make(chan bool), } } @@ -142,6 +144,7 @@ func (c *Cron) Stop() { return // already stopped } + c.tickerDone <- true c.ticker.Stop() c.ticker = nil } @@ -168,8 +171,13 @@ func (c *Cron) Start() { // run after each tick go func() { - for t := range c.ticker.C { - c.runDue(t) + for { + select { + case <-c.tickerDone: + return + case t := <-c.ticker.C: + c.runDue(t) + } } }() })