|
|
@@ -137,8 +137,11 @@ func (c *Cron) Location() *time.Location {
|
|
|
return c.location
|
|
|
}
|
|
|
|
|
|
-// Start the cron scheduler in its own go-routine.
|
|
|
+// Start the cron scheduler in its own go-routine, or no-op if already started.
|
|
|
func (c *Cron) Start() {
|
|
|
+ if c.running {
|
|
|
+ return
|
|
|
+ }
|
|
|
c.running = true
|
|
|
go c.run()
|
|
|
}
|
|
|
@@ -177,8 +180,9 @@ func (c *Cron) run() {
|
|
|
effective = c.entries[0].Next
|
|
|
}
|
|
|
|
|
|
+ timer := time.NewTimer(effective.Sub(now))
|
|
|
select {
|
|
|
- case now = <-time.After(effective.Sub(now)):
|
|
|
+ case now = <-timer.C:
|
|
|
// Run every entry whose next time was this effective time.
|
|
|
for _, e := range c.entries {
|
|
|
if e.Next != effective {
|
|
|
@@ -198,11 +202,13 @@ func (c *Cron) run() {
|
|
|
c.snapshot <- c.entrySnapshot()
|
|
|
|
|
|
case <-c.stop:
|
|
|
+ timer.Stop()
|
|
|
return
|
|
|
}
|
|
|
|
|
|
// 'now' should be updated after newEntry and snapshot cases.
|
|
|
now = time.Now().In(c.location)
|
|
|
+ timer.Stop()
|
|
|
}
|
|
|
}
|
|
|
|