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 job definitions and operations.
Specify when a job runs.
// Fixed interval
gocron.DurationJob(5*time.Minute)
// Random interval (jittered)
gocron.DurationRandomJob(time.Minute, 5*time.Minute)// Standard cron
gocron.CronJob("*/5 * * * *", false)
// With seconds
gocron.CronJob("*/30 * * * * *", true)
// With timezone
gocron.CronJob("TZ=America/New_York 0 9 * * *", false)See Cron Jobs API
// Daily at specific times
gocron.DailyJob(
1, // every day
gocron.NewAtTimes(
gocron.NewAtTime(9, 0, 0),
gocron.NewAtTime(17, 0, 0),
),
)
// Weekly on specific days
gocron.WeeklyJob(
1, // every week
gocron.NewWeekdays(time.Monday, time.Friday),
gocron.NewAtTimes(gocron.NewAtTime(9, 0, 0)),
)
// Monthly on specific days
gocron.MonthlyJob(
1, // every month
gocron.NewDaysOfTheMonth(1, 15),
gocron.NewAtTimes(gocron.NewAtTime(0, 0, 0)),
)gocron.OneTimeJob(
gocron.OneTimeJobStartDateTime(
time.Now().Add(time.Hour),
),
)Specify what to run.
task := gocron.NewTask(func() {
doWork()
})task := gocron.NewTask(func(msg string, count int) {
fmt.Printf("%s: %d\n", msg, count)
}, "hello", 42)task := gocron.NewTask(func(ctx context.Context) {
select {
case <-ctx.Done():
log.Println("Cancelled")
return
default:
doWork()
}
})task := gocron.NewTask(func() error {
if err := doWork(); err != nil {
return fmt.Errorf("work failed: %w", err)
}
return nil
})See Tasks API
j, err := s.NewJob(
gocron.DurationJob(time.Minute),
gocron.NewTask(myFunc),
gocron.WithName("my-job"),
gocron.WithTags("worker"),
)id := j.ID()
name := j.Name()
tags := j.Tags()
nextRun, err := j.NextRun()
lastRun, err := j.LastRun()err := j.RunNow()Triggers immediate execution outside of schedule.
newJob, err := s.Update(
j.ID(),
gocron.DurationJob(5*time.Minute), // New interval
gocron.NewTask(myFunc),
)err := s.RemoveJob(j.ID())gocron.WithName("job-name")
gocron.WithTags("tag1", "tag2")gocron.WithSingletonMode(gocron.LimitModeReschedule)
gocron.WithIntervalFromCompletion()gocron.WithStartAt(gocron.WithStartImmediately())
gocron.WithLimitRunsTo(100)gocron.WithEventListeners(
gocron.BeforeJobRuns(func(jobID uuid.UUID, jobName string) {
log.Printf("Starting: %s", jobName)
}),
gocron.AfterJobRuns(func(jobID uuid.UUID, jobName string) {
log.Printf("Completed: %s", jobName)
}),
gocron.AfterJobRunsWithError(func(jobID uuid.UUID, jobName string, err error) {
log.Printf("Failed: %s - %v", jobName, err)
}),
)gocron.WithDistributedJobLocker(locker)j, err := s.NewJob(
gocron.DurationJob(time.Minute),
gocron.NewTask(func(ctx context.Context) error {
select {
case <-ctx.Done():
return ctx.Err()
default:
if err := doWork(); err != nil {
return fmt.Errorf("work failed: %w", err)
}
return nil
}
}),
gocron.WithName("worker"),
gocron.WithTags("background", "worker"),
gocron.WithSingletonMode(gocron.LimitModeReschedule),
gocron.WithEventListeners(
gocron.AfterJobRunsWithError(func(jobID uuid.UUID, jobName string, err error) {
log.Printf("Job %s failed: %v", jobName, err)
}),
),
)
if err != nil {
log.Fatal(err)
}
log.Printf("Job created: %s (next run: %v)", j.Name(), j.NextRun())Install with Tessl CLI
npx tessl i tessl/golang-github-com-go-co-op-gocron-v2docs
api
examples
guides