Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/xgfone/gron
Another job periodic runner like crontab.
https://github.com/xgfone/gron
cron cron-job cronjob crontab go-cron go-crontab go-job go-task job job-scheduler scheduler task
Last synced: 19 days ago
JSON representation
Another job periodic runner like crontab.
- Host: GitHub
- URL: https://github.com/xgfone/gron
- Owner: xgfone
- License: apache-2.0
- Created: 2019-07-01T12:54:07.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2021-10-17T14:16:18.000Z (about 3 years ago)
- Last Synced: 2024-10-30T20:49:15.688Z (about 2 months ago)
- Topics: cron, cron-job, cronjob, crontab, go-cron, go-crontab, go-job, go-task, job, job-scheduler, scheduler, task
- Language: Go
- Size: 67.4 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# gron [![Build Status](https://github.com/xgfone/gron/actions/workflows/go.yml/badge.svg)](https://github.com/xgfone/gron/actions/workflows/go.yml) [![GoDoc](https://pkg.go.dev/badge/github.com/xgfone/gron)](https://pkg.go.dev/github.com/xgfone/gron) [![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg?style=flat-square)](https://raw.githubusercontent.com/xgfone/gron/master/LICENSE)
Another job periodic runner like `crontab` supporting `Go1.7+`
## Example
```go
package mainimport (
"context"
"fmt"
"time""github.com/xgfone/gron"
)func jobRunner(name string) gron.Runner {
return func(c context.Context, now time.Time) (result interface{}, err error) {
fmt.Printf("Starting to run job '%s' at '%s'\n", name, now.Format(time.RFC3339Nano))
return
}
}func jobResultHook(result gron.JobResult) {
fmt.Printf("End to run job '%s', cost '%s'.\n", result.Job.Name(), result.Cost)
}func main() {
exe := gron.NewExecutor()
exe.AppendResultHooks(jobResultHook) // Add the job result hook
exe.Start() // Start the executor in the background goroutine.// Add jobs
exe.Schedule("job1", gron.Every(time.Minute), jobRunner("job1"))
exe.Schedule("job2", gron.MustParseWhen("@every 2m"), jobRunner("job2"))
everyMinuteScheduler := gron.MustParseWhen("*/1 * * * *")
exe.ScheduleJob(gron.NewJob("job3", everyMinuteScheduler, jobRunner("job3")))go func() {
time.Sleep(time.Minute * 4)
// exe.CancelJobs("job1", "job2", "job3")
exe.Stop()
}()// Wait until the executor is stopped.
exe.Wait()
}
```