项目作者: roylee0704

项目描述 :
gron,Cron Jobs在Go。
高级语言: Go
项目地址: git://github.com/roylee0704/gron.git
创建时间: 2016-06-04T08:02:22Z
项目社区:https://github.com/roylee0704/gron

开源协议:MIT License

下载


gron

Build Status
Go Report Card
GoDoc

Gron provides a clear syntax for writing and deploying cron jobs.

Goals

  • Minimalist APIs for scheduling jobs.
  • Thread safety.
  • Customizable Job Type.
  • Customizable Schedule.

Installation

  1. $ go get github.com/roylee0704/gron

Usage

Create schedule.go

  1. package main
  2. import (
  3. "fmt"
  4. "time"
  5. "github.com/roylee0704/gron"
  6. )
  7. func main() {
  8. c := gron.New()
  9. c.AddFunc(gron.Every(1*time.Hour), func() {
  10. fmt.Println("runs every hour.")
  11. })
  12. c.Start()
  13. }

Schedule Parameters

All scheduling is done in the machine’s local time zone (as provided by the Go time package).

Setup basic periodic schedule with gron.Every().

  1. gron.Every(1*time.Second)
  2. gron.Every(1*time.Minute)
  3. gron.Every(1*time.Hour)

Also support Day, Week by importing gron/xtime:

  1. import "github.com/roylee0704/gron/xtime"
  2. gron.Every(1 * xtime.Day)
  3. gron.Every(1 * xtime.Week)

Schedule to run at specific time with .At(hh:mm)

  1. gron.Every(30 * xtime.Day).At("00:00")
  2. gron.Every(1 * xtime.Week).At("23:59")

Custom Job Type

You may define custom job types by implementing gron.Job interface: Run().

For example:

  1. type Reminder struct {
  2. Msg string
  3. }
  4. func (r Reminder) Run() {
  5. fmt.Println(r.Msg)
  6. }

After job has defined, instantiate it and schedule to run in Gron.

  1. c := gron.New()
  2. r := Reminder{ "Feed the baby!" }
  3. c.Add(gron.Every(8*time.Hour), r)
  4. c.Start()

Custom Job Func

You may register Funcs to be executed on a given schedule. Gron will run them in their own goroutines, asynchronously.

  1. c := gron.New()
  2. c.AddFunc(gron.Every(1*time.Second), func() {
  3. fmt.Println("runs every second")
  4. })
  5. c.Start()

Custom Schedule

Schedule is the interface that wraps the basic Next method: Next(p time.Duration) time.Time

In gron, the interface value Schedule has the following concrete types:

  • periodicSchedule. adds time instant t to underlying period p.
  • atSchedule. reoccurs every period p, at time components(hh:mm).

For more info, checkout schedule.go.

Full Example

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/roylee0704/gron"
  5. "github.com/roylee0704/gron/xtime"
  6. )
  7. type PrintJob struct{ Msg string }
  8. func (p PrintJob) Run() {
  9. fmt.Println(p.Msg)
  10. }
  11. func main() {
  12. var (
  13. // schedules
  14. daily = gron.Every(1 * xtime.Day)
  15. weekly = gron.Every(1 * xtime.Week)
  16. monthly = gron.Every(30 * xtime.Day)
  17. yearly = gron.Every(365 * xtime.Day)
  18. // contrived jobs
  19. purgeTask = func() { fmt.Println("purge aged records") }
  20. printFoo = printJob{"Foo"}
  21. printBar = printJob{"Bar"}
  22. )
  23. c := gron.New()
  24. c.Add(daily.At("12:30"), printFoo)
  25. c.AddFunc(weekly, func() { fmt.Println("Every week") })
  26. c.Start()
  27. // Jobs may also be added to a running Gron
  28. c.Add(monthly, printBar)
  29. c.AddFunc(yearly, purgeTask)
  30. // Stop Gron (running jobs are not halted).
  31. c.Stop()
  32. }