All Projects → adhocore → gronx

adhocore / gronx

Licence: MIT license
Lightweight, fast and dependency-free Cron expression parser (due checker), task scheduler and/or daemon for Golang (tested on v1.13 and above) and standalone usage

Programming Languages

go
31211 projects - #10 most used programming language

Projects that are alternatives of or similar to gronx

php-cron-expr
Ultra lightweight, Dependency free and Super Fast Cron Expression parser for PHP
Stars: ✭ 42 (-79.61%)
Mutual labels:  crontab, scheduler, cron-expression, cron-parser, adhocore
Jiacrontab
简单可信赖的任务管理工具
Stars: ✭ 1,052 (+410.68%)
Mutual labels:  crontab, daemon, scheduler, job-scheduler
Go Quartz
Simple, zero-dependency scheduling library for Go
Stars: ✭ 118 (-42.72%)
Mutual labels:  crontab, scheduler, job-scheduler
crontab
cron expression parser and executor for dotnet core.
Stars: ✭ 13 (-93.69%)
Mutual labels:  crontab, cron-expression, task-scheduler
scheduler
Task Scheduler for Laravel applications. UI from scratch
Stars: ✭ 18 (-91.26%)
Mutual labels:  scheduler, task-manager, task-scheduler
Db Scheduler
Persistent cluster-friendly scheduler for Java
Stars: ✭ 352 (+70.87%)
Mutual labels:  scheduler, job-scheduler, task-scheduler
Openmangosteen
Devops定时调用http接口,定时执行SSH命令的WEB定时任务工具。
Stars: ✭ 41 (-80.1%)
Mutual labels:  scheduler, task-manager, task-scheduler
Cron Manager
A PHP cron task manager for MVC-type applications
Stars: ✭ 396 (+92.23%)
Mutual labels:  crontab, task-manager, task-scheduler
Bulwark Explorer
Block explorer for Bulwark Cryptocurrency
Stars: ✭ 57 (-72.33%)
Mutual labels:  crontab, daemon
Ppgo job
PPGo_Job是一款可视化的、多人多权限的、一任务多机执行的定时任务管理系统,采用golang开发,安装方便,资源消耗少,支持大并发,可同时管理多台服务器上的定时任务。
Stars: ✭ 1,152 (+459.22%)
Mutual labels:  crontab, job-scheduler
go-xxl-job-client
xxl-job go client
Stars: ✭ 36 (-82.52%)
Mutual labels:  scheduler, job-scheduler
Cronsun
A Distributed, Fault-Tolerant Cron-Style Job System.
Stars: ✭ 2,493 (+1110.19%)
Mutual labels:  crontab, job-scheduler
Cronicle
A simple, distributed task scheduler and runner with a web based UI.
Stars: ✭ 979 (+375.24%)
Mutual labels:  crontab, scheduler
Quantum Core
⌚ Cron-like job scheduler for Elixir
Stars: ✭ 1,905 (+824.76%)
Mutual labels:  crontab, scheduler
xxl-job-executor-go
xxl-job 执行器(golang 客户端)
Stars: ✭ 298 (+44.66%)
Mutual labels:  crontab, cronjob
Routine
go routine control, abstraction of the Main and some useful Executors.如果你不会管理Goroutine的话,用它
Stars: ✭ 40 (-80.58%)
Mutual labels:  crontab, job-scheduler
Ects
Elastic Crontab System 简单易用的分布式定时任务管理系统
Stars: ✭ 156 (-24.27%)
Mutual labels:  crontab, scheduler
ptScheduler
Pretty tiny Scheduler or ptScheduler is an Arduino library for writing non-blocking periodic tasks easily.
Stars: ✭ 14 (-93.2%)
Mutual labels:  scheduler, task-scheduler
croner
Trigger functions and/or evaluate cron expressions in JavaScript. No dependencies. Most features. All environments.
Stars: ✭ 169 (-17.96%)
Mutual labels:  crontab, scheduler
time.clj
time util for Clojure(Script)
Stars: ✭ 45 (-78.16%)
Mutual labels:  scheduler, cronjob

adhocore/gronx

Latest Version Software License Go Report Test Codecov Donate 15 Donate 25 Donate 50 Tweet

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.

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

Find gronx in pkg.go.dev.

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.

Check the section below for more sophisticated way of managing tasks automatically using gronx daemon called tasker.


Go Tasker

Tasker is a task manager that can be programatically used in Golang applications. It runs as a daemon and and invokes tasks scheduled with cron expression:

package main

import (
	"context"
	"time"

	"github.com/adhocore/gronx/pkg/tasker"
)

func main() {
	taskr := tasker.New(tasker.Option{
		Verbose: true,
		// optional: defaults to local
		Tz:      "Asia/Bangkok",
		// optional: defaults to stderr log stream
		Out:     "/full/path/to/output-file",
	})

	// add task to run every minute
	taskr.Task("* * * * *", func(ctx context.Context) (int, error) {
		// do something ...

		// then return exit code and error, for eg: if everything okay
		return 0, nil
	}).Task("*/5 * * * *", func(ctx context.Context) (int, error) { // every 5 minutes
		// you can also log the output to Out file as configured in Option above:
		taskr.Log.Printf("done something in %d s", 2)

		return 0, nil
	})

	// every 10 minute with arbitrary command
	taskr.Task("@10minutes", taskr.Taskify("command --option val -- args"))

	// ... add more tasks

	// optionally if you want tasker to stop after 2 hour, pass the duration with Until():
	taskr.Until(2 * time.Hour)

	// finally run the tasker, it ticks sharply on every minute and runs all the tasks due on that time!
	// it exits gracefully when ctrl+c is received making sure pending tasks are completed.
	taskr.Run()
}

Task Daemon

It can also be used as standalone task daemon instead of programmatic usage for Golang application.

First, just install tasker command:

go get -u github.com/adhocore/gronx/cmd/tasker

Then prepare a taskfile (example) in crontab format (or can even point to existing crontab).

user is not supported: it is just cron expr followed by the command.

Finally run the task daemon like so

tasker -file path/to/taskfile

You can pass more options to control the behavior of task daemon, see below.

Tasker command options:

-file string <required>
    The task file in crontab format
-out string
    The fullpath to file where output from tasks are sent to
-shell string
    The shell to use for running tasks (default "/usr/bin/bash")
-tz string
    The timezone to use for tasks (default "Local")
-until int
    The timeout for task daemon in minutes
-verbose
    The verbose mode outputs as much as possible

Examples:

tasker -verbose -file path/to/taskfile -until 120 # run until next 120min (i.e 2hour) with all feedbacks echoed back
tasker -verbose -file path/to/taskfile -out path/to/output # with all feedbacks echoed to the output file
tasker -tz America/New_York -file path/to/taskfile -shell zsh # run all tasks using zsh shell based on NY timezone

File extension of taskfile for (-file option) does not matter: can be any or none. The directory for outfile (-out option) must exist, file is created by task daemon.

Same timezone applies for all tasks currently and it might support overriding timezone per task in future release.

Notes on Windows

In Windows if it doesn't find bash.exe or git-bash.exe it will use powershell. powershell may not be compatible with Unix flavored commands. Also to note: you can't do chaining with cmd1 && cmd2 but rather cmd1 ; cmd2.


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.


Other projects

My other golang projects you might find interesting and useful:

  • urlsh - URL shortener and bookmarker service with UI, API, Cache, Hits Counter and forwarder using postgres and redis in backend, bulma in frontend; has web and cli client
  • fast - Check your internet speed with ease and comfort right from the terminal
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].