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.
How to create and configure schedulers with NewScheduler and all SchedulerOption functions.
func NewScheduler(options ...SchedulerOption) (Scheduler, error)Creates a new scheduler with optional configuration. Returns an error if any option is invalid.
s, err := gocron.NewScheduler()
if err != nil {
return err
}
defer s.Shutdown()func WithLocation(location *time.Location) SchedulerOptionSets the default timezone for all jobs. Defaults to time.Local. Returns ErrWithLocationNil if nil.
Affects:
loc, _ := time.LoadLocation("America/Chicago")
s, _ := gocron.NewScheduler(
gocron.WithLocation(loc),
)func WithLogger(logger Logger) SchedulerOptionSets the logger for internal scheduler logging. Returns ErrWithLoggerNil if nil.
See Logger interface and Observability Guide.
s, _ := gocron.NewScheduler(
gocron.WithLogger(gocron.NewLogger(gocron.LogLevelInfo)),
)func WithStopTimeout(timeout time.Duration) SchedulerOptionSets the maximum time to wait for running jobs during StopJobs() or Shutdown(). Defaults to no timeout. Returns ErrWithStopTimeoutZeroOrNegative if ≤ 0.
s, _ := gocron.NewScheduler(
gocron.WithStopTimeout(30*time.Second),
)func WithLimitConcurrentJobs(limit uint, mode LimitMode) SchedulerOptionLimits the number of jobs running concurrently across the entire scheduler. Returns ErrWithLimitConcurrentJobsZero if limit is 0.
Modes:
LimitModeReschedule: Skip jobs if limit reachedLimitModeWait: Queue jobs if limit reacheds, _ := gocron.NewScheduler(
gocron.WithLimitConcurrentJobs(3, gocron.LimitModeReschedule),
)See Concurrency Guide.
func WithGlobalJobOptions(jobOptions ...JobOption) SchedulerOptionApplies job options to all jobs by default. Per-job options override global ones.
s, _ := gocron.NewScheduler(
gocron.WithGlobalJobOptions(
gocron.WithTags("production"),
gocron.WithSingletonMode(gocron.LimitModeReschedule),
),
)
// All jobs get these options by default
j, _ := s.NewJob(
gocron.DurationJob(time.Minute),
gocron.NewTask(myFunc),
gocron.WithTags("critical"), // adds to global "production" tag
)func WithMonitor(monitor Monitor) SchedulerOptionSets a basic metrics collector. Returns ErrWithMonitorNil if nil.
See Monitor interface and Observability Guide.
s, _ := gocron.NewScheduler(
gocron.WithMonitor(&myMonitor{}),
)func WithMonitorStatus(monitor MonitorStatus) SchedulerOptionSets a metrics collector that includes status and error information. Returns ErrWithMonitorNil if nil.
See MonitorStatus interface and Observability Guide.
s, _ := gocron.NewScheduler(
gocron.WithMonitorStatus(&myMonitorStatus{}),
)func WithSchedulerMonitor(monitor SchedulerMonitor) SchedulerOptionSets a detailed lifecycle monitor. Returns ErrSchedulerMonitorNil if nil.
See SchedulerMonitor interface and Observability Guide.
s, _ := gocron.NewScheduler(
gocron.WithSchedulerMonitor(&mySchedulerMonitor{}),
)func WithDistributedElector(elector Elector) SchedulerOptionEnables leader election for distributed deployments. Only the leader instance runs jobs. Returns ErrWithDistributedElectorNil if nil.
See Elector interface and Distributed Guide.
s, _ := gocron.NewScheduler(
gocron.WithDistributedElector(&myElector{}),
)func WithDistributedLocker(locker Locker) SchedulerOptionEnables per-job distributed locking. Returns ErrWithDistributedLockerNil if nil.
See Locker interface and Distributed Guide.
s, _ := gocron.NewScheduler(
gocron.WithDistributedLocker(&myLocker{}),
)func WithClock(clock clockwork.Clock) SchedulerOptionSets a custom clock implementation for testing. Returns ErrWithClockNil if nil.
import "github.com/jonboulle/clockwork"
fakeClock := clockwork.NewFakeClock()
s, _ := gocron.NewScheduler(
gocron.WithClock(fakeClock),
)
// In tests
fakeClock.Advance(5 * time.Minute)s, err := gocron.NewScheduler()
if err != nil {
return err
}
defer s.Shutdown()
j, err := s.NewJob(
gocron.DurationJob(time.Minute),
gocron.NewTask(func() {
fmt.Println("Running")
}),
)
s.Start()
select {} // blockloc, _ := time.LoadLocation("America/New_York")
s, err := gocron.NewScheduler(
gocron.WithLocation(loc),
gocron.WithLogger(gocron.NewLogger(gocron.LogLevelInfo)),
gocron.WithStopTimeout(30*time.Second),
gocron.WithLimitConcurrentJobs(5, gocron.LimitModeWait),
gocron.WithGlobalJobOptions(
gocron.WithTags("production"),
),
)Install with Tessl CLI
npx tessl i tessl/golang-github-com-go-co-op-gocron-v2@2.19.1docs
api
examples
guides