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.
Practical examples of job and scheduler options.
package main
import (
"context"
"fmt"
"time"
"github.com/go-co-op/gocron/v2"
"github.com/google/uuid"
)
func main() {
s, _ := gocron.NewScheduler()
defer s.Shutdown()
j, _ := s.NewJob(
gocron.DurationJob(time.Minute),
gocron.NewTask(func() error {
return doWork()
}),
// Identity options
gocron.WithName("my-job"),
gocron.WithTags("worker", "background"),
// Concurrency options
gocron.WithSingletonMode(gocron.LimitModeReschedule),
gocron.WithIntervalFromCompletion(),
// Timing options
gocron.WithStartAt(gocron.WithStartImmediately()),
gocron.WithLimitRunsTo(100),
// Event options
gocron.WithEventListeners(
gocron.BeforeJobRuns(func(jobID uuid.UUID, jobName string) {
fmt.Printf("Starting: %s\n", jobName)
}),
gocron.AfterJobRuns(func(jobID uuid.UUID, jobName string) {
fmt.Printf("Completed: %s\n", jobName)
}),
gocron.AfterJobRunsWithError(func(jobID uuid.UUID, jobName string, err error) {
fmt.Printf("Failed: %s - %v\n", jobName, err)
}),
),
)
s.Start()
select {}
}
func doWork() error {
fmt.Println("Working...")
return nil
}func main() {
loc, _ := time.LoadLocation("America/New_York")
s, _ := gocron.NewScheduler(
// Location
gocron.WithLocation(loc),
// Lifecycle
gocron.WithStopTimeout(30*time.Second),
// Concurrency
gocron.WithLimitConcurrentJobs(10, gocron.LimitModeReschedule),
// Observability
gocron.WithLogger(&myLogger{}),
gocron.WithSchedulerMonitor(&myMonitor{}),
// Global job options
gocron.WithGlobalJobOptions(
gocron.WithTags("global"),
),
)
defer s.Shutdown()
// Add jobs...
s.Start()
select {}
}
type myLogger struct{}
func (l *myLogger) Debug(msg string, args ...any) {
fmt.Printf("[DEBUG] "+msg+"\n", args...)
}
func (l *myLogger) Error(msg string, args ...any) {
fmt.Printf("[ERROR] "+msg+"\n", args...)
}
func (l *myLogger) Info(msg string, args ...any) {
fmt.Printf("[INFO] "+msg+"\n", args...)
}
func (l *myLogger) Warn(msg string, args ...any) {
fmt.Printf("[WARN] "+msg+"\n", args...)
}
type myMonitor struct{}
func (m *myMonitor) JobScheduled(jobID uuid.UUID, job gocron.Job) {
fmt.Printf("Scheduled: %s\n", job.Name())
}
func (m *myMonitor) JobUnscheduled(jobID uuid.UUID, job gocron.Job) {
fmt.Printf("Unscheduled: %s\n", job.Name())
}
func (m *myMonitor) JobStarted(jobID uuid.UUID, job gocron.Job) {
fmt.Printf("Started: %s\n", job.Name())
}
func (m *myMonitor) JobCompleted(jobID uuid.UUID, job gocron.Job, err error) {
if err != nil {
fmt.Printf("Failed: %s - %v\n", job.Name(), err)
} else {
fmt.Printf("Completed: %s\n", job.Name())
}
}
func (m *myMonitor) ConcurrencyLimitReached(limitType string, job gocron.Job) {
fmt.Printf("Limit reached: %s for %s\n", limitType, job.Name())
}Install with Tessl CLI
npx tessl i tessl/golang-github-com-go-co-op-gocron-v2@2.19.1docs
api
examples
guides