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.
Guide to controlling concurrent job execution in gocron.
Gocron provides two levels of concurrency control:
Prevent a job from running concurrently with itself using WithSingletonMode.
Skip overlapping runs. Best for idempotent jobs.
j, _ := s.NewJob(
gocron.DurationJob(30*time.Second),
gocron.NewTask(healthCheck),
gocron.WithSingletonMode(gocron.LimitModeReschedule),
)Use cases:
Queue overlapping runs. All runs execute sequentially.
j, _ := s.NewJob(
gocron.DurationJob(30*time.Second),
gocron.NewTask(processQueue),
gocron.WithSingletonMode(gocron.LimitModeWait),
)Use cases:
Detailed guide: Singleton Mode
Limit total concurrent jobs across all jobs using scheduler-level options.
s, _ := gocron.NewScheduler(
gocron.WithLimitConcurrentJobs(3, gocron.LimitModeReschedule),
)Only 3 jobs run concurrently across all scheduled jobs.
Detailed guide: Scheduler Limits
Control when next run is calculated:
j, _ := s.NewJob(
gocron.DurationJob(5*time.Minute),
gocron.NewTask(slowOperation),
gocron.WithIntervalFromCompletion(), // 5min rest after completion
)Detailed guide: Interval Calculation
| Requirement | Solution |
|---|---|
| Prevent job self-overlap | WithSingletonMode |
| Limit total concurrent jobs | WithLimitConcurrentJobs at scheduler level |
| Guaranteed rest between runs | WithIntervalFromCompletion |
| Skip missed runs | LimitModeReschedule |
| Execute all runs | LimitModeWait |
Track concurrency events using SchedulerMonitor:
type myMonitor struct{}
func (m *myMonitor) ConcurrencyLimitReached(limitType string, job gocron.Job) {
log.Printf("Limit reached: %s for job %s", limitType, job.Name())
}
s, _ := gocron.NewScheduler(
gocron.WithSchedulerMonitor(&myMonitor{}),
)Install with Tessl CLI
npx tessl i tessl/golang-github-com-go-co-op-gocron-v2@2.19.1docs
api
examples
guides