git.sr.ht/~pingoo/stdx@v0.0.0-20240218134121-094174641f6e/cron/doc.go (about)

     1  /*
     2  Package cron implements a cron spec parser and job runner.
     3  
     4  # Installation
     5  
     6  To download the specific tagged release, run:
     7  
     8  	go get git.sr.ht/~pingoo/stdx/cron@v3.0.0
     9  
    10  Import it in your program as:
    11  
    12  	import "git.sr.ht/~pingoo/stdx/cron"
    13  
    14  It requires Go 1.11 or later due to usage of Go Modules.
    15  
    16  # Usage
    17  
    18  Callers may register Funcs to be invoked on a given schedule.  Cron will run
    19  them in their own goroutines.
    20  
    21  	c := cron.New()
    22  	c.AddFunc("30 * * * *", func() { fmt.Println("Every hour on the half hour") })
    23  	c.AddFunc("30 3-6,20-23 * * *", func() { fmt.Println(".. in the range 3-6am, 8-11pm") })
    24  	c.AddFunc("CRON_TZ=Asia/Tokyo 30 04 * * *", func() { fmt.Println("Runs at 04:30 Tokyo time every day") })
    25  	c.AddFunc("@hourly",      func() { fmt.Println("Every hour, starting an hour from now") })
    26  	c.AddFunc("@every 1h30m", func() { fmt.Println("Every hour thirty, starting an hour thirty from now") })
    27  	c.Start()
    28  	..
    29  	// Funcs are invoked in their own goroutine, asynchronously.
    30  	...
    31  	// Funcs may also be added to a running Cron
    32  	c.AddFunc("@daily", func() { fmt.Println("Every day") })
    33  	..
    34  	// Inspect the cron job entries' next and previous run times.
    35  	inspect(c.Entries())
    36  	..
    37  	c.Stop()  // Stop the scheduler (does not stop any jobs already running).
    38  
    39  # CRON Expression Format
    40  
    41  A cron expression represents a set of times, using 5 space-separated fields.
    42  
    43  	Field name   | Mandatory? | Allowed values  | Allowed special characters
    44  	----------   | ---------- | --------------  | --------------------------
    45  	Minutes      | Yes        | 0-59            | * / , -
    46  	Hours        | Yes        | 0-23            | * / , -
    47  	Day of month | Yes        | 1-31            | * / , - ?
    48  	Month        | Yes        | 1-12 or JAN-DEC | * / , -
    49  	Day of week  | Yes        | 0-6 or SUN-SAT  | * / , - ?
    50  
    51  Month and Day-of-week field values are case insensitive.  "SUN", "Sun", and
    52  "sun" are equally accepted.
    53  
    54  The specific interpretation of the format is based on the Cron Wikipedia page:
    55  https://en.wikipedia.org/wiki/Cron
    56  
    57  # Alternative Formats
    58  
    59  Alternative Cron expression formats support other fields like seconds. You can
    60  implement that by creating a custom Parser as follows.
    61  
    62  	cron.New(
    63  		cron.WithParser(
    64  			cron.NewParser(
    65  				cron.SecondOptional | cron.Minute | cron.Hour | cron.Dom | cron.Month | cron.Dow | cron.Descriptor)))
    66  
    67  Since adding Seconds is the most common modification to the standard cron spec,
    68  cron provides a builtin function to do that, which is equivalent to the custom
    69  parser you saw earlier, except that its seconds field is REQUIRED:
    70  
    71  	cron.New(cron.WithSeconds())
    72  
    73  That emulates Quartz, the most popular alternative Cron schedule format:
    74  http://www.quartz-scheduler.org/documentation/quartz-2.x/tutorials/crontrigger.html
    75  
    76  # Special Characters
    77  
    78  Asterisk ( * )
    79  
    80  The asterisk indicates that the cron expression will match for all values of the
    81  field; e.g., using an asterisk in the 5th field (month) would indicate every
    82  month.
    83  
    84  Slash ( / )
    85  
    86  Slashes are used to describe increments of ranges. For example 3-59/15 in the
    87  1st field (minutes) would indicate the 3rd minute of the hour and every 15
    88  minutes thereafter. The form "*\/..." is equivalent to the form "first-last/...",
    89  that is, an increment over the largest possible range of the field.  The form
    90  "N/..." is accepted as meaning "N-MAX/...", that is, starting at N, use the
    91  increment until the end of that specific range.  It does not wrap around.
    92  
    93  Comma ( , )
    94  
    95  Commas are used to separate items of a list. For example, using "MON,WED,FRI" in
    96  the 5th field (day of week) would mean Mondays, Wednesdays and Fridays.
    97  
    98  Hyphen ( - )
    99  
   100  Hyphens are used to define ranges. For example, 9-17 would indicate every
   101  hour between 9am and 5pm inclusive.
   102  
   103  Question mark ( ? )
   104  
   105  Question mark may be used instead of '*' for leaving either day-of-month or
   106  day-of-week blank.
   107  
   108  # Predefined schedules
   109  
   110  You may use one of several pre-defined schedules in place of a cron expression.
   111  
   112  	Entry                  | Description                                | Equivalent To
   113  	-----                  | -----------                                | -------------
   114  	@yearly (or @annually) | Run once a year, midnight, Jan. 1st        | 0 0 1 1 *
   115  	@monthly               | Run once a month, midnight, first of month | 0 0 1 * *
   116  	@weekly                | Run once a week, midnight between Sat/Sun  | 0 0 * * 0
   117  	@daily (or @midnight)  | Run once a day, midnight                   | 0 0 * * *
   118  	@hourly                | Run once an hour, beginning of hour        | 0 * * * *
   119  
   120  # Intervals
   121  
   122  You may also schedule a job to execute at fixed intervals, starting at the time it's added
   123  or cron is run. This is supported by formatting the cron spec like this:
   124  
   125  	@every <duration>
   126  
   127  where "duration" is a string accepted by time.ParseDuration
   128  (http://golang.org/pkg/time/#ParseDuration).
   129  
   130  For example, "@every 1h30m10s" would indicate a schedule that activates after
   131  1 hour, 30 minutes, 10 seconds, and then every interval after that.
   132  
   133  Note: The interval does not take the job runtime into account.  For example,
   134  if a job takes 3 minutes to run, and it is scheduled to run every 5 minutes,
   135  it will have only 2 minutes of idle time between each run.
   136  
   137  # Time zones
   138  
   139  By default, all interpretation and scheduling is done in the machine's local
   140  time zone (time.Local). You can specify a different time zone on construction:
   141  
   142  	cron.New(
   143  	    cron.WithLocation(time.UTC))
   144  
   145  Individual cron schedules may also override the time zone they are to be
   146  interpreted in by providing an additional space-separated field at the beginning
   147  of the cron spec, of the form "CRON_TZ=Asia/Tokyo".
   148  
   149  For example:
   150  
   151  	# Runs at 6am in time.Local
   152  	cron.New().AddFunc("0 6 * * ?", ...)
   153  
   154  	# Runs at 6am in America/New_York
   155  	nyc, _ := time.LoadLocation("America/New_York")
   156  	c := cron.New(cron.WithLocation(nyc))
   157  	c.AddFunc("0 6 * * ?", ...)
   158  
   159  	# Runs at 6am in Asia/Tokyo
   160  	cron.New().AddFunc("CRON_TZ=Asia/Tokyo 0 6 * * ?", ...)
   161  
   162  	# Runs at 6am in Asia/Tokyo
   163  	c := cron.New(cron.WithLocation(nyc))
   164  	c.SetLocation("America/New_York")
   165  	c.AddFunc("CRON_TZ=Asia/Tokyo 0 6 * * ?", ...)
   166  
   167  The prefix "TZ=(TIME ZONE)" is also supported for legacy compatibility.
   168  
   169  Be aware that jobs scheduled during daylight-savings leap-ahead transitions will
   170  not be run!
   171  
   172  # Job Wrappers
   173  
   174  A Cron runner may be configured with a chain of job wrappers to add
   175  cross-cutting functionality to all submitted jobs. For example, they may be used
   176  to achieve the following effects:
   177  
   178    - Recover any panics from jobs (activated by default)
   179    - Delay a job's execution if the previous run hasn't completed yet
   180    - Skip a job's execution if the previous run hasn't completed yet
   181    - Log each job's invocations
   182  
   183  Install wrappers for all jobs added to a cron using the `cron.WithChain` option:
   184  
   185  	cron.New(cron.WithChain(
   186  		cron.SkipIfStillRunning(logger),
   187  	))
   188  
   189  Install wrappers for individual jobs by explicitly wrapping them:
   190  
   191  	job = cron.NewChain(
   192  		cron.SkipIfStillRunning(logger),
   193  	).Then(job)
   194  
   195  # Thread safety
   196  
   197  Since the Cron service runs concurrently with the calling code, some amount of
   198  care must be taken to ensure proper synchronization.
   199  
   200  All cron methods are designed to be correctly synchronized as long as the caller
   201  ensures that invocations have a clear happens-before ordering between them.
   202  
   203  # Logging
   204  
   205  Cron defines a Logger interface that is a subset of the one defined in
   206  github.com/go-logr/logr. It has two logging levels (Info and Error), and
   207  parameters are key/value pairs. This makes it possible for cron logging to plug
   208  into structured logging systems. An adapter, [Verbose]PrintfLogger, is provided
   209  to wrap the standard library *log.Logger.
   210  
   211  For additional insight into Cron operations, verbose logging may be activated
   212  which will record job runs, scheduling decisions, and added or removed jobs.
   213  Activate it with a one-off logger as follows:
   214  
   215  	cron.New(
   216  		cron.WithLogger(
   217  			cron.VerbosePrintfLogger(log.New(os.Stdout, "cron: ", log.LstdFlags))))
   218  
   219  # Implementation
   220  
   221  Cron entries are stored in an array, sorted by their next activation time.  Cron
   222  sleeps until the next job is due to be run.
   223  
   224  Upon waking:
   225    - it runs each entry that is active on that second
   226    - it calculates the next run times for the jobs that were run
   227    - it re-sorts the array of entries by next activation time.
   228    - it goes to sleep until the soonest job.
   229  */
   230  package cron