ryanfaerman / gronx

Lightweight, fast and dependency-free Cron expression parser (due checker) for Golang (tested on v1.13 and above)

Home Page:https://github.com/adhocore/gronx

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

adhocore/gronx

Latest Version Software License Go Report Test Donate Tweet

gronx is Golang cron expression parser ported from adhocore/cron-expr.

  • Zero dependency.
  • Very fast because it bails early in case a segment doesn't match.

Installation

go get -u github.com/adhocore/gronx

Usage

import (
	"time"

	"github.com/adhocore/gronx"
)

gron := gronx.New()
expr := "* * * * *"

// check if expr is even valid, returns bool
gron.IsValid(expr) // true

// check if expr is due for current time, returns bool and error
gron.IsDue(expr) // true|false, nil

// check if expr is due for given time
gron.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:

  • @yearly or @annually - every year
  • @monthly - every month
  • @daily - every day
  • @weekly - every week
  • @hourly - every hour
  • @5minutes - every 5 minutes
  • @10minutes - every 10 minutes
  • @15minutes - every 15 minutes
  • @30minutes - every 30 minutes
  • @always - every minute
gron.IsDue("@5minutes")

Modifiers

Following modifiers supported

  • Day of Month / 3rd segment:
    • L stands for last day of month (eg: L could mean 29th for February in leap year)
    • W stands for closest week day (eg: 10W is closest week days (MON-FRI) to 10th date)
  • Day of Week / 5th segment:
    • L stands for last weekday of month (eg: 2L is last monday)
    • # stands for nth day of week in the month (eg: 1#2 is second sunday)

License

© MIT | 2021-2099, Jitendra Adhikari

Credits

This project is ported from adhocore/cron-expr and release managed by please.

About

Lightweight, fast and dependency-free Cron expression parser (due checker) for Golang (tested on v1.13 and above)

https://github.com/adhocore/gronx

License:MIT License


Languages

Language:Go 100.0%