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.
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 ¶
- Variables
- type Job
- func (j *Job) Err() error
- func (j *Job) LastRun() time.Time
- func (j *Job) LimitRunsTo(n int)
- func (j *Job) NextRun() time.Time
- func (j *Job) RemoveAfterLastRun() *Job
- func (j *Job) RunCount() int
- func (j *Job) ScheduledAtTime() string
- func (j *Job) ScheduledTime() time.Time
- func (j *Job) Tag(t string, others ...string)
- func (j *Job) Tags() []string
- func (j *Job) Untag(t string)
- func (j *Job) Weekday() (time.Weekday, error)
- type Scheduler
- func (s *Scheduler) At(t string) *Scheduler
- func (s *Scheduler) ChangeLocation(newLocation *time.Location)
- func (s *Scheduler) Clear()
- 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 uint64) *Scheduler
- func (s *Scheduler) Friday() *Scheduler
- func (s *Scheduler) Hour() *Scheduler
- func (s *Scheduler) Hours() *Scheduler
- func (s *Scheduler) IsRunning() bool
- func (s *Scheduler) Jobs() []*Job
- func (s *Scheduler) Len() int
- func (s *Scheduler) Less(i, j int) bool
- func (s *Scheduler) Location() *time.Location
- func (s *Scheduler) Minute() *Scheduler
- func (s *Scheduler) Minutes() *Scheduler
- func (s *Scheduler) Monday() *Scheduler
- func (s *Scheduler) Month(dayOfTheMonth int) *Scheduler
- func (s *Scheduler) Months(dayOfTheMonth int) *Scheduler
- func (s *Scheduler) NextRun() (*Job, time.Time)
- func (s *Scheduler) Remove(j interface{})
- func (s *Scheduler) RemoveByReference(j *Job)
- func (s *Scheduler) RemoveJobByTag(tag string) error
- func (s *Scheduler) RunAll()
- func (s *Scheduler) RunAllWithDelay(d int)
- func (s *Scheduler) Saturday() *Scheduler
- func (s *Scheduler) Scheduled(j interface{}) bool
- func (s *Scheduler) Second() *Scheduler
- func (s *Scheduler) Seconds() *Scheduler
- func (s *Scheduler) SetTag(t []string) *Scheduler
- func (s *Scheduler) StartAsync() chan struct{}
- func (s *Scheduler) StartAt(t time.Time) *Scheduler
- func (s *Scheduler) StartBlocking()
- func (s *Scheduler) Stop()
- func (s *Scheduler) Sunday() *Scheduler
- func (s *Scheduler) Swap(i, j int)
- func (s *Scheduler) Thursday() *Scheduler
- func (s *Scheduler) Tuesday() *Scheduler
- func (s *Scheduler) Wednesday() *Scheduler
- func (s *Scheduler) Week() *Scheduler
- func (s *Scheduler) Weekday(startDay time.Weekday) *Scheduler
- func (s *Scheduler) Weeks() *Scheduler
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( ErrTimeFormat = errors.New("time format error") ErrParamsNotAdapted = errors.New("the number of params is not adapted") ErrNotAFunction = errors.New("only functions can be schedule into the job queue") ErrPeriodNotSpecified = errors.New("unspecified job period") 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") )
Error declarations for gocron related errors
Functions ¶
This section is empty.
Types ¶
type Job ¶
Job struct stores the information necessary to run a Job
func (*Job) LastRun ¶
LastRun returns the time the job was run last
Example ¶
package main import ( "fmt" "time" "github.com/go-co-op/gocron" ) var task = func() { fmt.Println("I am a task") } func main() { s := gocron.NewScheduler(time.UTC) job, _ := s.Every(1).Second().Do(task) go func() { for { fmt.Println("Last run", job.LastRun()) time.Sleep(time.Second) } }() <-s.StartAsync() }
Output:
func (*Job) LimitRunsTo ¶
LimitRunsTo limits the number of executions of this job to n. However, the job will still remain in the scheduler
Example ¶
package main import ( "fmt" "time" "github.com/go-co-op/gocron" ) var task = func() { fmt.Println("I am a task") } func main() { s := gocron.NewScheduler(time.UTC) job, _ := s.Every(1).Second().Do(task) job.LimitRunsTo(2) s.StartAsync() }
Output:
func (*Job) NextRun ¶
NextRun returns the time the job will run next
Example ¶
package main import ( "fmt" "time" "github.com/go-co-op/gocron" ) var task = func() { fmt.Println("I am a task") } func main() { s := gocron.NewScheduler(time.UTC) job, _ := s.Every(1).Second().Do(task) go func() { for { fmt.Println("Next run", job.NextRun()) time.Sleep(time.Second) } }() <-s.StartAsync() }
Output:
func (*Job) RemoveAfterLastRun ¶
RemoveAfterLastRun update the job in order to remove the job after its last exec
Example ¶
package main import ( "fmt" "time" "github.com/go-co-op/gocron" ) var task = func() { fmt.Println("I am a task") } func main() { s := gocron.NewScheduler(time.UTC) job, _ := s.Every(1).Second().Do(task) job.LimitRunsTo(1) job.RemoveAfterLastRun() s.StartAsync() }
Output:
func (*Job) RunCount ¶
RunCount returns the number of time the job ran so far
Example ¶
package main import ( "fmt" "time" "github.com/go-co-op/gocron" ) var task = func() { fmt.Println("I am a task") } func main() { s := gocron.NewScheduler(time.UTC) job, _ := s.Every(1).Second().Do(task) go func() { for { fmt.Println("Run count", job.RunCount()) time.Sleep(time.Second) } }() <-s.StartAsync() }
Output:
func (*Job) ScheduledAtTime ¶
ScheduledAtTime returns the specific time of day the Job will run at
func (*Job) ScheduledTime ¶
ScheduledTime returns the time of the Job's next scheduled run
Example ¶
package main import ( "fmt" "time" "github.com/go-co-op/gocron" ) var task = func() { fmt.Println("I am a task") } func main() { s := gocron.NewScheduler(time.UTC) job, _ := s.Every(1).Day().At("10:30").Do(task) fmt.Println(job.ScheduledAtTime()) }
Output: 10:30
type Scheduler ¶
type Scheduler struct {
// contains filtered or unexported fields
}
Scheduler struct stores a list of Jobs and the location of time Scheduler Scheduler 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"
Example ¶
package main import ( "fmt" "time" "github.com/go-co-op/gocron" ) var task = func() { fmt.Println("I am a task") } func main() { s := gocron.NewScheduler(time.UTC) _, _ = s.Every(1).Day().At("10:30").Do(task) _, _ = s.Every(1).Monday().At("10:30:01").Do(task) }
Output:
func (*Scheduler) ChangeLocation ¶
ChangeLocation changes the default time location
Example ¶
package main import ( "fmt" "time" "github.com/go-co-op/gocron" ) func main() { s := gocron.NewScheduler(time.UTC) fmt.Println(s.Location()) location, err := time.LoadLocation("America/Los_Angeles") if err != nil { panic(err) } s.ChangeLocation(location) fmt.Println(s.Location()) }
Output: UTC America/Los_Angeles
func (*Scheduler) Clear ¶
func (s *Scheduler) Clear()
Clear clear all Jobs from this scheduler
Example ¶
package main import ( "fmt" "time" "github.com/go-co-op/gocron" ) var task = func() { fmt.Println("I am a task") } func main() { s := gocron.NewScheduler(time.UTC) _, _ = s.Every(1).Second().Do(task) _, _ = s.Every(1).Minute().Do(task) _, _ = s.Every(1).Month(1).Do(task) fmt.Println(len(s.Jobs())) // Print the number of jobs before clearing s.Clear() // Clear all the jobs fmt.Println(len(s.Jobs())) // Print the number of jobs after clearing s.StartAsync() }
Output: 3 0
func (*Scheduler) Location ¶
Location provides the current location set on the scheduler
Example ¶
package main import ( "fmt" "time" "github.com/go-co-op/gocron" ) func main() { s := gocron.NewScheduler(time.UTC) fmt.Println(s.Location()) }
Output: UTC
func (*Scheduler) NextRun ¶
NextRun datetime when the next Job should run.
Example ¶
package main import ( "fmt" "time" "github.com/go-co-op/gocron" ) var task = func() { fmt.Println("I am a task") } func main() { s := gocron.NewScheduler(time.UTC) _, _ = s.Every(1).Day().At("10:30").Do(task) s.StartAsync() _, time := s.NextRun() fmt.Println(time.Format("15:04")) // print only the hour and minute (hh:mm) }
Output: 10:30
func (*Scheduler) Remove ¶
func (s *Scheduler) Remove(j interface{})
Remove specific Job j by function
func (*Scheduler) RemoveByReference ¶
RemoveByReference removes specific Job j by reference
func (*Scheduler) RemoveJobByTag ¶
RemoveJobByTag will Remove Jobs by Tag
Example ¶
package main import ( "fmt" "time" "github.com/go-co-op/gocron" ) var task = func() { fmt.Println("I am a task") } func main() { s := gocron.NewScheduler(time.UTC) tag1 := []string{"tag1"} tag2 := []string{"tag2"} _, _ = s.Every(1).Week().SetTag(tag1).Do(task) _, _ = s.Every(1).Week().SetTag(tag2).Do(task) s.StartAsync() _ = s.RemoveJobByTag("tag1") }
Output:
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) StartAsync ¶
func (s *Scheduler) StartAsync() chan struct{}
StartAsync starts all jobs without blocking the current thread
Example ¶
package main import ( "fmt" "time" "github.com/go-co-op/gocron" ) var task = func() { fmt.Println("I am a task") } func main() { s := gocron.NewScheduler(time.UTC) _, _ = s.Every(3).Seconds().Do(task) s.StartAsync() }
Output:
func (*Scheduler) StartAt ¶
StartAt schedules the next run of the Job
Example ¶
package main import ( "fmt" "time" "github.com/go-co-op/gocron" ) var task = func() { fmt.Println("I am a task") } func main() { s := gocron.NewScheduler(time.UTC) specificTime := time.Date(2019, time.November, 10, 15, 0, 0, 0, time.UTC) _, _ = s.Every(1).Hour().StartAt(specificTime).Do(task) s.StartBlocking() }
Output:
func (*Scheduler) StartBlocking ¶
func (s *Scheduler) StartBlocking()
StartBlocking starts all jobs and blocks the current thread
Example ¶
package main import ( "fmt" "time" "github.com/go-co-op/gocron" ) var task = func() { fmt.Println("I am a task") } func main() { s := gocron.NewScheduler(time.UTC) _, _ = s.Every(3).Seconds().Do(task) s.StartBlocking() }
Output:
func (*Scheduler) Stop ¶
func (s *Scheduler) Stop()
Stop stops the scheduler. This is a no-op if the scheduler is already stopped .
Example ¶
package main import ( "fmt" "time" "github.com/go-co-op/gocron" ) var task = func() { fmt.Println("I am a task") } func main() { s := gocron.NewScheduler(time.UTC) _, _ = s.Every(1).Second().Do(task) s.StartAsync() s.Stop() }
Output: