run the scheduler.. this is private just due to the need to synchronize access to the 'running' state variable.
()
| 237 | // run the scheduler.. this is private just due to the need to synchronize |
| 238 | // access to the 'running' state variable. |
| 239 | func (c *Cron) run() { |
| 240 | c.logger.Info("start") |
| 241 | |
| 242 | // Figure out the next activation times for each entry. |
| 243 | now := c.now() |
| 244 | for _, entry := range c.entries { |
| 245 | entry.Next = entry.Schedule.Next(now) |
| 246 | c.logger.Info("schedule", "now", now, "entry", entry.ID, "next", entry.Next) |
| 247 | } |
| 248 | |
| 249 | for { |
| 250 | // Determine the next entry to run. |
| 251 | sort.Sort(byTime(c.entries)) |
| 252 | |
| 253 | var timer *time.Timer |
| 254 | if len(c.entries) == 0 || c.entries[0].Next.IsZero() { |
| 255 | // If there are no entries yet, just sleep - it still handles new entries |
| 256 | // and stop requests. |
| 257 | timer = time.NewTimer(100000 * time.Hour) |
| 258 | } else { |
| 259 | timer = time.NewTimer(c.entries[0].Next.Sub(now)) |
| 260 | } |
| 261 | |
| 262 | for { |
| 263 | select { |
| 264 | case now = <-timer.C: |
| 265 | now = now.In(c.location) |
| 266 | c.logger.Info("wake", "now", now) |
| 267 | |
| 268 | // Run every entry whose next time was less than now |
| 269 | for _, e := range c.entries { |
| 270 | if e.Next.After(now) || e.Next.IsZero() { |
| 271 | break |
| 272 | } |
| 273 | c.startJob(e.WrappedJob) |
| 274 | e.Prev = e.Next |
| 275 | e.Next = e.Schedule.Next(now) |
| 276 | c.logger.Info("run", "now", now, "entry", e.ID, "next", e.Next) |
| 277 | } |
| 278 | |
| 279 | case newEntry := <-c.add: |
| 280 | timer.Stop() |
| 281 | now = c.now() |
| 282 | newEntry.Next = newEntry.Schedule.Next(now) |
| 283 | c.entries = append(c.entries, newEntry) |
| 284 | c.logger.Info("added", "now", now, "entry", newEntry.ID, "next", newEntry.Next) |
| 285 | |
| 286 | case replyChan := <-c.snapshot: |
| 287 | replyChan <- c.entrySnapshot() |
| 288 | continue |
| 289 | |
| 290 | case <-c.stop: |
| 291 | timer.Stop() |
| 292 | c.logger.Info("stop") |
| 293 | return |
| 294 | |
| 295 | case id := <-c.remove: |
| 296 | timer.Stop() |
no test coverage detected