cron.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. // Simple interface for submitted cron jobs.
  17. type Job interface {
  18. Run()
  19. }
  20. // A cron entry consists of a schedule and the func to execute on that schedule.
  21. type Entry struct {
  22. *Schedule
  23. Next time.Time
  24. Job Job
  25. }
  26. type byTime []*Entry
  27. func (s byTime) Len() int { return len(s) }
  28. func (s byTime) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
  29. func (s byTime) Less(i, j int) bool {
  30. // Two zero times should return false.
  31. // Otherwise, zero is "greater" than any other time.
  32. // (To sort it at the end of the list.)
  33. if s[i].Next.IsZero() {
  34. return false
  35. }
  36. if s[j].Next.IsZero() {
  37. return true
  38. }
  39. return s[i].Next.Before(s[j].Next)
  40. }
  41. func New() *Cron {
  42. return &Cron{
  43. Entries: nil,
  44. add: make(chan *Entry, 1),
  45. stop: make(chan struct{}, 1),
  46. }
  47. }
  48. // Provide a default implementation for those that want to run a simple func.
  49. type jobAdapter func()
  50. func (r jobAdapter) Run() { r() }
  51. func (c *Cron) AddFunc(spec string, cmd func()) {
  52. c.AddJob(spec, jobAdapter(cmd))
  53. }
  54. func (c *Cron) AddJob(spec string, cmd Job) {
  55. entry := &Entry{Parse(spec), time.Time{}, cmd}
  56. select {
  57. case c.add <- entry:
  58. // The run loop accepted the entry, nothing more to do.
  59. return
  60. default:
  61. // No one listening to that channel, so just add to the array.
  62. c.Entries = append(c.Entries, entry)
  63. entry.Next = entry.Schedule.Next(time.Now().Local()) // Just in case..
  64. }
  65. }
  66. func (c *Cron) Start() {
  67. go c.Run()
  68. }
  69. func (c *Cron) Run() {
  70. // Figure out the next activation times for each entry.
  71. now := time.Now().Local()
  72. for _, entry := range c.Entries {
  73. entry.Next = entry.Schedule.Next(now)
  74. }
  75. for {
  76. // Determine the next entry to run.
  77. sort.Sort(byTime(c.Entries))
  78. var effective time.Time
  79. if len(c.Entries) == 0 || c.Entries[0].Next.IsZero() {
  80. // If there are no entries yet, just sleep - it still handles new entries
  81. // and stop requests.
  82. effective = now.AddDate(10, 0, 0)
  83. } else {
  84. effective = c.Entries[0].Next
  85. }
  86. select {
  87. case now = <-time.After(effective.Sub(now)):
  88. // Run every entry whose next time was this effective time.
  89. for _, e := range c.Entries {
  90. if e.Next != effective {
  91. break
  92. }
  93. go e.Job.Run()
  94. e.Next = e.Schedule.Next(effective)
  95. }
  96. case newEntry := <-c.add:
  97. c.Entries = append(c.Entries, newEntry)
  98. newEntry.Next = newEntry.Schedule.Next(now)
  99. case <-c.stop:
  100. return
  101. }
  102. }
  103. }
  104. func (c Cron) Stop() {
  105. c.stop <- struct{}{}
  106. }