cronalt is a Golang job scheduling package that allows you to run jobs at a given interval.
- Scheduler runs in-process
- Runtime safety (avoids reflection)
- Pluggable and extensible
cronalt is inspired by gocron
Install using go modules
go get github.com/ahmedalhulaibi/cronalt@v1.0.0
See internal/examples/basic
directory for a simple example
The job is simply aware of the task it's provided. It is defined as an interface. Jobs can be extended/wrapped using a decorator to extend functionality.
A Job Timer informs the Scheduler how long to wait before triggering the next run for a given Job. The Job Timer is also defined as an interface to allow for irregular scheduling.
The Scheduler orchestrates all Jobs. It starts all the jobs and stops all the jobs. For each job, an individual goroutine is kicked off with its Job Timer informing the routine how the job should be scheduled.
Decorate your job with a locker implementation.
See internal/examples/redsync
for an example.
Please beware that using Redis for locks as demonstrated in the example can lead to unintended consequences. This example is not tested in production. If you're worried about race conditions, consider redesigning your process such that it does not require a distributed lock.
Additional reading:
- How to do distributed locking by Martin Kleppmann
- Is Redlock safe? by Salvatore Sanfilippo
- redlock: unsafe at any time by Alisdair Sullivan
Decorate your job with an error handler implementation.
See internal/examples/errorhandler
for an example.
Decorate all your jobs with a circuit breaker which cancels the parent context. This will stop the entire process, not just the individual routine.
See internal/examples/circuitbreaker
for an example
Decorate your job with a context decorator.
See internal/examples/runid
for an example
This is not supported out of the box.
See internal/examples/dynamicscheduling
for an example of a dynamic scheduler built around cronalt.Scheduler
.
Decorate your job with a counter. See extensions/counter/counter.go
for the job decorator.