Skip to content

Commit

Permalink
update when logic
Browse files Browse the repository at this point in the history
  • Loading branch information
cj123 committed Dec 16, 2019
1 parent d1c8d28 commit 32f074f
Showing 1 changed file with 13 additions and 16 deletions.
29 changes: 13 additions & 16 deletions pkg/when/when.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
}()
Expand Down

0 comments on commit 32f074f

Please sign in to comment.