A Golang job scheduling library that lets you run Go functions at pre-determined intervals using cron expressions, fixed durations, daily, weekly, monthly, or one-time schedules with support for distributed deployments.
Quick reference for scheduler operations.
// Basic scheduler
s, err := gocron.NewScheduler()
// With options
s, err := gocron.NewScheduler(
gocron.WithLocation(time.UTC),
gocron.WithStopTimeout(30*time.Second),
gocron.WithLimitConcurrentJobs(10, gocron.LimitModeReschedule),
)gocron.WithLimitConcurrentJobs(limit int, mode LimitMode)Limit total concurrent jobs across all jobs.
gocron.WithStopTimeout(duration time.Duration)
gocron.WithGlobalJobOptions(...JobOption)gocron.WithLogger(Logger)
gocron.WithMonitor(Monitor)
gocron.WithMonitorStatus(MonitorStatus)
gocron.WithSchedulerMonitor(SchedulerMonitor)gocron.WithDistributedElector(Elector)
gocron.WithDistributedLocker(Locker)loc, _ := time.LoadLocation("America/New_York")
gocron.WithLocation(loc)s.Start()Begin scheduling and executing jobs.
err := s.Shutdown()Stop scheduler gracefully, waiting for running jobs.
err := s.StopJobs()Use Shutdown() instead.
j, err := s.NewJob(
gocron.DurationJob(time.Minute),
gocron.NewTask(myFunc),
gocron.WithName("my-job"),
)newJob, err := s.Update(
jobID,
gocron.DurationJob(5*time.Minute),
gocron.NewTask(myFunc),
)// By ID
err := s.RemoveJob(jobID)
// By tags
err = s.RemoveByTags("tag1", "tag2")jobs := s.Jobs()
for _, j := range jobs {
log.Printf("Job: %s (ID: %s)", j.Name(), j.ID())
}waiting := s.JobsWaitingInQueue()
log.Printf("%d jobs waiting", waiting)// Create scheduler
loc, _ := time.LoadLocation("America/New_York")
s, err := gocron.NewScheduler(
gocron.WithLocation(loc),
gocron.WithStopTimeout(30*time.Second),
gocron.WithLimitConcurrentJobs(10, gocron.LimitModeReschedule),
gocron.WithLogger(myLogger),
gocron.WithSchedulerMonitor(myMonitor),
)
if err != nil {
panic(err)
}
defer s.Shutdown()
// Add jobs
j1, _ := s.NewJob(
gocron.DurationJob(time.Minute),
gocron.NewTask(task1),
gocron.WithName("task1"),
gocron.WithTags("worker"),
)
j2, _ := s.NewJob(
gocron.CronJob("0 9 * * *", false),
gocron.NewTask(task2),
gocron.WithName("daily-report"),
gocron.WithTags("report"),
)
// Start scheduling
s.Start()
// Wait for shutdown signal
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
defer cancel()
<-ctx.Done()
log.Println("Shutting down...")Install with Tessl CLI
npx tessl i tessl/golang-github-com-go-co-op-gocron-v2@2.19.1docs
api
examples
guides