Sometimes, there is a need for a cron-like jobs runner which run jobs (commands) and waits until the job finishes before starting it again in some pre-specified interval.
The tool is under active development, don't use it in production now. The library will be ready for production in the following few days.
Installation is performed via a simple command:
go get github.com/tiabc/jobsrunner/jobsrunner
Now, specify jobs in the config file (see below) and run the application:
$GOPATH/bin/jobsrunner config.json
After launch, jobsrunner immediately starts all the specified jobs. Information about jobs schedule is not stored anywhere and execution of every job will be triggered upon restart.
Jobs are specified as follows:
{
"version": 1,
"jobs": [
{
"cmd": "yourapp check-statuses",
"interval": "5 seconds"
}
]
}
Currently, version
must be 1
and the list of jobs consists of two fields:
cmd
- a command to run.interval
- time to wait before running the command again. A positive integer and modifier (seconds
,minutes
,hours
and may also be singular) are expected. Bigger intervals are not supported as the current version does not store information about jobs schedule anywhere and triggers their execution on restart.
jobsrunner can also be used as a library, for example:
package main
import (
"context"
"log"
"github.com/tiabc/jobsrunner"
)
func main() {
r, err := jobsrunner.NewFromFile("your-config.json")
if err != nil {
log.Fatal(err)
}
// The second variable is the cancel function which can finish jobsrunner.
ctx, _ := context.WithCancel(context.Background())
// This call blocks the execution until the context is cancelled.
r.Run(ctx)
}
Contributions are welcome. Feel free to propose changes, create pull requests or request new functionality.
MIT