quartz for Go
A Quartz-style job scheduler for Go, stdlib only.
A from-scratch, standard-library-only Go take on the classic Quartz job scheduler — no cgo, no third-party dependencies. You get Jobs (any Execute(context.Context) error, plus a JobFunc adapter and a JobDataMap for per-job state), two Trigger implementations, and a Scheduler that fires due triggers on a bounded worker pool. SimpleTrigger covers start-time-plus-interval-plus-repeat schedules; CronTrigger runs on a real cron parser supporting five- or six-field expressions with ranges, steps, lists, names and the * / ? wildcards, evaluated in any *time.Location. The scheduler adds graceful shutdown with drain, per-trigger and per-job pause/resume, configurable misfire policies, and job/trigger listeners that can veto a fire. Storage sits behind a JobStore interface with an in-memory implementation, and every fire-time decision flows through an injectable clock so schedules are fully deterministic and testable without sleeping.
Install
$ go get github.com/malcolmston/quartzQuick start
import "github.com/malcolmston/quartz"
// A pool of 4 workers running in real time.
s := quartz.NewScheduler(quartz.Options{Concurrency: 4})
job := quartz.NewJobDetail(quartz.NewKey("daily-report"),
quartz.JobFunc(func(ctx context.Context) error {
fmt.Println("generating report")
return nil
}))
// Fire every day at 10:00:00.
trigger, _ := quartz.NewCronTriggerWithKeys(
quartz.NewKey("report-trigger"), job.Key(), "0 0 10 * * *")
s.ScheduleJob(job, trigger)
s.Start()
defer s.Shutdown(true) // graceful drainFeatures
- Jobs — any
Job(Execute(context.Context) error), aJobFuncadapter, and aJobDataMapof per-job state carried through each fire JobDetail— names a job via aKey, withWithDescription,WithDataandDurablebuilder optionsSimpleTrigger— start time plus a fixedIntervaland a repeat count (orRepeatForever), with an optional end timeCronTriggerover a real parser —ParseCron/CronExpressionhandle 5/6 fields, ranges (1-5), steps (*/15), lists, names and?, evaluated in any*time.LocationviaInScheduler—NewSchedulerwith a boundedOptions.Concurrencyworker pool,ScheduleJob,Start, andShutdownwith optional drain- Pause / resume —
PauseTrigger/ResumeTriggerandPauseJob/ResumeJob, plus out-of-bandTriggerJob - Misfire policies —
MisfireSmart,MisfireFireNow,MisfireIgnoreandMisfireDoNothingviaWithMisfirePolicy - Listeners —
JobListenerandTriggerListenerhooks (embedBaseJobListener/BaseTriggerListener); a trigger listener canVetoJobExecution - Pluggable storage — a
JobStoreinterface with an in-memoryMemoryJobStore(NewMemoryJobStore) - Injectable clock —
Options.ClockplusProcessDuedrive cron and next-fire logic deterministically in tests, without sleeping - Zero dependencies — pure Go standard library, nothing to audit but the toolchain