A easy to use but yet powerfull job schedueler written in Go
The basic concept here is the Cronnie connects to a postgres table which it manages itself. by the magic of postgres we can subscribe to the changes on this table, so if a new job is created Cronnie figures out what to do and when to do it
most of the database specific handling is in the db file if you want to deep dive into it. the idea is that Cronnie is fully event based so there is no polling in this code
As with any golang package you can just use the go get
command.
go get github.com/xantios/cronnie
For a full code example see example/main.go
Cronie heavily relies on the concept of a JobMap. this is simple a map[string]cronnie.Job
a cronnie.Job is a function which excepts a map[string]string
A quick example
package main
func ExampleJobHandler(arg map[string]string) bool {
// Example
return true
}
func main() {
exampleMap := map[string]Cronnie.Job{
"example": ExampleJobHandler,
},
}
A simple config can be created using the following snippet
package main
import Cronnie "github.com/xantios/cronnie"
func main() {
config := &Cronnie.Config{
Uri: "host=db user=MY_USER password=PASSWORD dbname=app port=5432 sslmode=disable",
JobMap: map[string]Cronnie.Job{
"example": ExampleJobHandler,
},
Logger: logger,
}
}
Create a instance to work with, if you have a config this is easy.
cron, err := Cronnie.New(config)
To add a task, take the reference created above (or any instance for that matter) and take a look at the following example code. a more involved example can be found in the example directory
cron.Create("example", map[string]string{
"arg_1": "This is the first argument",
"arg_2": "This is the second argument",
}, time.Time{})
This is where the actual magic happens!
Just simply run the Run()
function should suffice.
e := cron.Run()