Start starts the cron ticker. Calling Start() on already started cron will restart the ticker.
()
| 176 | // |
| 177 | // Calling Start() on already started cron will restart the ticker. |
| 178 | func (c *Cron) Start() { |
| 179 | c.Stop() |
| 180 | |
| 181 | // delay the ticker to start at 00 of 1 c.interval duration |
| 182 | now := time.Now() |
| 183 | next := now.Add(c.interval).Truncate(c.interval) |
| 184 | delay := next.Sub(now) |
| 185 | |
| 186 | c.mux.Lock() |
| 187 | c.startTimer = time.AfterFunc(delay, func() { |
| 188 | c.mux.Lock() |
| 189 | c.ticker = time.NewTicker(c.interval) |
| 190 | c.mux.Unlock() |
| 191 | |
| 192 | // run immediately at 00 |
| 193 | c.runDue(time.Now()) |
| 194 | |
| 195 | // run after each tick |
| 196 | go func() { |
| 197 | for { |
| 198 | select { |
| 199 | case <-c.tickerDone: |
| 200 | return |
| 201 | case t := <-c.ticker.C: |
| 202 | c.runDue(t) |
| 203 | } |
| 204 | } |
| 205 | }() |
| 206 | }) |
| 207 | c.mux.Unlock() |
| 208 | } |
| 209 | |
| 210 | // HasStarted checks whether the current Cron ticker has been started. |
| 211 | func (c *Cron) HasStarted() bool { |