talira

A Golang job scheduling package which lets you run Go functions at pre-determined intervals

open sourceMITself-hostablefree

gocron runs Go functions on a schedule inside your own process: fixed or random durations, crontab expressions, daily, weekly and monthly times, or one-off times. A scheduler hands due jobs to an executor that can keep a job from overlapping itself (singleton mode) and cap how many run at once. When several replicas run the same scheduler, a distributed elector lets only the elected leader run jobs, or a distributed locker locks each run to one instance; the go-co-op organization publishes lockers for GORM-backed SQL databases and Redis, an etcd elector and gocron-ui, a web dashboard for the jobs. The GORM locker keys each lock on the job's name and its scheduled time with a unique constraint, so a run that another instance already claimed is skipped. It started as a fork of jasonlvhit/gocron; v2 is the current major version.

Problems it solves

  • run a Go function every night at 2am inside my service
  • cron-style scheduled tasks in a Go program without the system crontab
  • scaling my Go service out made every scheduled task fire on each copy
  • make sure only one instance of my Go service runs each scheduled job
  • a scheduled Go job takes longer than its interval and the runs pile up on each other
  • limit how many scheduled jobs run at the same time in a Go service
  • elect one leader among my Go replicas to run the scheduled tasks
  • use our database to lock each scheduled run so only one server executes it
  • test scheduled jobs in Go without waiting for real time to pass
  • see and trigger my in-process scheduled jobs from a web dashboard
  • get metrics on how long each scheduled job took and whether it failed

Features

  • duration, random duration, crontab, daily, weekly, monthly and one-time jobs
  • intervals from the scheduled time or from the last completion
  • singleton mode per job and a concurrency limit per scheduler, each rescheduling or queueing
  • distributed elector: only the elected leader runs jobs (etcd elector published)
  • distributed locker: each run locked to one instance (GORM and Redis lockers published)
  • GORM locker keys locks on job name plus scheduled timestamp, with configurable precision and TTL cleanup
  • event listeners on jobs and across the scheduler
  • Monitor, MonitorStatus and SchedulerMonitor interfaces for metrics
  • pluggable logger
  • gomock mocks and a fake clock (WithClock) for tests
View 1 more
  • gocron-ui web dashboard to monitor, trigger and manage jobs

Limitations

  • Doesn't coordinate runs across instances by default: each scheduler runs every job unless you add a distributed elector or locker12
  • Doesn't synchronize run times across schedulers with a locker: clock skew between instances can leave one instance running most jobs2

Capabilities

Takes pluginsplan not known

Alternatives

The same job

These do what gocron does.

  1. River Fast and reliable background jobs

Compare gocron with the top 3