Golang timed cron expression

Source: Internet
Author: User
Tags ranges
This is a creation in Article, where the information may have evolved or changed.

Go Timing Basic Instance Code

import ("log""github.com/robfig/cron")func main() {i := 0c := cron.New()spec := "0/5 * * * * ?"//var spec_ string = "*/5 * * * * ?"c.AddFunc(spec, func() {i++log.Println("cron running:", i)})c.AddFunc("@every 1h1m", func() {i++log.Println("cron running:", i)})c.Start()select{}//阻塞主线程不退出}
Expression Description:/*usagecallers may register, Funcs to being invoked on a given schedule. Cron would runthem in their own goroutines.c: = cron. New () C.addfunc ("0 * * * *", func () {fmt. Println ("Every hour on the Half Hour")}) C.addfunc ("@hourly", func () {fmt. Println ("Every Hour")}) C.addfunc ("@every 1h30m", func () {fmt. Println ("Every Hour Thirty")}) C.start (): Funcs is invoked in their own goroutine, asynchronously....//Funcs may also is added to a running Cronc.addfunc ("@dai Ly ", func () {fmt. Println ("Every Day")}): Inspect the Cron job entries ' next and previous run Times.inspect (C.entries ()): C.stop ()//Stop the Scheduler (does not Stop any jobs already running). Cron expression formata cron expression represents a set of times, using 6 space-separated fields. Field name | Mandatory? | Allowed values | Allowed special Characters----------| ---------- | --------------  | --------------------------Seconds | Yes | 0-59 | */,-minutes | Yes | 0-59 | */,-hours | Yes | 0-23 | */,-day of Month | Yes | 1-31 | * / , - ? Month | Yes | 1-12 or Jan-dec | */,-day of week | Yes | 0-6 or Sun-sat | * / , - ?  Note:month and Day-of-week field values is case insensitive. "Sun", "Sun", and "sun" are equally accepted. Special Charactersasterisk (*) The asterisk indicates that the cron expression would match for all values of Thefield; e.g., using a asterisk in the 5th Field (month) would indicate Everymonth. Slash (/) slashes is used to describe increments of ranges. For example 3-59/15 in the1st field (minutes) would indicate the 3rd minute of the hour and every 15minutes thereafter. The form "*\/..." is equivalent to the form "first-last/...", that's, an increment over the largest possible range of the  Field. The form "n/..." is accepted as meaning "n-max/...", that's, starting at N, use theincrement until the end of that Specifi  C range. It does notWrap Around.comma (,) commas is used to separate items of a list. For example, using ' Mon,wed,fri ' inthe 5th field (day of week) would mean Mondays, Wednesdays and Fridays.hyphen (-) Hyph ENS is used to define ranges. For example, 9-17 would indicate everyhour between 9am and 5pm inclusive. Question mark (?) Question mark May is used instead of ' * ' for leaving either Day-of-month Orday-of-week blank. Predefined schedulesyou may use one of the several pre-defined schedules in place of a cron expression. Entry | Description | Equivalent to-----| -----------                                | -------------@yearly (or @annually) | Run once a year, midnight, Jan. 1st | 0 0 0 1 1 * @monthly | Run once a month, midnight, first of month | 0 0 0 1 * * @weekly | Run once a week, midnight on Sunday | 0 0 0 * * 0@daily (or @midnight) | Run once a day, midnight | 0 0 0 * * * @hourly | Run once an hour, beginning of hour | 0 0 * * * *intervalsyou also schedule a job to execute at fixed intervals, starting at the time it's Addedor Cron is R Un. This was supported by formatting the cron spec like this:*************************************************************** @every <duration>where "duration" is a string accepted by time. Parseduration (http://golang.org/pkg/time/#ParseDuration). For example, "@every 1h30m10s" would indicate a schedule the activates Immediately,and then every 1 hour, minutes, 10 Seconds.  Note:the interval does not take the job runtime to account. For example,if a job takes 3 minutes to run, and it was scheduled to run every 5 minutes,it would have only 2 minutes of IDL E time between each run. Time Zonesall interpretation and scheduling is do in the machine's local time zone (asprovided by the Go time package (h Ttp://www.golang.org/pkg/time). Being aware that jobs scheduled during DaylighT-savings leap-ahead Transitions willnot be run! Thread safetysince The Cron service runs concurrently with the calling code, some amount Ofcare must is taken to ensure PR Oper synchronization. All cron methods is designed to being correctly synchronized as long as the callerensures that invocations has a clear happ Ens-before ordering between them.  Implementationcron entries is stored in a array, sorted by their next activation time. Cronsleeps until the next job is due to be run. Upon waking:-it runs each entry that's active on this second-it calculates the next run times for the jobs that were RU N-it re-sorts The array of entries by next activation time.-It goes to sleep until the soonest job.*/
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.