Documentation
¶
Overview ¶
Package gocron : A Golang Job Scheduling Package.
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/
maintained by Mark Salpeter mark@dealyze.com
Copyright 2014 Jason Lyu. jasonlvhit@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 Clear()
- func IsRunning() bool
- func Remove(j *Job)
- func RunAll()
- func RunAllWithDelay(d time.Duration)
- func RunPending()
- func Start()
- func Stop()
- type Job
- func (j *Job) At(t string) *Job
- func (j *Job) Day() *Job
- func (j *Job) Days() *Job
- func (j *Job) Do(task interface{}, params ...interface{}) *Job
- func (j *Job) Friday() *Job
- func (j *Job) Hour() *Job
- func (j *Job) Hours() *Job
- func (j *Job) Location(loc *time.Location) *Job
- func (j *Job) Minute() *Job
- func (j *Job) Minutes() *Job
- func (j *Job) Monday() *Job
- func (j *Job) Saturday() *Job
- func (j *Job) Second() *Job
- func (j *Job) Seconds() *Job
- func (j *Job) Sunday() *Job
- func (j *Job) Thursday() *Job
- func (j *Job) Tuesday() *Job
- func (j *Job) Wednesday() *Job
- func (j *Job) Week() *Job
- func (j *Job) Weekday(weekday time.Weekday) *Job
- func (j *Job) Weeks() *Job
- type Scheduler
Constants ¶
const ( // Day is the duration for a Days worth of time Day = 24 * time.Hour // Week is the duration for a Weeks worth of time Week = 7 * Day )
Variables ¶
var ( // ErrTaskIsNotAFuncError is the error panicked when a task passed to `Job.Do` ErrTaskIsNotAFuncError = errors.New("the `task` your a scheduling must be of type func") // ErrMissmatchedTaskParams is the error panicked when someone passes too many or too few params to `Job.Do` ErrMissmatchedTaskParams = errors.New("the `task` your a scheduling must be of type func") // ErrJobIsNotInitialized is the error panicked when a job is scheduled that was not initialized ErrJobIsNotInitialized = errors.New("this job was not intialized") // ErrIncorrectTimeFormat is the error panicked when `At` is passed an incorrect time ErrIncorrectTimeFormat = errors.New("the time format is incorrect") // ErrIntervalNotValid error panicked when the interval is not valid ErrIntervalNotValid = errors.New("the interval must be greater than 0") )
Functions ¶
func RunAll ¶
func RunAll()
RunAll runs all jobs of the regardless if they are scheduled to run or not.
func RunAllWithDelay ¶
RunAllWithDelay runs all of the jobs with a delay between each of them
This can help to distribute the system load generated by the jobs more evenly over time.
func RunPending ¶
func RunPending()
RunAll Runs all of the jobs that are scheduled to run
Please note that it is *intended behavior that `RunPending()` does not run missed jobs*. For example, if you've registered a job that should run every minute and you only call `RunPending()` in one hour increments then your job will only be run once every hour
Types ¶
type Job ¶
type Job struct {
// contains filtered or unexported fields
}
Job calculates the time intervals in which a task should be executed.
func (*Job) At ¶
At adds a time component to daily or weekly recurring tasks.
note: if no time is specified, the `At` time will default to whenever `Schedule.Start()` is called
Example
// ... Every(1).Day().At("10:30").Do(task) // performs a task every day at 10:30 am Every(1).Monday().At("22:30").Do(task) // performs a task every Monday at 10:30 pm Every(1).Monday().Do(task) // performs a task every Monday at whatever time `Schedule.Start()` is called
func (*Job) Days ¶
Days sets a task to run every `x` number of hours
Example
// ... Every(5).Days().Do(task) // executes the task func every 5 days
func (*Job) Do ¶
Do specifies the taks that should be called executed and the parameters it should be passed Example
// ... job := Every(1).Day().At("10:30").Do(task, paramOne, "paramTwo") // performs `task(paramOne, "paramTwo")` every day at 10:30 am job.Do(task2, paramThree, "paramFour") // `task2(paramThree, "paramFour")` will perperformed at the same interval
func (*Job) Hours ¶
Hours sets a task to run every `x` number of hours
Example
// ... Every(5).Hours().Do(task) // executes the task func every 5 hours
func (*Job) Location ¶
Location sets the timezone of the job. Jobs created by `NewJob(...)` have a default location of`time.Local`. Jobs created by `Scheduler.Every(...)` have a default timezone of whatever `Scheduler.Location(...)` is set to.
Example
// ... est, err := time.LoadLocation("America/New_York") if err != nil { // you probably haven't set up your server correctly ;) panic(err) } Every(2).Monday().At("05:00").Location(est).Do(task) // executes the task every monday at 5:00 am eastern standard time
func (*Job) Minutes ¶
Minutes sets a job to run every `x` number of minutes
Example
// ... Every(5).Minutes().Do(task) // executes the task func every 5 minutes
func (*Job) Seconds ¶
Seconds sets a job to run every `x` number of seconds
Example
// ... Every(5).Seconds().Do(task) // executes the task func every 5 seconds
func (*Job) Weekday ¶
Weekday sets the task to be performed on a certian day of the week
Example
// ... scheduler.Every(1).Weekday(time.Sunday).Do(task) // executes the task once every Sunday at whatever time `Scheduler.Start()` is called scheduler.Every(2).Weekday(time.Monday).At("05:00").Do(task) // executes the task every other Monday at 7 am
type Scheduler ¶
type Scheduler interface { // Start starts the scheduler Start() // IsRunning returns true if the job has started IsRunning() bool // Stop stops the scheduler from executing jobs Stop() // Every creates a new job, and adds it to the `Scheduler` Every(interval uint64) *Job // Remove removes an individual job from the scheduler. It returns true if the job was found and removed from the `Scheduler` Remove(*Job) bool // Clear removes all of the jobs that have been added to the scheduler Clear() // Location sets the default location of every job created with `Every`. // The default location is `time.Local` Location(*time.Location) // NextRun returns the next next job to be run and the time in which it will be run NextRun() (*Job, time.Time) // RunAll runs all of the jobs regardless of wether or not they are pending RunAll() // RunPending runs all of the pending jobs RunPending() // RunAllWithDelay runs all of the jobs regardless of wether or not they are pending with a delay RunAllWithDelay(time.Duration) //Add job to the scheduler Add(*Job) bool }
Scheduler keeps a slice of jobs that it executes at a regular interval
func NewScheduler ¶
func NewScheduler() Scheduler
NewScheduler create a new scheduler. Note: the current implementation is not concurrency safe.