cron.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 schedule. It may be started, stopped, and the entries may
  10. // be inspected while running.
  11. type Cron struct {
  12. entries []*Entry
  13. stop chan struct{}
  14. add chan *Entry
  15. snapshot chan []*Entry
  16. running bool
  17. }
  18. // Job is an interface for submitted cron jobs.
  19. type Job interface {
  20. Run()
  21. }
  22. // Entry consists of a schedule and the func to execute on that schedule.
  23. type Entry struct {
  24. // The schedule on which this job should be run.
  25. *Schedule
  26. // The next time the job will run. This is the zero time if Cron has not been
  27. // started or this entry's schedule is unsatisfiable
  28. Next time.Time
  29. // The last time this job was run. This is the zero time if the job has never
  30. // been run.
  31. Prev time.Time
  32. // The Job to run.
  33. Job Job
  34. }
  35. // byTime is a wrapper for sorting the entry array by time
  36. // (with zero time at the end).
  37. type byTime []*Entry
  38. func (s byTime) Len() int { return len(s) }
  39. func (s byTime) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
  40. func (s byTime) Less(i, j int) bool {
  41. // Two zero times should return false.
  42. // Otherwise, zero is "greater" than any other time.
  43. // (To sort it at the end of the list.)
  44. if s[i].Next.IsZero() {
  45. return false
  46. }
  47. if s[j].Next.IsZero() {
  48. return true
  49. }
  50. return s[i].Next.Before(s[j].Next)
  51. }
  52. // New returns a new Cron job runner.
  53. func New() *Cron {
  54. return &Cron{
  55. entries: nil,
  56. add: make(chan *Entry),
  57. stop: make(chan struct{}),
  58. snapshot: make(chan []*Entry),
  59. running: false,
  60. }
  61. }
  62. // jobAdapter provides a default implementation for wrapping a simple func.
  63. type jobAdapter func()
  64. func (r jobAdapter) Run() { r() }
  65. // AddFunc adds a func to the Cron to be run on the given schedule.
  66. func (c *Cron) AddFunc(spec string, cmd func()) {
  67. c.AddJob(spec, jobAdapter(cmd))
  68. }
  69. // AddFunc adds a Job to the Cron to be run on the given schedule.
  70. func (c *Cron) AddJob(spec string, cmd Job) {
  71. entry := &Entry{
  72. Schedule: Parse(spec),
  73. Job: cmd,
  74. }
  75. if !c.running {
  76. c.entries = append(c.entries, entry)
  77. return
  78. }
  79. c.add <- entry
  80. }
  81. // Entries returns a snapshot of the cron entries.
  82. func (c *Cron) Entries() []*Entry {
  83. if c.running {
  84. c.snapshot <- nil
  85. x := <-c.snapshot
  86. return x
  87. }
  88. return c.entrySnapshot()
  89. }
  90. // Start the cron scheduler in its own go-routine.
  91. func (c *Cron) Start() {
  92. c.running = true
  93. go c.run()
  94. }
  95. // Run the scheduler.. this is private just due to the need to synchronize
  96. // access to the 'running' state variable.
  97. func (c *Cron) run() {
  98. // Figure out the next activation times for each entry.
  99. now := time.Now().Local()
  100. for _, entry := range c.entries {
  101. entry.Next = entry.Schedule.Next(now)
  102. }
  103. for {
  104. // Determine the next entry to run.
  105. sort.Sort(byTime(c.entries))
  106. var effective time.Time
  107. if len(c.entries) == 0 || c.entries[0].Next.IsZero() {
  108. // If there are no entries yet, just sleep - it still handles new entries
  109. // and stop requests.
  110. effective = now.AddDate(10, 0, 0)
  111. } else {
  112. effective = c.entries[0].Next
  113. }
  114. select {
  115. case now = <-time.After(effective.Sub(now)):
  116. // Run every entry whose next time was this effective time.
  117. for _, e := range c.entries {
  118. if e.Next != effective {
  119. break
  120. }
  121. go e.Job.Run()
  122. e.Prev = e.Next
  123. e.Next = e.Schedule.Next(effective)
  124. }
  125. case newEntry := <-c.add:
  126. c.entries = append(c.entries, newEntry)
  127. newEntry.Next = newEntry.Schedule.Next(now)
  128. case <-c.snapshot:
  129. c.snapshot <- c.entrySnapshot()
  130. case <-c.stop:
  131. return
  132. }
  133. }
  134. }
  135. // Stop the cron scheduler.
  136. func (c *Cron) Stop() {
  137. c.stop <- struct{}{}
  138. c.running = false
  139. }
  140. // entrySnapshot returns a copy of the current cron entry list.
  141. func (c *Cron) entrySnapshot() []*Entry {
  142. entries := []*Entry{}
  143. for _, e := range c.entries {
  144. entries = append(entries, &Entry{
  145. Schedule: e.Schedule,
  146. Next: e.Next,
  147. Prev: e.Prev,
  148. Job: e.Job,
  149. })
  150. }
  151. return entries
  152. }