Documentation
¶
Overview ¶
Package gocron : A Golang Job Scheduling Package.
Note from current maintainers:
A currently maintained fork of this project has been migrated to https://github.com/go-co-op/gocron
Disclaimer: we (the maintainers) tried, with no luck, to get in contact with Jason (the repository owner) in order to add new maintainers or leave the project within an organization. Unfortunately, he hasn't replied for months now (March, 2020).
So, we decided to move the project to a new repository (as stated above), in order to keep the evolution of the project coming from as many people as possible. Feel free to reach over!
An in-process scheduler for periodic jobs that uses the builder pattern for configuration. Schedule lets you run Golang functions periodically at pre-determined intervals using a simple, human-friendly syntax.
Inspired by the Ruby module clockwork <https://github.com/tomykaira/clockwork> and Python package schedule <https://github.com/dbader/schedule>
See also http://adam.heroku.com/past/2010/4/13/rethinking_cron/ http://adam.heroku.com/past/2010/6/30/replace_cron_with_clockwork/
Copyright 2014 Jason Lyu. jasonlvhit@gmail.com . Copyright 2025 Su Yang. soulteary@gmail.com . All rights reserved. Use of this source code is governed by a BSD-style . license that can be found in the LICENSE file.
Index ¶
- Constants
- Variables
- func ChangeLoc(newLocation *time.Location)
- func Clear()
- func NextTick() *time.Time
- func Remove(j interface{})
- func RunAll()
- func RunAllwithDelay(d int)
- func RunPending()
- func Scheduled(j interface{}) bool
- func SetLocker(l Locker)
- func Start() chan bool
- type Job
- func (j *Job) At(t string) *Job
- func (j *Job) Day() *Job
- func (j *Job) Days() *Job
- func (j *Job) Do(jobFun interface{}, params ...interface{}) error
- func (j *Job) DoSafely(jobFun interface{}, params ...interface{}) errordeprecated
- func (j *Job) Err() error
- func (j *Job) Friday() *Job
- func (j *Job) From(t *time.Time) *Job
- func (j *Job) GetAt() string
- func (j *Job) GetWeekday() time.Weekday
- func (j *Job) Hour() *Job
- func (j *Job) Hours() *Job
- func (j *Job) Loc(loc *time.Location) *Job
- func (j *Job) Lock() *Job
- func (j *Job) Minute() *Job
- func (j *Job) Minutes() *Job
- func (j *Job) Monday() (job *Job)
- func (j *Job) NextScheduledTime() time.Time
- func (j *Job) Saturday() *Job
- func (j *Job) Second() *Job
- func (j *Job) Seconds() *Job
- func (j *Job) Sunday() *Job
- func (j *Job) Tag(t string, others ...string)
- func (j *Job) Tags() []string
- func (j *Job) Thursday() *Job
- func (j *Job) Tuesday() *Job
- func (j *Job) Untag(t string)
- func (j *Job) Wednesday() *Job
- func (j *Job) Week() *Job
- func (j *Job) Weekday(startDay time.Weekday) *Job
- func (j *Job) Weeks() *Job
- func (j *Job) WithContext(ctx context.Context) *Job
- func (j *Job) WithTimeout(timeout time.Duration) *Job
- type Locker
- type Scheduler
- func (s *Scheduler) ChangeLoc(newLocation *time.Location)
- func (s *Scheduler) Clear()
- func (s *Scheduler) Every(interval uint64) *Job
- func (s *Scheduler) Jobs() []*Job
- func (s *Scheduler) Len() int
- func (s *Scheduler) Less(i, j int) bool
- func (s *Scheduler) NextRun() (*Job, time.Time)
- func (s *Scheduler) Remove(j interface{})
- func (s *Scheduler) RemoveByRef(j *Job)
- func (s *Scheduler) RemoveByTag(t string)
- func (s *Scheduler) RunAll()
- func (s *Scheduler) RunAllwithDelay(d int)
- func (s *Scheduler) RunPending()
- func (s *Scheduler) Scheduled(j interface{}) bool
- func (s *Scheduler) Start() chan bool
- func (s *Scheduler) StartWithContext(ctx context.Context) chan bool
- func (s *Scheduler) Swap(i, j int)
Constants ¶
const MAX_JOB_NUM = 10000
MAX_JOB_NUM max number of jobs, hack it if you need.
Variables ¶
var ( // ErrTimeFormat time format error ErrTimeFormat = errors.New("time format error") // ErrParamsNotAdapted parameter count mismatch ErrParamsNotAdapted = errors.New("the number of params is not adapted") // ErrNotAFunction not a function error, only functions can be scheduled into job queue ErrNotAFunction = errors.New("only functions can be schedule into the job queue") // ErrPeriodNotSpecified job period not specified error ErrPeriodNotSpecified = errors.New("unspecified job period") // ErrParameterCannotBeNil parameter cannot be nil error ErrParameterCannotBeNil = errors.New("nil parameters cannot be used with reflection") // ErrJobTimeout job execution timeout error ErrJobTimeout = errors.New("job execution timeout") // ErrJobCancelled job execution cancelled error ErrJobCancelled = errors.New("job execution cancelled") )
Functions ¶
func RunAllwithDelay ¶
func RunAllwithDelay(d int)
RunAllwithDelay run all the jobs with a delay in seconds
A delay of `delay` seconds is added between each job. This can help to distribute the system load generated by the jobs more evenly over time.
func RunPending ¶
func RunPending()
RunPending run all jobs that are scheduled to run
Please note that it is *intended behavior that run_pending() does not run missed jobs*. For example, if you've registered a job that should run every minute and you only call run_pending() in one hour increments then your job won't be run 60 times in between but only once.
Types ¶
type Job ¶
type Job struct {
// contains filtered or unexported fields
}
Job struct keeping information about job
func (*Job) At ¶
At schedules job at specific time of day
s.Every(1).Day().At("10:30:01").Do(task)
s.Every(1).Monday().At("10:30:01").Do(task)
func (*Job) DoSafely
deprecated
func (*Job) GetAt ¶
GetAt returns the specific time of day the job will run at
s.Every(1).Day().At("10:30").GetAt() == "10:30"
func (*Job) GetWeekday ¶
GetWeekday returns which day of the week the job will run on This should only be used when .Weekday(...) was called on the job.
func (*Job) Loc ¶
Loc sets the location for which to interpret "At"
s.Every(1).Day().At("10:30").Loc(time.UTC).Do(task)
func (*Job) NextScheduledTime ¶
NextScheduledTime returns the time of when this job is to run next
func (*Job) Tag ¶
Tag allows you to add labels to a job they don't impact the functionality of the job.
func (*Job) WithContext ¶
WithContext sets the context for job execution If the job function accepts context.Context as its first parameter, it will be passed automatically. Otherwise, the context is used for cancellation and timeout control.
scheduler.Every(1).Minute().WithContext(ctx).Do(task)
type Locker ¶
Locker provides a method to lock jobs from running at the same time on multiple instances of gocron. You can provide any locker implementation you wish.
type Scheduler ¶
type Scheduler struct {
// contains filtered or unexported fields
}
Scheduler struct, the only data member is the list of jobs. - implements the sort.Interface{} for sorting jobs, by the time nextRun
func NewSchedulerWithContext ¶
NewSchedulerWithContext creates a new scheduler with a context
func (*Scheduler) Remove ¶
func (s *Scheduler) Remove(j interface{})
Remove specific job j by function
func (*Scheduler) RemoveByRef ¶
RemoveByRef removes specific job j by reference
func (*Scheduler) RemoveByTag ¶
RemoveByTag removes specific job j by tag
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 delay seconds
func (*Scheduler) RunPending ¶
func (s *Scheduler) RunPending()
RunPending runs all the jobs that are scheduled to run.
func (*Scheduler) Start ¶
Start all the pending jobs Add seconds ticker Returns a channel that can be closed to stop the scheduler
func (*Scheduler) StartWithContext ¶
StartWithContext starts the scheduler with a context The scheduler will stop when the context is cancelled or the returned channel is closed