doc.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. Package cron implements a cron spec parser and job runner.
  3. Usage
  4. Callers may register Funcs to be invoked on a given schedule. Cron will run
  5. them in their own goroutines.
  6. c := cron.New()
  7. c.AddFunc("Every hour on the half hour","0 30 * * * *", func() { fmt.Println("Every hour on the half hour") })
  8. c.AddFunc("Every hour","@hourly", func() { fmt.Println("Every hour") })
  9. c.AddFunc("Every hour and a half","@every 1h30m", func() { fmt.Println("Every hour thirty") })
  10. c.Start()
  11. ..
  12. // Funcs are invoked in their own goroutine, asynchronously.
  13. ...
  14. // Funcs may also be added to a running Cron
  15. c.AddFunc("@daily", func() { fmt.Println("Every day") })
  16. ..
  17. // Inspect the cron job entries' next and previous run times.
  18. inspect(c.Entries())
  19. ..
  20. c.Stop() // Stop the scheduler (does not stop any jobs already running).
  21. CRON Expression Format
  22. A cron expression represents a set of times, using 6 space-separated fields.
  23. Field name | Mandatory? | Allowed values | Allowed special characters
  24. ---------- | ---------- | -------------- | --------------------------
  25. Seconds | Yes | 0-59 | * / , -
  26. Minutes | Yes | 0-59 | * / , -
  27. Hours | Yes | 0-23 | * / , -
  28. Day of month | Yes | 1-31 | * / , - ?
  29. Month | Yes | 1-12 or JAN-DEC | * / , -
  30. Day of week | Yes | 0-6 or SUN-SAT | * / , - ?
  31. Note: Month and Day-of-week field values are case insensitive. "SUN", "Sun",
  32. and "sun" are equally accepted.
  33. Special Characters
  34. Asterisk ( * )
  35. The asterisk indicates that the cron expression will match for all values of the
  36. field; e.g., using an asterisk in the 5th field (month) would indicate every
  37. month.
  38. Slash ( / )
  39. Slashes are used to describe increments of ranges. For example 3-59/15 in the
  40. 1st field (minutes) would indicate the 3rd minute of the hour and every 15
  41. minutes thereafter. The form "*\/..." is equivalent to the form "first-last/...",
  42. that is, an increment over the largest possible range of the field. The form
  43. "N/..." is accepted as meaning "N-MAX/...", that is, starting at N, use the
  44. increment until the end of that specific range. It does not wrap around.
  45. Comma ( , )
  46. Commas are used to separate items of a list. For example, using "MON,WED,FRI" in
  47. the 5th field (day of week) would mean Mondays, Wednesdays and Fridays.
  48. Hyphen ( - )
  49. Hyphens are used to define ranges. For example, 9-17 would indicate every
  50. hour between 9am and 5pm inclusive.
  51. Question mark ( ? )
  52. Question mark may be used instead of '*' for leaving either day-of-month or
  53. day-of-week blank.
  54. Predefined schedules
  55. You may use one of several pre-defined schedules in place of a cron expression.
  56. Entry | Description | Equivalent To
  57. ----- | ----------- | -------------
  58. @yearly (or @annually) | Run once a year, midnight, Jan. 1st | 0 0 0 1 1 *
  59. @monthly | Run once a month, midnight, first of month | 0 0 0 1 * *
  60. @weekly | Run once a week, midnight on Sunday | 0 0 0 * * 0
  61. @daily (or @midnight) | Run once a day, midnight | 0 0 0 * * *
  62. @hourly | Run once an hour, beginning of hour | 0 0 * * * *
  63. Intervals
  64. You may also schedule a job to execute at fixed intervals. This is supported by
  65. formatting the cron spec like this:
  66. @every <duration>
  67. where "duration" is a string accepted by time.ParseDuration
  68. (http://golang.org/pkg/time/#ParseDuration).
  69. For example, "@every 1h30m10s" would indicate a schedule that activates every
  70. 1 hour, 30 minutes, 10 seconds.
  71. Note: The interval does not take the job runtime into account. For example,
  72. if a job takes 3 minutes to run, and it is scheduled to run every 5 minutes,
  73. it will have only 2 minutes of idle time between each run.
  74. Time zones
  75. All interpretation and scheduling is done in the machine's local time zone (as
  76. provided by the Go time package (http://www.golang.org/pkg/time).
  77. Be aware that jobs scheduled during daylight-savings leap-ahead transitions will
  78. not be run!
  79. Thread safety
  80. Since the Cron service runs concurrently with the calling code, some amount of
  81. care must be taken to ensure proper synchronization.
  82. All cron methods are designed to be correctly synchronized as long as the caller
  83. ensures that invocations have a clear happens-before ordering between them.
  84. Implementation
  85. Cron entries are stored in an array, sorted by their next activation time. Cron
  86. sleeps until the next job is due to be run.
  87. Upon waking:
  88. - it runs each entry that is active on that second
  89. - it calculates the next run times for the jobs that were run
  90. - it re-sorts the array of entries by next activation time.
  91. - it goes to sleep until the soonest job.
  92. */
  93. package cron