cron.go 3.2 KB

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