{"modulePath":"github.com/generaltso/gogs","version":"v0.7.33","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package cron\n\nPackage cron implements a cron spec parser and job runner.\n\n### Usage {#hdr-Usage}\n\nCallers may register Funcs to be invoked on a given schedule.  Cron will run them in their own goroutines.\n\n\tc := cron.New()\n\tc.AddFunc(\"Every hour on the half hour\",\"0 30 * * * *\", func() { fmt.Println(\"Every hour on the half hour\") })\n\tc.AddFunc(\"Every hour\",\"@hourly\", func() { fmt.Println(\"Every hour\") })\n\tc.AddFunc(\"Every hour and a half\",\"@every 1h30m\", func() { fmt.Println(\"Every hour thirty\") })\n\tc.Start()\n\t..\n\t// Funcs are invoked in their own goroutine, asynchronously.\n\t...\n\t// Funcs may also be added to a running Cron\n\tc.AddFunc(\"@daily\", func() { fmt.Println(\"Every day\") })\n\t..\n\t// Inspect the cron job entries' next and previous run times.\n\tinspect(c.Entries())\n\t..\n\tc.Stop()  // Stop the scheduler (does not stop any jobs already running).\n\n### CRON Expression Format {#hdr-CRON_Expression_Format}\n\nA cron expression represents a set of times, using 6 space-separated fields.\n\n\tField name   | Mandatory? | Allowed values  | Allowed special characters\n\t----------   | ---------- | --------------  | --------------------------\n\tSeconds      | Yes        | 0-59            | * / , -\n\tMinutes      | Yes        | 0-59            | * / , -\n\tHours        | Yes        | 0-23            | * / , -\n\tDay of month | Yes        | 1-31            | * / , - ?\n\tMonth        | Yes        | 1-12 or JAN-DEC | * / , -\n\tDay of week  | Yes        | 0-6 or SUN-SAT  | * / , - ?\n\nNote: Month and Day-of-week field values are case insensitive.  \"SUN\", \"Sun\", and \"sun\" are equally accepted.\n\n### Special Characters {#hdr-Special_Characters}\n\nAsterisk ( \\* )\n\nThe asterisk indicates that the cron expression will match for all values of the field; e.g., using an asterisk in the 5th field (month) would indicate every month.\n\nSlash ( / )\n\nSlashes are used to describe increments of ranges. For example 3-59/15 in the 1st field (minutes) would indicate the 3rd minute of the hour and every 15 minutes thereafter. The form \"\\*\\\\/...\" is equivalent to the form \"first-last/...\", that is, an increment over the largest possible range of the field.  The form \"N/...\" is accepted as meaning \"N-MAX/...\", that is, starting at N, use the increment until the end of that specific range.  It does not wrap around.\n\nComma ( , )\n\nCommas are used to separate items of a list. For example, using \"MON,WED,FRI\" in the 5th field (day of week) would mean Mondays, Wednesdays and Fridays.\n\nHyphen ( - )\n\nHyphens are used to define ranges. For example, 9-17 would indicate every hour between 9am and 5pm inclusive.\n\nQuestion mark ( ? )\n\nQuestion mark may be used instead of '\\*' for leaving either day-of-month or day-of-week blank.\n\n### Predefined schedules {#hdr-Predefined_schedules}\n\nYou may use one of several pre-defined schedules in place of a cron expression.\n\n\tEntry                  | Description                                | Equivalent To\n\t-----                  | -----------                                | -------------\n\t@yearly (or @annually) | Run once a year, midnight, Jan. 1st        | 0 0 0 1 1 *\n\t@monthly               | Run once a month, midnight, first of month | 0 0 0 1 * *\n\t@weekly                | Run once a week, midnight on Sunday        | 0 0 0 * * 0\n\t@daily (or @midnight)  | Run once a day, midnight                   | 0 0 0 * * *\n\t@hourly                | Run once an hour, beginning of hour        | 0 0 * * * *\n\n### Intervals {#hdr-Intervals}\n\nYou may also schedule a job to execute at fixed intervals.  This is supported by formatting the cron spec like this:\n\n\t@every \u003cduration\u003e\n\nwhere \"duration\" is a string accepted by time.ParseDuration ([http://golang.org/pkg/time/#ParseDuration](http://golang.org/pkg/time/#ParseDuration)).\n\nFor example, \"@every 1h30m10s\" would indicate a schedule that activates every 1 hour, 30 minutes, 10 seconds.\n\nNote: The interval does not take the job runtime into account.  For example, if a job takes 3 minutes to run, and it is scheduled to run every 5 minutes, it will have only 2 minutes of idle time between each run.\n\n### Time zones {#hdr-Time_zones}\n\nAll interpretation and scheduling is done in the machine's local time zone (as provided by the Go time package ([http://www.golang.org/pkg/time](http://www.golang.org/pkg/time)).\n\nBe aware that jobs scheduled during daylight-savings leap-ahead transitions will not be run!\n\n### Thread safety {#hdr-Thread_safety}\n\nSince the Cron service runs concurrently with the calling code, some amount of care must be taken to ensure proper synchronization.\n\nAll cron methods are designed to be correctly synchronized as long as the caller ensures that invocations have a clear happens-before ordering between them.\n\n### Implementation {#hdr-Implementation}\n\nCron entries are stored in an array, sorted by their next activation time.  Cron sleeps until the next job is due to be run.\n\nUpon waking:\n\n  - it runs each entry that is active on that second\n  - it calculates the next run times for the jobs that were run\n  - it re-sorts the array of entries by next activation time.\n  - it goes to sleep until the soonest job.\n\n## Types\n\n```go\ntype ConstantDelaySchedule struct {\n\tDelay time.Duration\n}\n```\nConstantDelaySchedule represents a simple recurring duty cycle, e.g. \"Every 5 minutes\". It does not support jobs more frequent than once a second.\n\n```go\nfunc Every(duration time.Duration) ConstantDelaySchedule\n```\nEvery returns a crontab Schedule that activates once every duration. Delays of less than a second are not supported (will round up to 1 second). Any fields less than a Second are truncated.\n\n```go\nfunc (schedule ConstantDelaySchedule) Next(t time.Time) time.Time\n```\nNext returns the next time this should be run. This rounds so that the next activation time will be on the second.\n\n```go\ntype Cron struct {\n\t// contains filtered or unexported fields\n}\n```\nCron keeps track of any number of entries, invoking the associated func as specified by the schedule. It may be started, stopped, and the entries may be inspected while running.\n\n```go\nfunc New() *Cron\n```\nNew returns a new Cron job runner.\n\n```go\nfunc (c *Cron) AddFunc(desc, spec string, cmd func()) (*Entry, error)\n```\nAddFunc adds a func to the Cron to be run on the given schedule.\n\n```go\nfunc (c *Cron) AddJob(desc, spec string, cmd Job) (*Entry, error)\n```\nAddFunc adds a Job to the Cron to be run on the given schedule.\n\n```go\nfunc (c *Cron) Entries() []*Entry\n```\nEntries returns a snapshot of the cron entries.\n\n```go\nfunc (c *Cron) Schedule(desc, spec string, schedule Schedule, cmd Job) *Entry\n```\nSchedule adds a Job to the Cron to be run on the given schedule.\n\n```go\nfunc (c *Cron) Start()\n```\nStart the cron scheduler in its own go-routine.\n\n```go\nfunc (c *Cron) Stop()\n```\nStop the cron scheduler.\n\n```go\ntype Entry struct {\n\tDescription string\n\tSpec        string\n\n\t// The schedule on which this job should be run.\n\tSchedule Schedule\n\n\t// The next time the job will run. This is the zero time if Cron has not been\n\t// started or this entry's schedule is unsatisfiable\n\tNext time.Time\n\n\t// The last time this job was run. This is the zero time if the job has never\n\t// been run.\n\tPrev time.Time\n\n\t// The Job to run.\n\tJob Job\n\n\tExecTimes int // Execute times count.\n}\n```\nEntry consists of a schedule and the func to execute on that schedule.\n\n```go\ntype FuncJob func()\n```\nA wrapper that turns a func() into a cron.Job\n\n```go\nfunc (f FuncJob) Run()\n```\n\n```go\ntype Job interface {\n\tRun()\n}\n```\nJob is an interface for submitted cron jobs.\n\n```go\ntype Schedule interface {\n\t// Return the next activation time, later than the given time.\n\t// Next is invoked initially, and then each time the job is run.\n\tNext(time.Time) time.Time\n}\n```\nThe Schedule describes a job's duty cycle.\n\n```go\nfunc Parse(spec string) (_ Schedule, err error)\n```\nParse returns a new crontab schedule representing the given spec. It returns a descriptive error if the spec is not valid.\n\nIt accepts\n\n  - Full crontab specs, e.g. \"\\* \\* \\* \\* \\* ?\"\n  - Descriptors, e.g. \"@midnight\", \"@every 1h30m\"\n\n```go\ntype SpecSchedule struct {\n\tSecond, Minute, Hour, Dom, Month, Dow uint64\n}\n```\nSpecSchedule specifies a duty cycle (to the second granularity), based on a traditional crontab specification. It is computed initially and stored as bit sets.\n\n```go\nfunc (s *SpecSchedule) Next(t time.Time) time.Time\n```\nNext returns the next time this schedule is activated, greater than the given time.  If no time can be found to satisfy the schedule, return the zero time.\n\n","path":"github.com/generaltso/gogs/modules/cron","name":"cron","synopsis":"Package cron implements a cron spec parser and job runner.","isRedistributable":true}
