tiny-scheduler
A tiny cron scheduler for go
Features
- Run multiple jobs concurrently
- Specify Job frequency declaratively. i.e
1s
, 3m
, 5h
, 300ms
, etc.
- Prometheus compatible metrics
- Job Timeout
- Jobs Cancellation and Scheduler cleaning up
Installation
go get github.com/adhaamehab/tiny-scheduler
Usage
// main.go
package main
import (
"fmt"
"time"
"github.com/adhaamehab/tiny-scheduler"
)
func main() {
jobs := []*Job{
NewJob("Tick1", "1s", "1s", func() {
time.Sleep(500 * time.Millisecond)
fmt.Println("Tick 1") }, make([]interface{}, 0)
),
NewJob("Tick2", "3s", "1s", func() { fmt.Println("Tick 2") }, make([]interface{}, 0)),
}
s, err := NewScheduler(C{MetricsPort: 9092})
if err != nil {
panic(err)
}
s.AddMany(jobs)
// This will block the execution
// You can use s.StartAsync() to run the scheduler in the background
s.Start()
}
Components
Scheduler
Scheduler is the core component in tiny-scheduler. It gives the user a configurable api to manage the jobs and how they run.It is composed of Executor and Jobs. User has full control over both components (explained later).
Executor
The idea of the executor is to allow scheduler to work different type of execution algorithms. This will enable users to extend the package to run as a distributed scheduler.
Job
A Job to a function is the same idea in K8s Pod & container. A Job is just a wrapper for a go function with some additional meta data about how to run this function in the background.
The downside to the current Job implementation is the ability to cancel a singe Job. Also handling the Job overlapping might be tricky.
Monitoring
tiny-scheduler expose a MetricsPort
which expose prometheus metrics. This metrics tracks the number of running jobs and executors in addition to the execution time for each individual Job.
User can shut down the MetricsServer
using StopMetricsServer()
method. But it's yet a limitation to stop that. Using configuration. This should be enhanced later.
Road Map
- Ability to cancel individual jobs.
- Ability to configure metrics server before initializing the
Scheduler
.
- More default executors. (i.e) Distributed executor, etc.
- User provided logger and log mode.
- Mocking and better test coverage