cron.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // This library implements a cron spec parser and runner. See the README for
  2. // more details.
  3. package cron
  4. import (
  5. "sort"
  6. "time"
  7. )
  8. // Cron keeps track of any number of entries, invoking the associated func as
  9. // specified by the spec. See http://en.wikipedia.org/wiki/Cron
  10. // It may be started and stopped.
  11. type Cron struct {
  12. Entries []*Entry
  13. stop chan struct{}
  14. add chan *Entry
  15. }
  16. // A cron entry consists of a schedule and the func to execute on that schedule.
  17. type Entry struct {
  18. *Schedule
  19. Next time.Time
  20. Func func()
  21. }
  22. type byTime []*Entry
  23. func (s byTime) Len() int { return len(s) }
  24. func (s byTime) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
  25. func (s byTime) Less(i, j int) bool {
  26. // Two zero times should return false.
  27. // Otherwise, zero is "greater" than any other time.
  28. // (To sort it at the end of the list.)
  29. if s[i].Next.IsZero() {
  30. return false
  31. }
  32. if s[j].Next.IsZero() {
  33. return true
  34. }
  35. return s[i].Next.Before(s[j].Next)
  36. }
  37. func New() *Cron {
  38. return &Cron{
  39. Entries: nil,
  40. add: make(chan *Entry, 1),
  41. stop: make(chan struct{}, 1),
  42. }
  43. }
  44. func (c *Cron) Add(spec string, cmd func()) {
  45. entry := &Entry{Parse(spec), time.Time{}, cmd}
  46. select {
  47. case c.add <- entry:
  48. // The run loop accepted the entry, nothing more to do.
  49. return
  50. default:
  51. // No one listening to that channel, so just add to the array.
  52. c.Entries = append(c.Entries, entry)
  53. entry.Next = entry.Schedule.Next(time.Now().Local()) // Just in case..
  54. }
  55. }
  56. func (c *Cron) Start() {
  57. go c.Run()
  58. }
  59. func (c *Cron) Run() {
  60. // Figure out the next activation times for each entry.
  61. now := time.Now().Local()
  62. for _, entry := range c.Entries {
  63. entry.Next = entry.Schedule.Next(now)
  64. }
  65. for {
  66. // Determine the next entry to run.
  67. sort.Sort(byTime(c.Entries))
  68. var effective time.Time
  69. if len(c.Entries) == 0 || c.Entries[0].Next.IsZero() {
  70. // If there are no entries yet, just sleep - it still handles new entries
  71. // and stop requests.
  72. effective = now.AddDate(10, 0, 0)
  73. } else {
  74. effective = c.Entries[0].Next
  75. }
  76. select {
  77. case now = <-time.After(effective.Sub(now)):
  78. // Run every entry whose next time was this effective time.
  79. for _, e := range c.Entries {
  80. if e.Next != effective {
  81. break
  82. }
  83. go e.Func()
  84. e.Next = e.Schedule.Next(effective)
  85. }
  86. case newEntry := <-c.add:
  87. c.Entries = append(c.Entries, newEntry)
  88. newEntry.Next = newEntry.Schedule.Next(now)
  89. case <-c.stop:
  90. return
  91. }
  92. }
  93. }
  94. func (c Cron) Stop() {
  95. c.stop <- struct{}{}
  96. }