55
66Gron provides a clear syntax for writing and deploying cron jobs.
77
8+ ** This is a fork repo of [ github.com/roylee0704/gron] ( https://github.com/roylee0704/gron ) .**
9+
10+ Just add feature of canceling job.
11+
812## Goals
913
1014- Minimalist APIs for scheduling jobs.
@@ -15,7 +19,8 @@ Gron provides a clear syntax for writing and deploying cron jobs.
1519## Installation
1620
1721``` sh
18- $ go get github.com/roylee0704/gron
22+ # $ go get github.com/roylee0704/gron
23+ $ go get github.com/fakeYanss/gron
1924```
2025
2126## Usage
@@ -27,7 +32,8 @@ package main
2732import (
2833 " fmt"
2934 " time"
30- " github.com/roylee0704/gron"
35+ // "github.com/roylee0704/gron"
36+ " github.com/fakeYanss/gron"
3137)
3238
3339func main () {
@@ -53,7 +59,8 @@ gron.Every(1*time.Hour)
5359
5460Also support ` Day ` , ` Week ` by importing ` gron/xtime ` :
5561``` go
56- import " github.com/roylee0704/gron/xtime"
62+ // import "github.com/roylee0704/gron/xtime"
63+ import " github.com/fakeYanss/gron/xtime"
5764
5865gron.Every (1 * xtime.Day )
5966gron.Every (1 * xtime.Week )
@@ -117,7 +124,7 @@ type canceledJob struct { // implements of JobWithCancel interface
117124 id string
118125}
119126
120- func (j *canceledJob ) JobID () {
127+ func (j *canceledJob ) JobID () string {
121128 return id
122129}
123130
@@ -145,42 +152,68 @@ package main
145152
146153import (
147154 " fmt"
148- " github.com/roylee0704/gron"
149- " github.com/roylee0704/gron/xtime"
155+ " time"
156+
157+ " github.com/fakeYanss/gron"
158+ " github.com/fakeYanss/gron/xtime"
150159)
151160
152- type PrintJob struct { Msg string }
161+ type printJob struct { Msg string }
153162
154- func (p PrintJob ) Run () {
163+ func (p printJob ) Run () {
155164 fmt.Println (p.Msg )
156165}
157166
158- func main () {
167+ type canceledJob struct { // implements of JobWithCancel interface
168+ id string
169+ }
170+
171+ func (j *canceledJob ) JobID () string {
172+ return j.id
173+ }
174+
175+ func (j *canceledJob ) Run () {
176+ fmt.Printf (" job %s run\n " , j.id )
177+ }
159178
179+ func main () {
160180 var (
161- // schedules
162181 daily = gron.Every (1 * xtime.Day )
163182 weekly = gron.Every (1 * xtime.Week )
164183 monthly = gron.Every (30 * xtime.Day )
165184 yearly = gron.Every (365 * xtime.Day )
166-
167- // contrived jobs
168- purgeTask = func () { fmt.Println (" purge aged records" ) }
185+ purgeTask = func () { fmt.Println (" purge unwanted records" ) }
169186 printFoo = printJob{" Foo" }
170187 printBar = printJob{" Bar" }
171188 )
172189
173190 c := gron.New ()
174191
175- c.Add (daily.At (" 12:30" ), printFoo)
192+ c.AddFunc (gron.Every (1 *time.Hour ), func () {
193+ fmt.Println (" Every 1 hour" )
194+ })
195+ c.Start ()
196+
176197 c.AddFunc (weekly, func () { fmt.Println (" Every week" ) })
198+ c.Add (daily.At (" 12:30" ), printFoo)
177199 c.Start ()
178200
179- // Jobs may also be added to a running Gron
201+ // Jobs may also be added to a running Cron
180202 c.Add (monthly, printBar)
181203 c.AddFunc (yearly, purgeTask)
182204
183- // Stop Gron (running jobs are not halted).
184- c.Stop ()
205+ c.AddFuncWithJobID (gron.Every (1 *time.Second ), " job-id-1" , func () {
206+ fmt.Println (" job-id-1 runs every second" )
207+ })
208+ c.AddCancelingJob (gron.Every (1 *time.Second ), &canceledJob{id: " job-id-2" })
209+ c.Start ()
210+
211+ time.Sleep (5 * time.Second )
212+ c.Cancel (" job-id-1" )
213+ time.Sleep (5 * time.Second )
214+
215+ // Stop the scheduler (does not stop any jobs already running).
216+ defer c.Stop ()
185217}
218+
186219```
0 commit comments