v1.2.1

Getting Started

Last updated


This page covers installing go-scheduler, creating a scheduler, registering a first task, and shutting down gracefully.

Prerequisites


  • Go 1.23 or higher
  • A non-Windows platform: core.New depends on log/syslog and falls back to stderr when syslog is unreachable

Installation


Using go get


  1. go get github.com/pardnchiu/go-scheduler@latest

The package lives in the core subdirectory:
  1. import "github.com/pardnchiu/go-scheduler/core"

From Source


  1. git clone https://github.com/pardnchiu/go-scheduler.git
  2. cd go-scheduler
  3. go build ./...
  4. go test -race ./...

First Task


  1. package main
  2. import (
  3. "context"
  4. "log"
  5. "os/signal"
  6. "syscall"
  7. "github.com/pardnchiu/go-scheduler/core"
  8. )
  9. func main() {
  10. c, err := core.New(core.Config{})
  11. if err != nil {
  12. log.Fatal(err)
  13. }
  14. id, err := c.Add("@every 30s", func() {
  15. log.Println("heartbeat")
  16. }, "heartbeat")
  17. if err != nil {
  18. log.Fatal(err)
  19. }
  20. log.Println("task id:", id)
  21. c.Start()
  22. ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
  23. defer stop()
  24. <-ctx.Done()
  25. // wait for in-flight tasks
  26. <-c.Stop().Done()
  27. }

StepCallResult
1core.New(core.Config{})Creates a scheduler in time.Local
2c.Add("@every 30s", ...)Registers the task and returns an increasing ID; @every has a 30-second minimum
3c.Start()Computes each task's next fire time and starts the event loop
4c.Stop().Done()Stops scheduling; the context is cancelled once in-flight tasks finish

Where go-scheduler Fits


go-scheduler is an in-process library: you import core, register tasks in your own Go program, and each action runs in a goroutine of that process, with no separate server to deploy.
ProjectShapeDescription
robfig/cronIn-process Go libraryA cron library for Go
go-co-op/gocronIn-process Go libraryFluent Go cron scheduling
reugn/go-quartzIn-process Go libraryA minimalist, zero-dependency scheduling library
adhocore/gronxGo library and standalone daemonA cron expression parser with a task runner, job scheduler, and daemon
madflojo/tasksIn-process Go libraryAn in-process scheduler for recurring tasks
ouqiang/gocronSelf-hosted serviceA scheduling and management system with a web interface and separate task nodes running shell and HTTP jobs
labulakalia/crocodileSelf-hosted serviceA distributed task scheduling system with a scheduling center that calls worker nodes over gRPC

Within that in-process group, go-scheduler keeps every option on one Add call: a five-field cron expression, descriptor, or @every interval; prerequisites as []core.Wait with a per-prerequisite Stop or Skip policy; and an execution limit with a timeout callback. Stop returns a context that is cancelled once in-flight tasks without dependencies finish.

Next Steps


中文