Lightweight, fast and dependency-free Cron expression parser (due checker, next/prev due date finder), task runner, job scheduler and/or daemon for Golang (tested on v1.13+) and standalone usage. If you are bold, use it to replace crontab entirely.
adhocore/gronx
gronx is Golang cron expression parser ported from adhocore/cron-expr with task runner
and daemon that supports crontab like task list file. Use it programatically in Golang or as standalone binary instead of crond. If that's not enough, you can use gronx to find the next (NextTick()) or previous (PrevTick()) run time of an expression from any arbitrary point of time.
Zero dependency.
Very fast because it bails early in case a segment doesn't match.
import (
"time""github.com/adhocore/gronx"
)
gron:=gronx.New()
expr:="* * * * *"// check if expr is even valid, returns boolgron.IsValid(expr) // true// check if expr is due for current time, returns bool and errorgron.
import("time""github.com/adhocore/gronx")gron:=gronx.New()expr:="* * * * *"// check if expr is even valid, returns boolgron.IsValid(expr)// true// check if expr is due for current time, returns bool and errorgron.IsDue(expr)// true|false, nil// check if expr is due for given timegron.IsDue(expr,time.Date(2021,time.April,1,1,1,0,0,time.UTC))// true|false, nil
In a more practical level, you would use this tool to manage and invoke jobs in app itself and not
mess around with crontab for each and every new tasks/jobs. It doesn't yet replace that but rather supplements it.
There is a plan though #1.
In crontab just put one entry with * * * * * which points to your Go entry point that uses this tool.
Then in that entry point you would invoke different tasks if the corresponding Cron expr is due.
Simple map structure would work for this.
Cron Expression
Cron expression normally consists of 5 segments viz:
<minute> <hour> <day> <month> <weekday>
and sometimes there can be 6th segment for <year> at the end.
For each segments you can have multiple choices separated by comma:
Eg: 0,30 * * * * means either 0th or 30th minute.
To specify range of values you can use dash:
Eg: 10-15 * * * * means 10th, 11th, 12th, 13th, 14th and 15th minute.
To specify range of step you can combine a dash and slash:
Eg: 10-15/2 * * * * means every 2 minutes between 10 and 15 i.e 10th, 12th and 14th minute.
For the 3rd and 5th segment, there are additional modifiers (optional).
And if you want, you can mix them up:
5,12-20/4,55 * * * * matches if any one of 5 or 12-20/4 or 55 matches the minute.
Real Abbreviations
You can use real abbreviations for month and week days. eg: JAN, dec, fri, SUN
Tags
Following tags are available and they are converted to real cron expressions before parsing: