Involved Source Filesexecutor.go 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.job.golocker.goscheduler.gotime_helper.go
Code Examples
package main
import (
"fmt"
"time"
"github.com/go-co-op/gocron"
)
func main() {
s := gocron.NewScheduler(time.UTC)
_, err := s.Every(1).Day().At("bad time").Do(func() {})
fmt.Println(err)
}
package main
import (
"fmt"
"time"
"github.com/go-co-op/gocron"
)
var task = func() {}
func main() {
s := gocron.NewScheduler(time.UTC)
job, _ := s.Every(1).Second().Do(task)
go func() {
for {
fmt.Println("Count of finished job runs", job.FinishedRunCount())
time.Sleep(time.Second)
}
}()
s.StartAsync()
}
package main
import (
"fmt"
"time"
"github.com/go-co-op/gocron"
)
var task = func() {}
func main() {
s := gocron.NewScheduler(time.UTC)
j, _ := s.Every(1).Second().Name("job_name").Do(task)
fmt.Println(j.GetName())
}
package main
import (
"fmt"
"time"
"github.com/go-co-op/gocron"
)
func main() {
s := gocron.NewScheduler(time.UTC)
j, _ := s.Every(10).Seconds().Do(func() { time.Sleep(2 * time.Second) })
fmt.Println(j.IsRunning())
s.StartAsync()
time.Sleep(time.Second)
fmt.Println(j.IsRunning())
time.Sleep(time.Second)
s.Stop()
time.Sleep(1 * time.Second)
fmt.Println(j.IsRunning())
}
package main
import (
"fmt"
"time"
"github.com/go-co-op/gocron"
)
var task = func() {}
func main() {
s := gocron.NewScheduler(time.UTC)
job, _ := s.Every(1).Second().Do(task)
s.StartAsync()
fmt.Println("Last run:", job.LastRun())
}
package main
import (
"time"
"github.com/go-co-op/gocron"
)
var task = func() {}
func main() {
s := gocron.NewScheduler(time.UTC)
job, _ := s.Every(1).Second().Do(task)
job.LimitRunsTo(2)
s.StartAsync()
}
package main
import (
"time"
"github.com/go-co-op/gocron"
)
var task = func() {}
func main() {
s := gocron.NewScheduler(time.UTC)
s.Every(1).Second().Name("job_name").Do(task)
}
package main
import (
"fmt"
"time"
"github.com/go-co-op/gocron"
)
var task = func() {}
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()
}
package main
import (
"fmt"
"time"
"github.com/go-co-op/gocron"
)
var task = func() {}
func main() {
s := gocron.NewScheduler(time.UTC)
job, _ := s.Every(1).Second().Do(task)
s.StartAsync()
fmt.Println("Previous run:", job.PreviousRun())
}
package main
import (
"fmt"
"time"
"github.com/go-co-op/gocron"
)
func main() {
s := gocron.NewScheduler(time.UTC)
job, _ := s.Every("1s").Name("my_func").Do(func() error { return fmt.Errorf("error") })
job.RegisterEventListeners(
gocron.AfterJobRuns(func(jobName string) {
fmt.Printf("afterJobRuns: %s\n", jobName)
}),
gocron.BeforeJobRuns(func(jobName string) {
fmt.Printf("beforeJobRuns: %s\n", jobName)
}),
gocron.WhenJobReturnsError(func(jobName string, err error) {
fmt.Printf("whenJobReturnsError: %s, %v\n", jobName, err)
}),
gocron.WhenJobReturnsNoError(func(jobName string) {
fmt.Printf("whenJobReturnsNoError: %s\n", jobName)
}),
)
s.StartAsync()
time.Sleep(100 * time.Millisecond)
s.Stop()
}
package main
import (
"fmt"
"time"
"github.com/go-co-op/gocron"
)
var task = func() {}
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()
}
package main
import (
"fmt"
"time"
"github.com/go-co-op/gocron"
)
var task = func() {}
func main() {
s := gocron.NewScheduler(time.UTC)
job, _ := s.Every(1).Day().At("10:30").Do(task)
s.StartAsync()
fmt.Println(job.ScheduledAtTime())
// if multiple times are set, the earliest time will be returned
job1, _ := s.Every(1).Day().At("10:30;08:00").Do(task)
fmt.Println(job1.ScheduledAtTime())
}
package main
import (
"fmt"
"time"
"github.com/go-co-op/gocron"
)
var task = func() {}
func main() {
s := gocron.NewScheduler(time.UTC)
job, _ := s.Every(1).Day().At("10:30;08:00").Do(task)
s.StartAsync()
fmt.Println(job.ScheduledAtTimes())
}
package main
import (
"fmt"
"time"
"github.com/go-co-op/gocron"
)
var task = func() {}
func main() {
s := gocron.NewScheduler(time.UTC)
job, _ := s.Every(1).Day().At("10:30").Do(task)
s.StartAsync()
fmt.Println(job.ScheduledTime())
}
package main
import (
"time"
"github.com/go-co-op/gocron"
)
var task = func() {}
func main() {
s := gocron.NewScheduler(time.UTC)
job, _ := s.Every(1).Week().Do(task)
job.SetEventListeners(func() {}, func() {})
}
package main
import (
"time"
"github.com/go-co-op/gocron"
)
var task = func() {}
func main() {
s := gocron.NewScheduler(time.UTC)
job, _ := s.Every(1).Second().Do(task)
job.SingletonMode()
}
package main
import (
"fmt"
"time"
"github.com/go-co-op/gocron"
)
var task = func() {}
func main() {
s := gocron.NewScheduler(time.UTC)
job, _ := s.Every("1s").Do(task)
job.Tag("tag1", "tag2", "tag3")
s.StartAsync()
fmt.Println(job.Tags())
}
package main
import (
"fmt"
"time"
"github.com/go-co-op/gocron"
)
var task = func() {}
func main() {
s := gocron.NewScheduler(time.UTC)
job, _ := s.Every("1s").Do(task)
job.Tag("tag1", "tag2", "tag3")
s.StartAsync()
fmt.Println(job.Tags())
}
package main
import (
"fmt"
"time"
"github.com/go-co-op/gocron"
)
var task = func() {}
func main() {
s := gocron.NewScheduler(time.UTC)
job, _ := s.Every("1s").Do(task)
job.Tag("tag1", "tag2", "tag3")
s.StartAsync()
fmt.Println(job.Tags())
job.Untag("tag2")
fmt.Println(job.Tags())
}
package main
import (
"fmt"
"time"
"github.com/go-co-op/gocron"
)
var task = func() {}
func main() {
s := gocron.NewScheduler(time.UTC)
weeklyJob, _ := s.Every(1).Week().Monday().Do(task)
weekday, _ := weeklyJob.Weekday()
fmt.Println(weekday)
dailyJob, _ := s.Every(1).Day().Do(task)
_, err := dailyJob.Weekday()
fmt.Println(err)
}
package main
import (
"fmt"
"time"
"github.com/go-co-op/gocron"
)
var task = func() {}
func main() {
s := gocron.NewScheduler(time.UTC)
j, _ := s.Every(1).Week().Monday().Wednesday().Friday().Do(task)
fmt.Println(j.Weekdays())
}
package main
import (
"time"
"github.com/go-co-op/gocron"
)
var task = func() {}
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)
// multiple
_, _ = s.Every(1).Monday().At("10:30;18:00").Do(task)
_, _ = s.Every(1).Monday().At("10:30").At("18:00").Do(task)
}
package main
import (
"fmt"
"log"
"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 {
log.Fatalf("Error loading location: %s", err)
}
s.ChangeLocation(location)
fmt.Println(s.Location())
}
package main
import (
"fmt"
"time"
"github.com/go-co-op/gocron"
)
var task = func() {}
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()
}
package main
import (
"time"
"github.com/go-co-op/gocron"
)
var task = func() {}
func main() {
s := gocron.NewScheduler(time.UTC)
// parsing handled by https://pkg.go.dev/github.com/robfig/cron/v3
// which follows https://en.wikipedia.org/wiki/Cron
_, _ = s.Cron("*/1 * * * *").Do(task) // every minute
_, _ = s.Cron("0 1 * * *").Do(task) // every day at 1 am
_, _ = s.Cron("0 0 * * 6,0").Do(task) // weekends only
}
package main
import (
"time"
"github.com/go-co-op/gocron"
)
var task = func() {}
func main() {
s := gocron.NewScheduler(time.UTC)
// parsing handled by https://pkg.go.dev/github.com/robfig/cron/v3
// which follows https://en.wikipedia.org/wiki/Cron
_, _ = s.CronWithSeconds("*/1 * * * * *").Do(task) // every second
_, _ = s.CronWithSeconds("0-30 * * * * *").Do(task) // every second 0-30
}
package main
import ()
func main() {
// Implement your own custom time struct
//
// type myCustomTime struct{}
//
// var _ gocron.TimeWrapper = (*myCustomTime)(nil)
//
// func (m myCustomTime) Now(loc *time.Location) time.Time {
// panic("implement me")
// }
//
// func (m myCustomTime) Sleep(duration time.Duration) {
// panic("implement me")
// }
//
// func (m myCustomTime) Unix(sec int64, nsec int64) time.Time {
// panic("implement me")
// }
//
// mct := myCustomTime{}
//
// s := gocron.NewScheduler(time.UTC)
// s.CustomTime(mct)
}
package main
import (
"time"
"github.com/go-co-op/gocron"
)
var task = func() {}
func main() {
s := gocron.NewScheduler(time.UTC)
s.CustomTimer(func(d time.Duration, f func()) *time.Timer {
// force jobs with 1 minute interval to run every second
if d == time.Minute {
d = time.Second
}
return time.AfterFunc(d, f)
})
// this job will run every 1 second
_, _ = s.Every("1m").Do(task)
}
package main
import (
"time"
"github.com/go-co-op/gocron"
)
var task = func() {}
func main() {
s := gocron.NewScheduler(time.UTC)
_, _ = s.Every("24h").Do(task)
_, _ = s.Every(1).Day().Do(task)
_, _ = s.Every(1).Days().Do(task)
}
package main
import (
"time"
"github.com/go-co-op/gocron"
)
var task = func() {}
func main() {
s := gocron.NewScheduler(time.UTC)
_, _ = s.Every("24h").Do(task)
_, _ = s.Every(1).Day().Do(task)
_, _ = s.Every(1).Days().Do(task)
}
package main
import (
"fmt"
"time"
"github.com/go-co-op/gocron"
)
var task = func() {}
func main() {
s := gocron.NewScheduler(time.UTC)
j1, err := s.Every(1).Second().Do(task)
fmt.Printf("Job: %v, Error: %v", j1, err)
taskWithParameters := func(param1, param2 string) {}
j2, err := s.Every(1).Second().Do(taskWithParameters, "param1", "param2")
fmt.Printf("Job: %v, Error: %v", j2, err)
s.StartAsync()
}
package main
import (
"fmt"
"log"
"time"
"github.com/go-co-op/gocron"
)
func main() {
task := func(in string, job gocron.Job) {
fmt.Printf("this job's last run: %s this job's next run: %s\n", job.LastRun(), job.NextRun())
fmt.Printf("in argument is %s\n", in)
ticker := time.NewTicker(100 * time.Millisecond)
defer ticker.Stop()
for {
select {
case <-job.Context().Done():
fmt.Printf("function has been canceled, performing cleanup and exiting gracefully\n")
return
case <-ticker.C:
fmt.Printf("performing a hard job that takes a long time that I want to kill whenever I want\n")
}
}
}
var err error
s := gocron.NewScheduler(time.UTC)
s.SingletonModeAll()
j, err := s.Every(1).Hour().Tag("myJob").DoWithJobDetails(task, "foo")
if err != nil {
log.Fatalln("error scheduling job", err)
}
s.StartAsync()
// Simulate some more work
time.Sleep(time.Second)
// I want to stop the job, together with the underlying goroutine
fmt.Printf("now I want to kill the job\n")
err = s.RemoveByTag("myJob")
if err != nil {
log.Fatalln("error removing job by tag", err)
}
// Wait a bit so that we can see that the job is exiting gracefully
time.Sleep(time.Second)
fmt.Printf("Job: %#v, Error: %#v", j, err)
}
package main
import (
"time"
"github.com/go-co-op/gocron"
)
var task = func() {}
func main() {
s := gocron.NewScheduler(time.UTC)
_, _ = s.Every(1).Second().Do(task)
_, _ = s.Every(1 * time.Second).Do(task)
_, _ = s.Every("1s").Do(task)
s.StartAsync()
}
package main
import (
"time"
"github.com/go-co-op/gocron"
)
var task = func() {}
func main() {
s := gocron.NewScheduler(time.UTC)
// every 1 - 5 seconds randomly
_, _ = s.EveryRandom(1, 5).Seconds().Do(task)
// every 5 - 10 hours randomly
_, _ = s.EveryRandom(5, 10).Hours().Do(task)
s.StartAsync()
}
package main
import (
"fmt"
"time"
"github.com/go-co-op/gocron"
)
var task = func() {}
func main() {
s := gocron.NewScheduler(time.UTC)
j, _ := s.Every(1).Day().Friday().Do(task)
s.StartAsync()
wd, _ := j.Weekday()
fmt.Println(wd)
}
package main
import (
"fmt"
"time"
"github.com/go-co-op/gocron"
)
var task = func() {}
func main() {
s := gocron.NewScheduler(time.UTC)
_, _ = s.Every(1).Second().Tag("tag1").Do(task)
_, _ = s.Every(1).Second().Tag("tag2").Do(task)
fmt.Println(s.GetAllTags())
}
package main
import (
"time"
"github.com/go-co-op/gocron"
)
var task = func() {}
func main() {
s := gocron.NewScheduler(time.UTC)
_, _ = s.Every("1h").Do(task)
_, _ = s.Every(1).Hour().Do(task)
_, _ = s.Every(1).Hours().Do(task)
}
package main
import (
"time"
"github.com/go-co-op/gocron"
)
var task = func() {}
func main() {
s := gocron.NewScheduler(time.UTC)
_, _ = s.Every("1h").Do(task)
_, _ = s.Every(1).Hour().Do(task)
_, _ = s.Every(1).Hours().Do(task)
}
package main
import (
"fmt"
"time"
"github.com/go-co-op/gocron"
)
var task = func() {}
func main() {
s := gocron.NewScheduler(time.UTC)
_, _ = s.Every("1s").Do(task)
fmt.Println(s.IsRunning())
s.StartAsync()
fmt.Println(s.IsRunning())
}
package main
import (
"time"
"github.com/go-co-op/gocron"
)
func main() {
s := gocron.NewScheduler(time.UTC)
j, _ := s.Every("1s").Do(func() {})
s.StartAsync()
time.Sleep(10 * time.Second)
_, _ = s.Job(j).Every("10m").Update()
time.Sleep(30 * time.Minute)
_, _ = s.Job(j).Every(1).Day().At("02:00").Update()
}
package main
import (
"fmt"
"time"
"github.com/go-co-op/gocron"
)
var task = func() {}
func main() {
s := gocron.NewScheduler(time.UTC)
_, _ = s.Every("1s").Do(task)
_, _ = s.Every("1s").Do(task)
_, _ = s.Every("1s").Do(task)
fmt.Println(len(s.Jobs()))
}
package main
import (
"fmt"
"time"
"github.com/go-co-op/gocron"
)
var task = func() {}
func main() {
s := gocron.NewScheduler(time.UTC)
_, _ = s.Every("1s").Do(task)
_, _ = s.Every("1s").Do(task)
_, _ = s.Every("1s").Do(task)
fmt.Printf("map of job uuid to job: %+v", s.JobsMap())
}
package main
import (
"fmt"
"time"
"github.com/go-co-op/gocron"
)
var task = func() {}
func main() {
s := gocron.NewScheduler(time.UTC)
_, _ = s.Every("1s").Do(task)
_, _ = s.Every("1s").Do(task)
_, _ = s.Every("1s").Do(task)
fmt.Println(s.Len())
}
package main
import (
"fmt"
"time"
"github.com/go-co-op/gocron"
)
var task = func() {}
func main() {
s := gocron.NewScheduler(time.UTC)
j, _ := s.Every(1).Second().LimitRunsTo(1).Do(task)
s.StartAsync()
time.Sleep(time.Millisecond * 100)
fmt.Println(j.RunCount())
}
package main
import (
"fmt"
"time"
"github.com/go-co-op/gocron"
)
func main() {
s := gocron.NewScheduler(time.UTC)
fmt.Println(s.Location())
}
package main
import (
"time"
"github.com/go-co-op/gocron"
)
var task = func() {}
func main() {
s := gocron.NewScheduler(time.UTC)
_, _ = s.Every(1).Day().Midday().Do(task)
s.StartAsync()
}
package main
import (
"time"
"github.com/go-co-op/gocron"
)
var task = func() {}
func main() {
s := gocron.NewScheduler(time.UTC)
_, _ = s.Every(1).Millisecond().Do(task)
_, _ = s.Every(1).Milliseconds().Do(task)
_, _ = s.Every("1ms").Seconds().Do(task)
_, _ = s.Every(time.Millisecond).Seconds().Do(task)
}
package main
import (
"time"
"github.com/go-co-op/gocron"
)
var task = func() {}
func main() {
s := gocron.NewScheduler(time.UTC)
_, _ = s.Every(1).Millisecond().Do(task)
_, _ = s.Every(1).Milliseconds().Do(task)
_, _ = s.Every("1ms").Seconds().Do(task)
_, _ = s.Every(time.Millisecond).Seconds().Do(task)
}
package main
import (
"time"
"github.com/go-co-op/gocron"
)
var task = func() {}
func main() {
s := gocron.NewScheduler(time.UTC)
_, _ = s.Every("1m").Do(task)
_, _ = s.Every(1).Minute().Do(task)
_, _ = s.Every(1).Minutes().Do(task)
}
package main
import (
"time"
"github.com/go-co-op/gocron"
)
var task = func() {}
func main() {
s := gocron.NewScheduler(time.UTC)
_, _ = s.Every("1m").Do(task)
_, _ = s.Every(1).Minute().Do(task)
_, _ = s.Every(1).Minutes().Do(task)
}
package main
import (
"fmt"
"time"
"github.com/go-co-op/gocron"
)
var task = func() {}
func main() {
s := gocron.NewScheduler(time.UTC)
j, _ := s.Every(1).Day().Monday().Do(task)
s.StartAsync()
wd, _ := j.Weekday()
fmt.Println(wd)
}
package main
import (
"time"
"github.com/go-co-op/gocron"
)
var task = func() {}
func main() {
s := gocron.NewScheduler(time.UTC)
_, _ = s.Every(1).Month().Do(task)
_, _ = s.Every(1).Month(1).Do(task)
_, _ = s.Every(1).Months(1).Do(task)
_, _ = s.Every(1).Month(1, 2).Do(task)
_, _ = s.Month(1, 2).Every(1).Do(task)
_, _ = s.Every(1).Month(-1).Do(task)
_, _ = s.Every(1).Month(-2).Do(task)
}
package main
import (
"time"
"github.com/go-co-op/gocron"
)
var task = func() {}
func main() {
s := gocron.NewScheduler(time.UTC)
_, _ = s.MonthFirstWeekday(time.Monday).Do(task)
s.StartAsync()
}
package main
import (
"time"
"github.com/go-co-op/gocron"
)
var task = func() {}
func main() {
s := gocron.NewScheduler(time.UTC)
_, _ = s.Every(1).MonthLastDay().Do(task)
_, _ = s.Every(2).MonthLastDay().Do(task)
_, _ = s.Every(1).MonthLastDay(-1).Do(task)
_, _ = s.Every(1).MonthLastDay(-2).Do(task)
}
package main
import (
"time"
"github.com/go-co-op/gocron"
)
var task = func() {}
func main() {
s := gocron.NewScheduler(time.UTC)
_, _ = s.Every(1).Month(1).Do(task)
_, _ = s.Every(1).Months(1).Do(task)
_, _ = s.Every(1).Months(-1).Do(task)
_, _ = s.Every(1).Months(-2).Do(task)
}
package main
import (
"time"
"github.com/go-co-op/gocron"
)
var task = func() {}
func main() {
s := gocron.NewScheduler(time.UTC)
_, _ = s.Every(1).Day().At("10:30").Do(task)
s.StartAsync()
s.NextRun()
}
package main
import (
"time"
"github.com/go-co-op/gocron"
)
var task = func() {}
func main() {
s := gocron.NewScheduler(time.UTC)
_, _ = s.Every("1s").Do(task)
s.StartAsync()
s.PauseJobExecution(true)
// jobs don't run
s.PauseJobExecution(false)
// jobs run again
}
package main
import (
"fmt"
"time"
"github.com/go-co-op/gocron"
)
func main() {
s := gocron.NewScheduler(time.UTC)
s.Every("1s").Name("my_func_1").Do(func() error { return fmt.Errorf("error_1") })
s.Every("1s").Name("my_func_2").
StartAt(time.Now().UTC().Add(50 * time.Millisecond)).
Do(func() error { return fmt.Errorf("error_2") })
s.RegisterEventListeners(
gocron.AfterJobRuns(func(jobName string) {
fmt.Printf("afterJobRuns: %s\n", jobName)
}),
gocron.BeforeJobRuns(func(jobName string) {
fmt.Printf("beforeJobRuns: %s\n", jobName)
}),
gocron.WhenJobReturnsError(func(jobName string, err error) {
fmt.Printf("whenJobReturnsError: %s, %v\n", jobName, err)
}),
gocron.WhenJobReturnsNoError(func(jobName string) {
fmt.Printf("whenJobReturnsNoError: %s\n", jobName)
}),
)
s.StartAsync()
time.Sleep(120 * time.Millisecond)
s.Stop()
}
package main
import (
"fmt"
"time"
"github.com/go-co-op/gocron"
)
var task = func() {}
func main() {
s := gocron.NewScheduler(time.UTC)
_, _ = s.Every(1).Week().Do(task)
s.StartAsync()
s.Remove(task)
fmt.Println(s.Len())
}
package main
import (
"fmt"
"time"
"github.com/go-co-op/gocron"
)
var task = func() {}
func main() {
s := gocron.NewScheduler(time.UTC)
j, _ := s.Every(1).Week().Do(task)
_, _ = s.Every(1).Week().Do(task)
s.StartAsync()
s.RemoveByID(j)
fmt.Println(s.Len())
}
package main
import (
"fmt"
"time"
"github.com/go-co-op/gocron"
)
var task = func() {}
func main() {
s := gocron.NewScheduler(time.UTC)
j, _ := s.Every(1).Week().Do(task)
_, _ = s.Every(1).Week().Do(task)
s.StartAsync()
s.RemoveByReference(j)
fmt.Println(s.Len())
}
package main
import (
"fmt"
"time"
"github.com/go-co-op/gocron"
)
var task = func() {}
func main() {
s := gocron.NewScheduler(time.UTC)
_, _ = s.Every(1).Week().Tag("tag1").Do(task)
_, _ = s.Every(1).Week().Tag("tag2").Do(task)
s.StartAsync()
_ = s.RemoveByTag("tag1")
fmt.Println(s.Len())
}
package main
import (
"fmt"
"time"
"github.com/go-co-op/gocron"
)
var task = func() {}
func main() {
s := gocron.NewScheduler(time.UTC)
_, _ = s.Every(1).Week().Tag("tag1", "tag2", "tag3").Do(task)
_, _ = s.Every(1).Week().Tag("tag1", "tag2").Do(task)
_, _ = s.Every(1).Week().Tag("tag1").Do(task)
s.StartAsync()
_ = s.RemoveByTags("tag1", "tag2")
fmt.Println(s.Len())
}
package main
import (
"fmt"
"time"
"github.com/go-co-op/gocron"
)
var task = func() {}
func main() {
s := gocron.NewScheduler(time.UTC)
_, _ = s.Every(1).Week().Tag("tag1", "tag2", "tag3").Do(task)
_, _ = s.Every(1).Week().Tag("tag1").Do(task)
_, _ = s.Every(1).Week().Tag("tag2").Do(task)
_, _ = s.Every(1).Week().Tag("tag3").Do(task)
s.StartAsync()
_ = s.RemoveByTagsAny("tag1", "tag3")
fmt.Println(s.Len())
}
package main
import (
"time"
"github.com/go-co-op/gocron"
)
var task = func() {}
func main() {
s := gocron.NewScheduler(time.UTC)
_, _ = s.Every(1).Day().At("10:00").Do(task)
_, _ = s.Every(2).Day().At("10:00").Do(task)
_, _ = s.Every(3).Day().At("10:00").Do(task)
s.StartAsync()
s.RunAll()
}
package main
import (
"time"
"github.com/go-co-op/gocron"
)
var task = func() {}
func main() {
s := gocron.NewScheduler(time.UTC)
_, _ = s.Every(1).Day().At("10:00").Do(task)
_, _ = s.Every(2).Day().At("10:00").Do(task)
_, _ = s.Every(3).Day().At("10:00").Do(task)
s.StartAsync()
s.RunAllWithDelay(10 * time.Second)
}
package main
import (
"time"
"github.com/go-co-op/gocron"
)
var task = func() {}
func main() {
s := gocron.NewScheduler(time.UTC)
_, _ = s.Every(1).Day().At("10:00").Do(task)
_, _ = s.Every(2).Day().Tag("tag").At("10:00").Do(task)
s.StartAsync()
_ = s.RunByTag("tag")
}
package main
import (
"time"
"github.com/go-co-op/gocron"
)
var task = func() {}
func main() {
s := gocron.NewScheduler(time.UTC)
_, _ = s.Every(1).Day().Tag("tag").At("10:00").Do(task)
_, _ = s.Every(2).Day().Tag("tag").At("10:00").Do(task)
s.StartAsync()
_ = s.RunByTagWithDelay("tag", 2*time.Second)
}
package main
import (
"fmt"
"time"
"github.com/go-co-op/gocron"
)
var task = func() {}
func main() {
s := gocron.NewScheduler(time.UTC)
j, _ := s.Every(1).Day().Saturday().Do(task)
s.StartAsync()
wd, _ := j.Weekday()
fmt.Println(wd)
}
package main
import (
"time"
"github.com/go-co-op/gocron"
)
var task = func() {}
func main() {
s := gocron.NewScheduler(time.UTC)
// the default unit is seconds
// these are all the same
_, _ = s.Every(1).Do(task)
_, _ = s.Every(1).Second().Do(task)
_, _ = s.Every(1).Seconds().Do(task)
_, _ = s.Every("1s").Seconds().Do(task)
_, _ = s.Every(time.Second).Seconds().Do(task)
}
package main
import (
"time"
"github.com/go-co-op/gocron"
)
var task = func() {}
func main() {
s := gocron.NewScheduler(time.UTC)
// the default unit is seconds
// these are all the same
_, _ = s.Every(1).Do(task)
_, _ = s.Every(1).Second().Do(task)
_, _ = s.Every(1).Seconds().Do(task)
_, _ = s.Every("1s").Seconds().Do(task)
_, _ = s.Every(time.Second).Seconds().Do(task)
}
package main
import (
"fmt"
"time"
"github.com/go-co-op/gocron"
)
func main() {
s := gocron.NewScheduler(time.UTC)
s.SetMaxConcurrentJobs(1, gocron.RescheduleMode)
_, _ = s.Every(1).Seconds().Do(func() {
fmt.Println("This will run once every 5 seconds even though it is scheduled every second because maximum concurrent job limit is set.")
time.Sleep(5 * time.Second)
})
}
package main
import (
"time"
"github.com/go-co-op/gocron"
)
var task = func() {}
func main() {
s := gocron.NewScheduler(time.UTC)
_, _ = s.Every(1).Second().SingletonMode().Do(task)
}
package main
import (
"time"
"github.com/go-co-op/gocron"
)
var task = func() {}
func main() {
s := gocron.NewScheduler(time.UTC)
s.SingletonModeAll()
_, _ = s.Every(1).Second().Do(task)
}
package main
import (
"time"
"github.com/go-co-op/gocron"
)
var task = func() {}
func main() {
s := gocron.NewScheduler(time.UTC)
_, _ = s.Every(3).Seconds().Do(task)
s.StartAsync()
}
package main
import (
"time"
"github.com/go-co-op/gocron"
)
var task = func() {}
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()
}
package main
import (
"time"
"github.com/go-co-op/gocron"
)
var task = func() {}
func main() {
s := gocron.NewScheduler(time.UTC)
_, _ = s.Every(3).Seconds().Do(task)
s.StartBlocking()
}
package main
import (
"time"
"github.com/go-co-op/gocron"
)
var task = func() {}
func main() {
s := gocron.NewScheduler(time.UTC)
_, _ = s.Cron("0 0 * * 6,0").StartImmediately().Do(task)
s.StartBlocking()
}
package main
import (
"fmt"
"time"
"github.com/go-co-op/gocron"
)
var task = func() {}
func main() {
s := gocron.NewScheduler(time.UTC)
_, _ = s.Every(1).Second().Do(task)
s.StartAsync()
s.Stop()
fmt.Println(s.IsRunning())
s = gocron.NewScheduler(time.UTC)
go func() {
time.Sleep(1 * time.Second)
s.Stop()
}()
s.StartBlocking()
fmt.Println(".Stop() stops the blocking start")
}
package main
import (
"fmt"
"time"
"github.com/go-co-op/gocron"
)
var task = func() {}
func main() {
s := gocron.NewScheduler(time.UTC)
j, _ := s.Every(1).Day().Sunday().Do(task)
s.StartAsync()
wd, _ := j.Weekday()
fmt.Println(wd)
}
package main
import (
"fmt"
"time"
"github.com/go-co-op/gocron"
)
var task = func() {}
func main() {
s := gocron.NewScheduler(time.UTC)
j, _ := s.Every(1).Week().Tag("tag").Do(task)
fmt.Println(j.Tags())
}
package main
import (
"fmt"
"time"
"github.com/go-co-op/gocron"
)
var task = func() {}
func main() {
s := gocron.NewScheduler(time.UTC)
s.TagsUnique()
_, _ = s.Every(1).Week().Tag("foo").Do(task)
_, err := s.Every(1).Week().Tag("foo").Do(task)
fmt.Println(err)
}
package main
import (
"fmt"
"time"
"github.com/go-co-op/gocron"
)
var task = func() {}
func main() {
s := gocron.NewScheduler(time.UTC)
_, _ = s.Every(1).Do(task)
fmt.Println(s.TaskPresent(task))
}
package main
import (
"fmt"
"time"
"github.com/go-co-op/gocron"
)
var task = func() {}
func main() {
s := gocron.NewScheduler(time.UTC)
j, _ := s.Every(1).Day().Thursday().Do(task)
s.StartAsync()
wd, _ := j.Weekday()
fmt.Println(wd)
}
package main
import (
"fmt"
"time"
"github.com/go-co-op/gocron"
)
var task = func() {}
func main() {
s := gocron.NewScheduler(time.UTC)
j, _ := s.Every(1).Day().Tuesday().Do(task)
s.StartAsync()
wd, _ := j.Weekday()
fmt.Println(wd)
}
package main
import (
"time"
"github.com/go-co-op/gocron"
)
var task = func() {}
func main() {
s := gocron.NewScheduler(time.UTC)
j, _ := s.Every("1s").Do(task)
s.StartAsync()
time.Sleep(10 * time.Second)
_, _ = s.Job(j).Every("10m").Update()
time.Sleep(30 * time.Minute)
_, _ = s.Job(j).Every(1).Day().At("02:00").Update()
}
package main
import (
"time"
"github.com/go-co-op/gocron"
)
var task = func() {}
func main() {
s := gocron.NewScheduler(time.UTC)
// job will run 5 minutes from the scheduler starting
_, _ = s.Every("5m").WaitForSchedule().Do(task)
// job will run immediately and 5 minutes from the scheduler starting
_, _ = s.Every("5m").Do(task)
s.StartAsync()
}
package main
import (
"time"
"github.com/go-co-op/gocron"
)
var task = func() {}
func main() {
s := gocron.NewScheduler(time.UTC)
s.WaitForScheduleAll()
// all jobs will run 5 minutes from the scheduler starting
_, _ = s.Every("5m").Do(task)
_, _ = s.Every("5m").Do(task)
s.StartAsync()
}
package main
import (
"fmt"
"time"
"github.com/go-co-op/gocron"
)
var task = func() {}
func main() {
s := gocron.NewScheduler(time.UTC)
j, _ := s.Every(1).Day().Wednesday().Do(task)
s.StartAsync()
wd, _ := j.Weekday()
fmt.Println(wd)
}
package main
import (
"time"
"github.com/go-co-op/gocron"
)
var task = func() {}
func main() {
s := gocron.NewScheduler(time.UTC)
_, _ = s.Every(1).Week().Do(task)
_, _ = s.Every(1).Weeks().Do(task)
_, _ = s.Every(1).Week().Monday().Wednesday().Friday().Do(task)
}
package main
import (
"time"
"github.com/go-co-op/gocron"
)
var task = func() {}
func main() {
s := gocron.NewScheduler(time.UTC)
_, _ = s.Every(1).Week().Weekday(time.Monday).Do(task)
_, _ = s.Every(1).Weeks().Weekday(time.Tuesday).Weekday(time.Friday).Do(task)
}
package main
import (
"time"
"github.com/go-co-op/gocron"
)
var task = func() {}
func main() {
s := gocron.NewScheduler(time.UTC)
_, _ = s.Every(1).Week().Do(task)
_, _ = s.Every(1).Weeks().Do(task)
_, _ = s.Every(2).Weeks().Monday().Wednesday().Friday().Do(task)
}
package main
import ()
func main() {
//redisOptions := &redis.Options{
// Addr: "localhost:6379",
//}
//redisClient := redis.NewClient(redisOptions)
//locker, err := redislock.NewRedisLocker(redisClient)
//if err != nil {
// // handle the error
//}
//
//s := gocron.NewScheduler(time.UTC)
//s.WithDistributedLocker(locker)
//_, err = s.Every("500ms").Do(task)
//if err != nil {
// // handle the error
//}
}
package main
import (
"fmt"
"github.com/go-co-op/gocron"
)
func main() {
gocron.SetPanicHandler(func(jobName string, _ interface{}) {
fmt.Printf("Panic in job: %s", jobName)
fmt.Println("do something to handle the panic")
})
}
Package-Level Type Names (total 8)
/* sort by: | */
Elector determines the leader from instances asking to be the leader. Only
the leader runs jobs. If the leader goes down, a new leader will be elected. IsLeader should return an error if the job should not be scheduled and nil if the job should be scheduled.
func (*Scheduler).WithDistributedElector(e Elector)
Job struct stores the information necessary to run a Job Context returns the job's context. The context controls cancellation. 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. FinishedRunCount returns the number of times the job has finished running GetName returns the name of the current job.
The name is either the name set using Job.Name() / Scheduler.Name() or
the name of the funcion as Go sees it, for example `main.func1` IsRunning reports whether any instances of the job function are currently running LastRun returns the time the job was run last 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() Name sets the name of the current job.
If the scheduler is running using WithDistributedLocker(),
the job name is used as the distributed lock key. NextRun returns the time the job will run next PreviousRun returns the job run time previous to LastRun RegisterEventListeners accepts EventListeners and registers them for the job
The event listeners are then called at the times described by each listener. RunCount returns the number of times the job has been started ScheduledAtTime returns the specific time of day the Job will run at.
If multiple times are set, the earliest time will be returned. ScheduledAtTimes returns the specific times of day the Job will run at ScheduledTime returns the time of the Job's next scheduled run Deprecated: SetEventListeners accepts two functions that will be called, one before and one after the job is run 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. Tag allows you to add arbitrary labels to a Job that do not
impact the functionality of the Job Tags returns the tags attached to the Job Untag removes a tag from a Job Weekday returns which day of the week the Job will run on and
will return an error if the Job is not scheduled weekly Weekdays returns a slice of time.Weekday that the Job will run in a week and
will return an error if the Job is not scheduled weekly
func (*Scheduler).Do(jobFun interface{}, params ...interface{}) (*Job, error)
func (*Scheduler).DoWithJobDetails(jobFun interface{}, params ...interface{}) (*Job, error)
func (*Scheduler).FindJobsByTag(tags ...string) ([]*Job, error)
func (*Scheduler).Jobs() []*Job
func (*Scheduler).JobsMap() map[uuid.UUID]*Job
func (*Scheduler).NextRun() (*Job, time.Time)
func (*Scheduler).Update() (*Job, error)
func (*Scheduler).Job(j *Job) *Scheduler
func (*Scheduler).RemoveByID(job *Job) error
func (*Scheduler).RemoveByReference(job *Job)
Locker represents the required interface to lock jobs when running multiple schedulers. Lock if an error is returned by lock, the job will not be scheduled.
func (*Scheduler).WithDistributedLocker(l Locker)
PanicHandlerFunc represents a type that can be set to handle panics occurring
during job execution.
func SetPanicHandler(handler PanicHandlerFunc)
Scheduler struct stores a list of Jobs and the location of time used by the Scheduler 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).
When the At time(s) occur on the same day on which the scheduler is started
the Job will be run at the first available At time.
For example: a schedule for every 2 days at 9am and 11am
- currently 7am -> Job runs at 9am and 11am on the day the scheduler was started
- currently 12 noon -> Job runs at 9am and 11am two days after the scheduler started ChangeLocation changes the default time location Clear clears all Jobs from this scheduler(*Scheduler) Cron(cronExpression string) *Scheduler(*Scheduler) CronWithSeconds(cronExpression string) *Scheduler CustomTime takes an in a struct that implements the TimeWrapper interface
allowing the caller to mock the time used by the scheduler. This is useful
for tests relying on gocron. CustomTimer takes in a function that mirrors the time.AfterFunc
This is used to mock the time.AfterFunc function used by the scheduler
for testing long intervals in a short amount of time. Day sets the unit with days Days set the unit with days Do specifies the jobFunc that should be called every time the Job runs DoWithJobDetails specifies the jobFunc that should be called every time the Job runs
and additionally passes the details of the current job to the jobFunc.
The last argument of the function must be a gocron.Job that will be passed by
the scheduler when the function is called. Every schedules a new periodic Job with an interval.
Interval can be an int, time.Duration or a string that
parses with time.ParseDuration().
Negative intervals will return an error.
Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".
The job is run immediately, unless:
* StartAt or At is set on the job,
* WaitForSchedule is set on the job,
* or WaitForScheduleAll is set on the scheduler. EveryRandom schedules a new period Job that runs at random intervals
between the provided lower (inclusive) and upper (inclusive) bounds.
The default unit is Seconds(). Call a different unit in the chain
if you would like to change that. For example, Minutes(), Hours(), etc. FindJobsByTag will return a slice of jobs that match all given tags Friday sets the start day as Friday GetAllTags returns all tags. Hour sets the unit with hours Hours sets the unit with hours IsRunning returns true if the scheduler is running 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() Jobs returns the list of Jobs from the scheduler JobsMap returns a map of job uuid to job Len returns the number of Jobs in the Scheduler LimitRunsTo limits the number of executions of this job to n.
Upon reaching the limit, the job is removed from the scheduler. Location provides the current location set on the scheduler(*Scheduler) Midday() *Scheduler Millisecond sets the unit with milliseconds Milliseconds sets the unit with milliseconds Minute sets the unit with minutes Minutes sets the unit with minutes Monday sets the start day as Monday Month sets the unit with months
Note: Only days 1 through 28 are allowed for monthly schedules
Note: Multiple of the same day of month is not allowed
Note: Negative numbers are special values and can only occur as single argument
and count backwards from the end of the month -1 == last day of the month, -2 == penultimate day of the month MonthFirstWeekday sets the job to run the first specified weekday of the month MonthLastDay sets the unit with months at every last day of the month
The optional parameter is a negative integer denoting days previous to the
last day of the month. E.g. -1 == the penultimate day of the month,
-2 == two days for the last day of the month Months sets the unit with months
Note: Only days 1 through 28 are allowed for monthly schedules
Note: Multiple of the same day of month is not allowed
Note: Negative numbers are special values and can only occur as single argument
and count backwards from the end of the month -1 == last day of the month, -2 == penultimate day of the month Name sets the name of the current job.
If the scheduler is running using WithDistributedLocker(), the job name is used
as the distributed lock key. If the job name is not set, the function name is used as the distributed lock key. NextRun datetime when the next Job should run.(*Scheduler) PauseJobExecution(shouldPause bool) RegisterEventListeners accepts EventListeners and registers them for all jobs
in the scheduler at the time this function is called.
The event listeners are then called at the times described by each listener.
If a new job is added, an additional call to this method, or the job specific
version must be executed in order for the new job to trigger event listeners. Remove specific Job by function
Removing a job stops that job's timer. However, if a job has already
been started by the job's timer before being removed, the only way to stop
it through gocron is to use DoWithJobDetails and access the job's Context which
informs you when the job has been canceled.
Alternatively, the job function would need to have implemented a means of
stopping, e.g. using a context.WithCancel() passed as params to Do method.
The above are based on what the underlying library suggests https://pkg.go.dev/time#Timer.Stop. RemoveByID removes the job from the scheduler looking up by id RemoveByReference removes specific Job by reference RemoveByTag will remove jobs that match the given tag. RemoveByTags will remove jobs that match all given tags. RemoveByTagsAny will remove jobs that match any one of the given tags. RunAll run all Jobs regardless if they are scheduled to run or not RunAllWithDelay runs all Jobs with the provided delay in between each Job RunByTag runs all the Jobs containing a specific tag
regardless of whether they are scheduled to run or not RunByTagWithDelay is same as RunByTag but introduces a delay between
each Job execution Saturday sets the start day as Saturday Second sets the unit with seconds Seconds sets the unit with seconds 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.
Note: WaitMode and RescheduleMode provide details on usage and potential risks. SingletonMode prevents a new job from starting if the prior job has not yet
completed its run
Warning: do not use this mode if your jobs will continue to stack
up beyond the ability of the limit workers to keep up. An example of
what NOT to do:
s.Every("1s").SingletonMode().Do(func() {
// this will result in an ever-growing number of goroutines
// blocked trying to send to the buffered channel
time.Sleep(10 * time.Minute)
}) SingletonModeAll prevents new jobs from starting if the prior instance of the
particular job has not yet completed its run
Warning: do not use this mode if your jobs will continue to stack
up beyond the ability of the limit workers to keep up. An example of
what NOT to do:
s := gocron.NewScheduler(time.UTC)
s.SingletonModeAll()
s.Every("1s").Do(func() {
// this will result in an ever-growing number of goroutines
// blocked trying to send to the buffered channel
time.Sleep(10 * time.Minute)
}) StartAsync starts all jobs without blocking the current thread 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 StartBlocking starts all jobs and blocks the current thread.
This blocking method can be stopped with Stop() from a separate goroutine. 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. 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.(*Scheduler) StopBlockingChan() Sunday sets the start day as Sunday Tag will add a tag when creating a job. 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() TaskPresent checks if specific job's function was added to the scheduler. Thursday sets the start day as Thursday Tuesday sets the start day as Tuesday 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. WaitForSchedule sets the job to not start immediately
but rather wait until the first scheduled interval. 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. Wednesday sets the start day as Wednesday Week sets the unit with weeks Weekday sets the scheduledWeekdays with a specifics weekdays Weeks sets the unit with weeks WithDistributedElector prevents the same job from being run more than once
when multiple schedulers are trying to schedule the same job, by allowing only
the leader to run jobs. Non-leaders wait until the leader instance goes down
and then a new leader is elected.
Compared with the distributed lock, the election is the same as leader/follower framework.
All jobs are only scheduled and execute on the leader scheduler instance. Only when the leader scheduler goes down
and one of the scheduler instances is successfully elected, then the new leader scheduler instance can schedule jobs. WithDistributedLocker prevents the same job from being run more than once
when multiple schedulers are trying to schedule the same job.
One strategy to reduce splay in the job execution times when using
intervals (e.g. 1s, 1m, 1h), on each scheduler instance, is to use
StartAt with time.Now().Round(interval) to start the job at the
next interval boundary.
Another strategy is to use the Cron or CronWithSeconds methods as they
use the same behavior described above using StartAt.
NOTE - the Locker will NOT lock jobs using the singleton options:
SingletonMode, or SingletonModeAll
NOTE - beware of potential race conditions when running the Locker
with SetMaxConcurrentJobs and WaitMode as jobs are not guaranteed
to be locked when each scheduler's is below its limit and able
to run the job.
func NewScheduler(loc *time.Location) *Scheduler
func (*Scheduler).At(i interface{}) *Scheduler
func (*Scheduler).Cron(cronExpression string) *Scheduler
func (*Scheduler).CronWithSeconds(cronExpression string) *Scheduler
func (*Scheduler).Day() *Scheduler
func (*Scheduler).Days() *Scheduler
func (*Scheduler).Every(interval interface{}) *Scheduler
func (*Scheduler).EveryRandom(lower, upper int) *Scheduler
func (*Scheduler).Friday() *Scheduler
func (*Scheduler).Hour() *Scheduler
func (*Scheduler).Hours() *Scheduler
func (*Scheduler).Job(j *Job) *Scheduler
func (*Scheduler).LimitRunsTo(i int) *Scheduler
func (*Scheduler).Midday() *Scheduler
func (*Scheduler).Millisecond() *Scheduler
func (*Scheduler).Milliseconds() *Scheduler
func (*Scheduler).Minute() *Scheduler
func (*Scheduler).Minutes() *Scheduler
func (*Scheduler).Monday() *Scheduler
func (*Scheduler).Month(daysOfMonth ...int) *Scheduler
func (*Scheduler).MonthFirstWeekday(weekday time.Weekday) *Scheduler
func (*Scheduler).MonthLastDay(dayCountBeforeLastDayOfMonth ...int) *Scheduler
func (*Scheduler).Months(daysOfTheMonth ...int) *Scheduler
func (*Scheduler).Name(name string) *Scheduler
func (*Scheduler).Saturday() *Scheduler
func (*Scheduler).Second() *Scheduler
func (*Scheduler).Seconds() *Scheduler
func (*Scheduler).SingletonMode() *Scheduler
func (*Scheduler).StartAt(t time.Time) *Scheduler
func (*Scheduler).StartImmediately() *Scheduler
func (*Scheduler).Sunday() *Scheduler
func (*Scheduler).Tag(t ...string) *Scheduler
func (*Scheduler).Thursday() *Scheduler
func (*Scheduler).Tuesday() *Scheduler
func (*Scheduler).WaitForSchedule() *Scheduler
func (*Scheduler).Wednesday() *Scheduler
func (*Scheduler).Week() *Scheduler
func (*Scheduler).Weekday(weekDay time.Weekday) *Scheduler
func (*Scheduler).Weeks() *Scheduler
var github.com/limanmys/render-engine/internal/constants.GLOBAL_SCHEDULER *Scheduler
TimeWrapper is an interface that wraps the Now, Sleep, and Unix methods of the time package.
This allows the library and users to mock the time package for testing.( TimeWrapper) Now(*time.Location) time.Time( TimeWrapper) Sleep(time.Duration)( TimeWrapper) Unix(int64, int64) time.Time
func (*Scheduler).CustomTime(customTimeWrapper TimeWrapper)
Package-Level Functions (total 6)
AfterJobRuns is called after the job is run
This is called even when an error is returned
BeforeJobRuns is called before the job is run
NewScheduler creates a new Scheduler
SetPanicHandler sets the global panicHandler to the given function.
Leaving it nil or setting it to nil disables automatic panic handling.
If the panicHandler is not nil, any panic that occurs during executing a job will be recovered
and the panicHandlerFunc will be called with the job's funcName and the recover data.
WhenJobReturnsError is called when the job returns an error
WhenJobReturnsNoError is called when the job does not return an error
the function must accept a single parameter, which is an error
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
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.
Warning: do not use this mode if your jobs will continue to stack
up beyond the ability of the limit workers to keep up. An example of
what NOT to do:
s.Every("1s").Do(func() {
// this will result in an ever-growing number of goroutines
// blocked trying to send to the buffered channel
time.Sleep(10 * time.Minute)
})
The pages are generated with Goldsv0.6.7. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds.