Cron expression parser and job scheduler with overlap prevention for Go
go get github.com/philiprehberger/go-cron-kitCron expression parser and job scheduler with overlap prevention for Go
go get github.com/philiprehberger/go-cron-kit
import "github.com/philiprehberger/go-cron-kit"
sched, err := cronkit.Parse("*/5 * * * *") // every 5 minutes
next := sched.Next(time.Now())
fmt.Println(next)
s := cronkit.NewScheduler()
s.Add("cleanup", "0 2 * * *", func(ctx context.Context) {
// runs daily at 2 AM
cleanupOldRecords(ctx)
})
s.Add("heartbeat", "*/5 * * * *", func(ctx context.Context) {
// runs every 5 minutes
sendHeartbeat(ctx)
})
// Start blocks until context is cancelled
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
go s.Start(ctx)
Jobs that are still running when the next tick fires are automatically skipped.
next, ok := s.NextRun("cleanup")
if ok {
fmt.Printf("Next cleanup: %s\n", next)
}
names := s.Jobs() // ["cleanup", "heartbeat"]
s.Stop() // graceful shutdown
Standard 5-field format: minute hour day-of-month month day-of-week
* — any value*/n — every n1-5 — ranges1,3,5 — listsWhen both day-of-month and day-of-week are restricted (not *), the job runs when either matches (POSIX cron OR semantics). For example, 0 0 15 * 1 runs at midnight on the 15th or on Mondays.
| Function / Method | Description |
|---|---|
Parse(expr string) (*Schedule, error) | Parse a standard 5-field cron expression |
NewScheduler() *Scheduler | Create a new job scheduler |
Schedule.Next(after time.Time) time.Time | Return the next time after t that matches the schedule |
Scheduler.Add(name, expr string, handler func(ctx context.Context)) error | Register a new job with the given cron expression |
Scheduler.Start(ctx context.Context) | Start the scheduler, blocking until cancelled or stopped |
Scheduler.Stop() | Signal the scheduler to stop gracefully |
Scheduler.NextRun(name string) (time.Time, bool) | Return the next scheduled run time for a named job |
Scheduler.Jobs() []string | Return a copy of all registered job names |
Types:
| Type | Description |
|---|---|
Schedule | A parsed cron expression with Minutes, Hours, DaysOfMonth, Months, and DaysOfWeek fields |
Job | A scheduled job with Name, Schedule, and Handler fields |
Scheduler | Manages cron jobs with overlap prevention |
go test ./...
go vet ./...
If you find this project useful: