From 32f074f5617aa8a7c39a584c6f5149fc7d974dd4 Mon Sep 17 00:00:00 2001 From: Callum Jones Date: Mon, 16 Dec 2019 13:45:27 +0000 Subject: [PATCH] update when logic --- pkg/when/when.go | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/pkg/when/when.go b/pkg/when/when.go index dc2f844b9..32256f2f6 100644 --- a/pkg/when/when.go +++ b/pkg/when/when.go @@ -48,24 +48,21 @@ func When(t time.Time, fn func()) (*Timer, error) { go func() { ticker := time.NewTicker(Resolution) - for { - select { - case tick := <-ticker.C: - var toStop []*Timer - - mutex.Lock() - for timer := range timers { - if tick.Round(Resolution).Equal(timer.t.Round(Resolution)) { - logrus.Debugf("Starting scheduled event (is now %s)", timer.t) - go timer.fn() - toStop = append(toStop, timer) - } + for tick := range ticker.C { + var toStop []*Timer + + mutex.Lock() + for timer := range timers { + if tick.Round(Resolution).Equal(timer.t.Round(Resolution)) || tick.Round(Resolution).After(timer.t.Round(Resolution)) { + logrus.Debugf("Starting scheduled event (is now %s)", timer.t) + go timer.fn() + toStop = append(toStop, timer) } - mutex.Unlock() + } + mutex.Unlock() - for _, timer := range toStop { - timer.Stop() - } + for _, timer := range toStop { + timer.Stop() } } }()