added done channel for the cron ticker
This commit is contained in:
parent
309c4fe6fe
commit
98ba003921
|
@ -27,6 +27,7 @@ type Cron struct {
|
||||||
startTimer *time.Timer
|
startTimer *time.Timer
|
||||||
jobs map[string]*job
|
jobs map[string]*job
|
||||||
interval time.Duration
|
interval time.Duration
|
||||||
|
tickerDone chan bool
|
||||||
|
|
||||||
sync.RWMutex
|
sync.RWMutex
|
||||||
}
|
}
|
||||||
|
@ -38,9 +39,10 @@ type Cron struct {
|
||||||
// You can change the default timezone with Cron.SetTimezone().
|
// You can change the default timezone with Cron.SetTimezone().
|
||||||
func New() *Cron {
|
func New() *Cron {
|
||||||
return &Cron{
|
return &Cron{
|
||||||
interval: 1 * time.Minute,
|
interval: 1 * time.Minute,
|
||||||
timezone: time.UTC,
|
timezone: time.UTC,
|
||||||
jobs: map[string]*job{},
|
jobs: map[string]*job{},
|
||||||
|
tickerDone: make(chan bool),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -142,6 +144,7 @@ func (c *Cron) Stop() {
|
||||||
return // already stopped
|
return // already stopped
|
||||||
}
|
}
|
||||||
|
|
||||||
|
c.tickerDone <- true
|
||||||
c.ticker.Stop()
|
c.ticker.Stop()
|
||||||
c.ticker = nil
|
c.ticker = nil
|
||||||
}
|
}
|
||||||
|
@ -168,8 +171,13 @@ func (c *Cron) Start() {
|
||||||
|
|
||||||
// run after each tick
|
// run after each tick
|
||||||
go func() {
|
go func() {
|
||||||
for t := range c.ticker.C {
|
for {
|
||||||
c.runDue(t)
|
select {
|
||||||
|
case <-c.tickerDone:
|
||||||
|
return
|
||||||
|
case t := <-c.ticker.C:
|
||||||
|
c.runDue(t)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in New Issue