cron.go 3.3 KB

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