CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/golang-github-com-go-co-op-gocron-v2

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.

Overview
Eval results
Files

scheduler.mddocs/guides/api/

API Reference: Scheduler

Quick reference for scheduler operations.

Creating Schedulers

// 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),
)

See Creating Schedulers API

Scheduler Options

Concurrency

gocron.WithLimitConcurrentJobs(limit int, mode LimitMode)

Limit total concurrent jobs across all jobs.

Lifecycle

gocron.WithStopTimeout(duration time.Duration)
gocron.WithGlobalJobOptions(...JobOption)

Observability

gocron.WithLogger(Logger)
gocron.WithMonitor(Monitor)
gocron.WithMonitorStatus(MonitorStatus)
gocron.WithSchedulerMonitor(SchedulerMonitor)

Distributed

gocron.WithDistributedElector(Elector)
gocron.WithDistributedLocker(Locker)

Timezone

loc, _ := time.LoadLocation("America/New_York")
gocron.WithLocation(loc)

Lifecycle Operations

Start

s.Start()

Begin scheduling and executing jobs.

Shutdown

err := s.Shutdown()

Stop scheduler gracefully, waiting for running jobs.

Stop (Deprecated)

err := s.StopJobs()

Use Shutdown() instead.

Job Management

Add Job

j, err := s.NewJob(
    gocron.DurationJob(time.Minute),
    gocron.NewTask(myFunc),
    gocron.WithName("my-job"),
)

Update Job

newJob, err := s.Update(
    jobID,
    gocron.DurationJob(5*time.Minute),
    gocron.NewTask(myFunc),
)

Remove Job

// By ID
err := s.RemoveJob(jobID)

// By tags
err = s.RemoveByTags("tag1", "tag2")

List Jobs

jobs := s.Jobs()
for _, j := range jobs {
    log.Printf("Job: %s (ID: %s)", j.Name(), j.ID())
}

Queue Status

waiting := s.JobsWaitingInQueue()
log.Printf("%d jobs waiting", waiting)

See Managing Jobs API

Example: Complete Setup

// 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...")

See Also

Install with Tessl CLI

npx tessl i tessl/golang-github-com-go-co-op-gocron-v2

docs

index.md

tile.json