cron.go 4.4 KB

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