doc.go 4.9 KB

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