Skip to content

Commit

Permalink
added done channel for the cron ticker
Browse files Browse the repository at this point in the history
  • Loading branch information
ganigeorgiev committed Mar 20, 2024
1 parent 309c4fe commit 98ba003
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions tools/cron/cron.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type Cron struct {
startTimer *time.Timer
jobs map[string]*job
interval time.Duration
tickerDone chan bool

sync.RWMutex
}
Expand All @@ -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),
}
}

Expand Down Expand Up @@ -142,6 +144,7 @@ func (c *Cron) Stop() {
return // already stopped
}

c.tickerDone <- true
c.ticker.Stop()
c.ticker = nil
}
Expand All @@ -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)
}
}
}()
})
Expand Down

0 comments on commit 98ba003

Please sign in to comment.