forked from jasonlvhit/gocron
-
Notifications
You must be signed in to change notification settings - Fork 7
/
gocron.go
526 lines (454 loc) · 11 KB
/
gocron.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
// goCron : A Golang Job Scheduling Package.
//
// An in-process scheduler for periodic jobs that uses the builder pattern
// for configuration. Schedule lets you run Golang functions periodically
// at pre-determined intervals using a simple, human-friendly syntax.
//
// Inspired by the Ruby module clockwork <https://github.com/tomykaira/clockwork>
// and
// Python package schedule <https://github.com/dbader/schedule>
//
// See also
// http://adam.heroku.com/past/2010/4/13/rethinking_cron/
// http://adam.heroku.com/past/2010/6/30/replace_cron_with_clockwork/
//
// Copyright 2014 Jason Lyu. [email protected] .
// All rights reserved.
// Use of this source code is governed by a BSD-style .
// license that can be found in the LICENSE file.
package gocron
import (
"errors"
"reflect"
"runtime"
"sort"
"time"
)
// Time location, default set by the time.Local (*time.Location)
var loc = time.Local
// Change the time location
func ChangeLoc(newLocation *time.Location) {
loc = newLocation
}
// Max number of jobs, hack it if you need.
const MAXJOBNUM = 10000
// Map for the function task store
var funcs = map[string]interface{}{}
// Map for function and params of function
var fparams = map[string]([]interface{}){}
type Job struct {
// pause interval * unit bettween runs
interval uint64
// the job jobFunc to run, func[jobFunc]
jobFunc string
// time units, ,e.g. 'minutes', 'hours'...
unit string
// optional time at which this job runs
atTime string
// datetime of last run
lastRun time.Time
// datetime of next run
nextRun time.Time
// cache the period between last an next run
period time.Duration
// Specific day of the week to start on
startDay time.Weekday
}
// Create a new job with the time interval.
func NewJob(intervel uint64) *Job {
return &Job{intervel, "", "", "", time.Unix(0, 0), time.Unix(0, 0), 0, time.Sunday}
}
// True if the job should be run now
func (j *Job) shouldRun() bool {
return time.Now().After(j.nextRun)
}
//Run the job and immdiately reschedulei it
func (j *Job) run() (result []reflect.Value, err error) {
f := reflect.ValueOf(funcs[j.jobFunc])
params := fparams[j.jobFunc]
if len(params) != f.Type().NumIn() {
err = errors.New("The number of param is not adapted.")
return
}
in := make([]reflect.Value, len(params))
for k, param := range params {
in[k] = reflect.ValueOf(param)
}
result = f.Call(in)
j.lastRun = time.Now()
j.scheduleNextRun()
return
}
// for given function fn , get the name of funciton.
func getFunctionName(fn interface{}) string {
return runtime.FuncForPC(reflect.ValueOf((fn)).Pointer()).Name()
}
// Specifies the jobFunc that should be called every time the job runs
//
func (j *Job) Do(jobFun interface{}, params ...interface{}) {
typ := reflect.TypeOf(jobFun)
if typ.Kind() != reflect.Func {
panic("only function can be schedule into the job queue.")
}
fname := getFunctionName(jobFun)
funcs[fname] = jobFun
fparams[fname] = params
j.jobFunc = fname
//schedule the next run
j.scheduleNextRun()
}
// s.Every(1).Day().At("10:30").Do(task)
// s.Every(1).Monday().At("10:30").Do(task)
func (j *Job) At(t string) *Job {
hour := int((t[0]-'0')*10 + (t[1] - '0'))
min := int((t[3]-'0')*10 + (t[4] - '0'))
if hour < 0 || hour > 23 || min < 0 || min > 59 {
panic("time format error.")
}
// time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC)
mock := time.Date(time.Now().Year(), time.Now().Month(), time.Now().Day(), int(hour), int(min), 0, 0, loc)
if j.unit == "days" {
if time.Now().After(mock) {
j.lastRun = mock
} else {
j.lastRun = time.Date(time.Now().Year(), time.Now().Month(), time.Now().Day()-1, hour, min, 0, 0, loc)
}
} else if j.unit == "weeks" {
if time.Now().After(mock) {
i := mock.Weekday() - j.startDay
if i < 0 {
i = 7 + i
}
j.lastRun = time.Date(time.Now().Year(), time.Now().Month(), time.Now().Day()-int(i), hour, min, 0, 0, loc)
} else {
j.lastRun = time.Date(time.Now().Year(), time.Now().Month(), time.Now().Day()-7, hour, min, 0, 0, loc)
}
}
return j
}
//Compute the instant when this job should run next
func (j *Job) scheduleNextRun() {
if j.lastRun == time.Unix(0, 0) {
if j.unit == "weeks" {
i := time.Now().Weekday() - j.startDay
if i < 0 {
i = 7 + i
}
j.lastRun = time.Date(time.Now().Year(), time.Now().Month(), time.Now().Day()-int(i), 0, 0, 0, 0, loc)
} else {
j.lastRun = time.Now()
}
}
if j.period != 0 {
// translate all the units to the Seconds
j.nextRun = j.lastRun.Add(j.period * time.Second)
} else {
switch j.unit {
case "minutes":
j.period = time.Duration(j.interval * 60)
break
case "hours":
j.period = time.Duration(j.interval * 60 * 60)
break
case "days":
j.period = time.Duration(j.interval * 60 * 60 * 24)
break
case "weeks":
j.period = time.Duration(j.interval * 60 * 60 * 24 * 7)
break
case "seconds":
j.period = time.Duration(j.interval)
}
j.nextRun = j.lastRun.Add(j.period * time.Second)
}
}
// the follow functions set the job's unit with seconds,minutes,hours...
// Set the unit with second
func (j *Job) Second() (job *Job) {
if j.interval != 1 {
panic("")
}
job = j.Seconds()
return
}
// Set the unit with seconds
func (j *Job) Seconds() (job *Job) {
j.unit = "seconds"
return j
}
// Set the unit with minute, which interval is 1
func (j *Job) Minute() (job *Job) {
if j.interval != 1 {
panic("")
}
job = j.Minutes()
return
}
//set the unit with minute
func (j *Job) Minutes() (job *Job) {
j.unit = "minutes"
return j
}
//set the unit with hour, which interval is 1
func (j *Job) Hour() (job *Job) {
if j.interval != 1 {
panic("")
}
job = j.Hours()
return
}
// Set the unit with hours
func (j *Job) Hours() (job *Job) {
j.unit = "hours"
return j
}
// Set the job's unit with day, which interval is 1
func (j *Job) Day() (job *Job) {
if j.interval != 1 {
panic("")
}
job = j.Days()
return
}
// Set the job's unit with days
func (j *Job) Days() *Job {
j.unit = "days"
return j
}
/*
// Set the unit with week, which the interval is 1
func (j *Job) Week() (job *Job) {
if j.interval != 1 {
panic("")
}
job = j.Weeks()
return
}
*/
// s.Every(1).Monday().Do(task)
// Set the start day with Monday
func (j *Job) Monday() (job *Job) {
if j.interval != 1 {
panic("")
}
j.startDay = 1
job = j.Weeks()
return
}
// Set the start day with Tuesday
func (j *Job) Tuesday() (job *Job) {
if j.interval != 1 {
panic("")
}
j.startDay = 2
job = j.Weeks()
return
}
// Set the start day woth Wednesday
func (j *Job) Wednesday() (job *Job) {
if j.interval != 1 {
panic("")
}
j.startDay = 3
job = j.Weeks()
return
}
// Set the start day with thursday
func (j *Job) Thursday() (job *Job) {
if j.interval != 1 {
panic("")
}
j.startDay = 4
job = j.Weeks()
return
}
// Set the start day with friday
func (j *Job) Friday() (job *Job) {
if j.interval != 1 {
panic("")
}
j.startDay = 5
job = j.Weeks()
return
}
// Set the start day with saturday
func (j *Job) Saturday() (job *Job) {
if j.interval != 1 {
panic("")
}
j.startDay = 6
job = j.Weeks()
return
}
// Set the start day with sunday
func (j *Job) Sunday() (job *Job) {
if j.interval != 1 {
panic("")
}
j.startDay = 0
job = j.Weeks()
return
}
//Set the units as weeks
func (j *Job) Weeks() *Job {
j.unit = "weeks"
return j
}
// Class Scheduler, the only data member is the list of jobs.
type Scheduler struct {
// Array store jobs
jobs [MAXJOBNUM]*Job
// Size of jobs which jobs holding.
size int
}
// Scheduler implements the sort.Interface{} for sorting jobs, by the time nextRun
func (s *Scheduler) Len() int {
return s.size
}
func (s *Scheduler) Swap(i, j int) {
s.jobs[i], s.jobs[j] = s.jobs[j], s.jobs[i]
}
func (s *Scheduler) Less(i, j int) bool {
return s.jobs[j].nextRun.After(s.jobs[i].nextRun)
}
// Create a new scheduler
func NewScheduler() *Scheduler {
return &Scheduler{[MAXJOBNUM]*Job{}, 0}
}
// Get the current runnable jobs, which shouldRun is True
func (s *Scheduler) getRunnableJobs() (running_jobs [MAXJOBNUM]*Job, n int) {
runnableJobs := [MAXJOBNUM]*Job{}
n = 0
sort.Sort(s)
for i := 0; i < s.size; i++ {
if s.jobs[i].shouldRun() {
runnableJobs[n] = s.jobs[i]
//fmt.Println(runnableJobs)
n++
} else {
break
}
}
return runnableJobs, n
}
// Datetime when the next job should run.
func (s *Scheduler) NextRun() (*Job, time.Time) {
if s.size <= 0 {
return nil, time.Now()
}
sort.Sort(s)
return s.jobs[0], s.jobs[0].nextRun
}
// Schedule a new periodic job
func (s *Scheduler) Every(interval uint64) *Job {
job := NewJob(interval)
s.jobs[s.size] = job
s.size++
return job
}
// Run all the jobs that are scheduled to run.
func (s *Scheduler) RunPending() {
runnableJobs, n := s.getRunnableJobs()
if n != 0 {
for i := 0; i < n; i++ {
runnableJobs[i].run()
}
}
}
// Run all jobs regardless if they are scheduled to run or not
func (s *Scheduler) RunAll() {
for i := 0; i < s.size; i++ {
s.jobs[i].run()
}
}
// Run all jobs with delay seconds
func (s *Scheduler) RunAllwithDelay(d int) {
for i := 0; i < s.size; i++ {
s.jobs[i].run()
time.Sleep(time.Duration(d))
}
}
// Remove specific job j
func (s *Scheduler) Remove(j interface{}) {
i := 0
for ; i < s.size; i++ {
if s.jobs[i].jobFunc == getFunctionName(j) {
break
}
}
for j := (i + 1); j < s.size; j++ {
s.jobs[i] = s.jobs[j]
i++
}
s.size = s.size - 1
}
// Delete all scheduled jobs
func (s *Scheduler) Clear() {
for i := 0; i < s.size; i++ {
s.jobs[i] = nil
}
s.size = 0
}
// Start all the pending jobs
// Add seconds ticker
func (s *Scheduler) Start() chan bool {
stopped := make(chan bool, 1)
ticker := time.NewTicker(1 * time.Second)
go func() {
for {
select {
case <-ticker.C:
s.RunPending()
case <-stopped:
return
}
}
}()
return stopped
}
// The following methods are shortcuts for not having to
// create a Schduler instance
var defaultScheduler = NewScheduler()
var jobs = defaultScheduler.jobs
// Schedule a new periodic job
func Every(interval uint64) *Job {
return defaultScheduler.Every(interval)
}
// Run all jobs that are scheduled to run
//
// Please note that it is *intended behavior that run_pending()
// does not run missed jobs*. For example, if you've registered a job
// that should run every minute and you only call run_pending()
// in one hour increments then your job won't be run 60 times in
// between but only once.
func RunPending() {
defaultScheduler.RunPending()
}
// Run all jobs regardless if they are scheduled to run or not.
func RunAll() {
defaultScheduler.RunAll()
}
// Run all the jobs with a delay in seconds
//
// A delay of `delay` seconds is added between each job. This can help
// to distribute the system load generated by the jobs more evenly over
// time.
func RunAllwithDelay(d int) {
defaultScheduler.RunAllwithDelay(d)
}
// Run all jobs that are scheduled to run
func Start() chan bool {
return defaultScheduler.Start()
}
// Clear
func Clear() {
defaultScheduler.Clear()
}
// Remove
func Remove(j interface{}) {
defaultScheduler.Remove(j)
}
// NextRun gets the next running time
func NextRun() (job *Job, time time.Time) {
return defaultScheduler.NextRun()
}