Documentation ¶
Overview ¶
Package gocron : A Golang Job Scheduling Package.
An in-process scheduler for periodic jobs that uses the builder pattern for configuration. gocron lets you run Golang functions periodically at pre-determined intervals using a simple, human-friendly syntax.
Index ¶
- Constants
- Variables
- type Job
- func (j *Job) Error() error
- func (j *Job) IsRunning() bool
- func (j *Job) LastRun() time.Time
- func (j *Job) LimitRunsTo(n int)
- func (j *Job) NextRun() time.Time
- func (j *Job) RunCount() int
- func (j *Job) ScheduledAtTime() string
- func (j *Job) ScheduledAtTimes() []string
- func (j *Job) ScheduledTime() time.Time
- func (j *Job) SingletonMode()
- func (j *Job) Tag(tags ...string)
- func (j *Job) Tags() []string
- func (j *Job) Untag(t string)
- func (j *Job) Weekday() (time.Weekday, error)
- func (j *Job) Weekdays() []time.Weekday
- type Scheduler
- func (s *Scheduler) At(i interface{}) *Scheduler
- func (s *Scheduler) ChangeLocation(newLocation *time.Location)
- func (s *Scheduler) Clear()
- func (s *Scheduler) Cron(cronExpression string) *Scheduler
- func (s *Scheduler) CronWithSeconds(cronExpression string) *Scheduler
- func (s *Scheduler) Day() *Scheduler
- func (s *Scheduler) Days() *Scheduler
- func (s *Scheduler) Do(jobFun interface{}, params ...interface{}) (*Job, error)
- func (s *Scheduler) Every(interval interface{}) *Scheduler
- func (s *Scheduler) FindJobsByTag(tags ...string) ([]*Job, error)
- func (s *Scheduler) Friday() *Scheduler
- func (s *Scheduler) Hour() *Scheduler
- func (s *Scheduler) Hours() *Scheduler
- func (s *Scheduler) IsRunning() bool
- func (s *Scheduler) Job(j *Job) *Scheduler
- func (s *Scheduler) Jobs() []*Job
- func (s *Scheduler) Len() int
- func (s *Scheduler) Less(first, second int) bool
- func (s *Scheduler) LimitRunsTo(i int) *Scheduler
- func (s *Scheduler) Location() *time.Location
- func (s *Scheduler) Midday() *Scheduler
- func (s *Scheduler) Millisecond() *Scheduler
- func (s *Scheduler) Milliseconds() *Scheduler
- func (s *Scheduler) Minute() *Scheduler
- func (s *Scheduler) Minutes() *Scheduler
- func (s *Scheduler) Monday() *Scheduler
- func (s *Scheduler) Month(daysOfMonth ...int) *Scheduler
- func (s *Scheduler) MonthFirstWeekday(weekday time.Weekday) *Scheduler
- func (s *Scheduler) MonthLastDay() *Scheduler
- func (s *Scheduler) Months(daysOfTheMonth ...int) *Scheduler
- func (s *Scheduler) NextRun() (*Job, time.Time)
- func (s *Scheduler) Remove(job interface{})
- func (s *Scheduler) RemoveByReference(job *Job)
- func (s *Scheduler) RemoveByTag(tag string) error
- func (s *Scheduler) RemoveByTags(tags ...string) error
- func (s *Scheduler) RemoveByTagsAny(tags ...string) error
- func (s *Scheduler) RunAll()
- func (s *Scheduler) RunAllWithDelay(d time.Duration)
- func (s *Scheduler) RunByTag(tag string) error
- func (s *Scheduler) RunByTagWithDelay(tag string, d time.Duration) error
- func (s *Scheduler) Saturday() *Scheduler
- func (s *Scheduler) Second() *Scheduler
- func (s *Scheduler) Seconds() *Scheduler
- func (s *Scheduler) SetMaxConcurrentJobs(n int, mode limitMode)
- func (s *Scheduler) SingletonMode() *Scheduler
- func (s *Scheduler) SingletonModeAll()
- func (s *Scheduler) StartAsync()
- func (s *Scheduler) StartAt(t time.Time) *Scheduler
- func (s *Scheduler) StartBlocking()
- func (s *Scheduler) StartImmediately() *Scheduler
- func (s *Scheduler) Stop()
- func (s *Scheduler) Sunday() *Scheduler
- func (s *Scheduler) Swap(i, j int)
- func (s *Scheduler) Tag(t ...string) *Scheduler
- func (s *Scheduler) TagsUnique()
- func (s *Scheduler) TaskPresent(j interface{}) bool
- func (s *Scheduler) Thursday() *Scheduler
- func (s *Scheduler) Tuesday() *Scheduler
- func (s *Scheduler) Update() (*Job, error)
- func (s *Scheduler) WaitForSchedule() *Scheduler
- func (s *Scheduler) WaitForScheduleAll()
- func (s *Scheduler) Wednesday() *Scheduler
- func (s *Scheduler) Week() *Scheduler
- func (s *Scheduler) Weekday(weekDay time.Weekday) *Scheduler
- func (s *Scheduler) Weeks() *Scheduler
Constants ¶
const ( // RescheduleMode - the default is that if a limit on maximum // concurrent jobs is set and the limit is reached, a job will // skip it's run and try again on the next occurrence in the schedule RescheduleMode limitMode = iota // WaitMode - if a limit on maximum concurrent jobs is set // and the limit is reached, a job will wait to try and run // until a spot in the limit is freed up. // // Note: this mode can produce unpredictable results as // job execution order isn't guaranteed. For example, a job that // executes frequently may pile up in the wait queue and be executed // many times back to back when the queue opens. WaitMode )
Variables ¶
var ( ErrNotAFunction = errors.New("only functions can be scheduled into the job queue") ErrNotScheduledWeekday = errors.New("job not scheduled weekly on a weekday") ErrJobNotFoundWithTag = errors.New("no jobs found with given tag") ErrUnsupportedTimeFormat = errors.New("the given time format is not supported") ErrInvalidInterval = errors.New(".Every() interval must be greater than 0") ErrInvalidIntervalType = errors.New(".Every() interval must be int, time.Duration, or string") ErrInvalidIntervalUnitsSelection = errors.New(".Every(time.Duration) and .Cron() cannot be used with units (e.g. .Seconds())") ErrAtTimeNotSupported = errors.New("the At() method is not supported for this time unit") ErrWeekdayNotSupported = errors.New("weekday is not supported for time unit") ErrInvalidDayOfMonthEntry = errors.New("only days 1 through 28 are allowed for monthly schedules") ErrTagsUnique = func(tag string) error { return fmt.Errorf("a non-unique tag was set on the job: %s", tag) } ErrWrongParams = errors.New("wrong list of params") ErrUpdateCalledWithoutJob = errors.New("a call to Scheduler.Update() requires a call to Scheduler.Job() first") ErrCronParseFailure = errors.New("cron expression failed to be parsed") ErrInvalidDaysOfMonthDuplicateValue = errors.New("duplicate days of month is not allowed in Month() and Months() methods") )
Error declarations for gocron related errors
Functions ¶
This section is empty.
Types ¶
type Job ¶
type Job struct {
// contains filtered or unexported fields
}
Job struct stores the information necessary to run a Job
func (*Job) Error ¶
Error returns an error if one occurred while creating the Job. If multiple errors occurred, they will be wrapped and can be checked using the standard unwrap options.
func (*Job) IsRunning ¶
IsRunning reports whether any instances of the job function are currently running
func (*Job) LimitRunsTo ¶
LimitRunsTo limits the number of executions of this job to n. Upon reaching the limit, the job is removed from the scheduler.
Note: If a job is added to a running scheduler and this method is then used you may see the job run more than the set limit as job is scheduled immediately by default upon being added to the scheduler. It is recommended to use the LimitRunsTo() func on the scheduler chain when scheduling the job. For example: scheduler.LimitRunsTo(1).Do()
func (*Job) ScheduledAtTime ¶
ScheduledAtTime returns the specific time of day the Job will run at. If multiple times are set, the earliest time will be returned.
func (*Job) ScheduledAtTimes ¶
ScheduledAtTimes returns the specific times of day the Job will run at
func (*Job) ScheduledTime ¶
ScheduledTime returns the time of the Job's next scheduled run
func (*Job) SingletonMode ¶
func (j *Job) SingletonMode()
SingletonMode prevents a new job from starting if the prior job has not yet completed it's run Note: If a job is added to a running scheduler and this method is then used you may see the job run overrun itself as job is scheduled immediately by default upon being added to the scheduler. It is recommended to use the SingletonMode() func on the scheduler chain when scheduling the job.
func (*Job) Tag ¶
Tag allows you to add arbitrary labels to a Job that do not impact the functionality of the Job
type Scheduler ¶
type Scheduler struct {
// contains filtered or unexported fields
}
Scheduler struct stores a list of Jobs and the location of time used by the Scheduler, and implements the sort.Interface{} for sorting Jobs, by the time of nextRun
func NewScheduler ¶
NewScheduler creates a new Scheduler
func (*Scheduler) At ¶
At schedules the Job at a specific time of day in the form "HH:MM:SS" or "HH:MM" or time.Time (note that only the hours, minutes, seconds and nanos are used).
func (*Scheduler) ChangeLocation ¶
ChangeLocation changes the default time location
func (*Scheduler) CronWithSeconds ¶
func (*Scheduler) Every ¶
Every schedules a new periodic Job with an interval. Interval can be an int, time.Duration or a string that parses with time.ParseDuration(). Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".
func (*Scheduler) FindJobsByTag ¶
FindJobsByTag will return a slice of Jobs that match all given tags
func (*Scheduler) Job ¶
Job puts the provided job in focus for the purpose of making changes to the job with the scheduler chain and finalized by calling Update()
func (*Scheduler) Less ¶
Less compares the next run of jobs based on their index. Returns true if the second job is after the first.
func (*Scheduler) LimitRunsTo ¶
LimitRunsTo limits the number of executions of this job to n. Upon reaching the limit, the job is removed from the scheduler.
func (*Scheduler) Millisecond ¶
Millisecond sets the unit with seconds
func (*Scheduler) Milliseconds ¶
Milliseconds sets the unit with seconds
func (*Scheduler) MonthFirstWeekday ¶
MonthFirstWeekday sets the job to run the first specified weekday of the month
func (*Scheduler) MonthLastDay ¶
MonthLastDay sets the unit with months at every last day of the month
func (*Scheduler) Months ¶
Months sets the unit with months Note: Only days 1 through 28 are allowed for monthly schedules Note: Multiple add same days of month cannot be allowed Note: -1 is a special value and can only occur as single argument
func (*Scheduler) Remove ¶
func (s *Scheduler) Remove(job interface{})
Remove specific Job by function
Removing a job stops that job's timer. However, if a job has already been started by by the job's timer before being removed, there is no way to stop it through gocron as https://pkg.go.dev/time#Timer.Stop explains. The job function would need to have implemented a means of stopping, e.g. using a context.WithCancel().
func (*Scheduler) RemoveByReference ¶
RemoveByReference removes specific Job by reference
func (*Scheduler) RemoveByTag ¶
RemoveByTag will remove Jobs that match the given tag.
func (*Scheduler) RemoveByTags ¶
RemoveByTags will remove Jobs that match all given tags.
func (*Scheduler) RemoveByTagsAny ¶
RemoveByTagsAny will remove Jobs that match any one of the given tags.
func (*Scheduler) RunAll ¶
func (s *Scheduler) RunAll()
RunAll run all Jobs regardless if they are scheduled to run or not
func (*Scheduler) RunAllWithDelay ¶
RunAllWithDelay runs all jobs with the provided delay in between each job
func (*Scheduler) RunByTag ¶
RunByTag runs all the jobs containing a specific tag regardless of whether they are scheduled to run or not
func (*Scheduler) RunByTagWithDelay ¶
RunByTagWithDelay is same as RunByTag but introduces a delay between each job execution
func (*Scheduler) SetMaxConcurrentJobs ¶
SetMaxConcurrentJobs limits how many jobs can be running at the same time. This is useful when running resource intensive jobs and a precise start time is not critical.
func (*Scheduler) SingletonMode ¶
SingletonMode prevents a new job from starting if the prior job has not yet completed its run
func (*Scheduler) SingletonModeAll ¶
func (s *Scheduler) SingletonModeAll()
SingletonModeAll prevents new jobs from starting if the prior instance of the particular job has not yet completed its run
func (*Scheduler) StartAsync ¶
func (s *Scheduler) StartAsync()
StartAsync starts all jobs without blocking the current thread
func (*Scheduler) StartAt ¶
StartAt schedules the next run of the Job. If this time is in the past, the configured interval will be used to calculate the next future time
func (*Scheduler) StartBlocking ¶
func (s *Scheduler) StartBlocking()
StartBlocking starts all jobs and blocks the current thread
func (*Scheduler) StartImmediately ¶
StartImmediately sets the job to run immediately upon starting the scheduler or adding the job to a running scheduler. This overrides the jobs start status of any previously called methods in the chain.
Note: This is the default behavior of the scheduler for most jobs, but is useful for overriding the default behavior of Cron scheduled jobs which default to WaitForSchedule.
func (*Scheduler) Stop ¶
func (s *Scheduler) Stop()
Stop stops the scheduler. This is a no-op if the scheduler is already stopped. It waits for all running jobs to finish before returning, so it is safe to assume that running jobs will finish when calling this.
func (*Scheduler) Swap ¶
Swap places each job into the other job's position given the provided job indexes.
func (*Scheduler) TagsUnique ¶
func (s *Scheduler) TagsUnique()
TagsUnique forces job tags to be unique across the scheduler when adding tags with (s *Scheduler) Tag(). This does not enforce uniqueness on tags added via (j *Job) Tag()
func (*Scheduler) TaskPresent ¶
TaskPresent checks if specific job's function was added to the scheduler.
func (*Scheduler) Update ¶
Update stops the job (if running) and starts it with any updates that were made to the job in the scheduler chain. Job() must be called first to put the given job in focus.
func (*Scheduler) WaitForSchedule ¶
WaitForSchedule sets the job to not start immediately but rather wait until the first scheduled interval.
func (*Scheduler) WaitForScheduleAll ¶
func (s *Scheduler) WaitForScheduleAll()
WaitForScheduleAll defaults the scheduler to create all new jobs with the WaitForSchedule option as true. The jobs will not start immediately but rather will wait until their first scheduled interval.