forked from gljubojevic/gocron
-
Notifications
You must be signed in to change notification settings - Fork 11
/
gocron.go
642 lines (550 loc) · 14.9 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
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
// Package 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"
"fmt"
"reflect"
"runtime"
"sort"
"strconv"
"strings"
"sync"
"time"
// perferencing a really stable redis client over exposing a redis interface
"github.com/go-redis/redis"
)
// globals
var (
// Time location, default set by the time.Local (*time.Location)
loc = time.Local
ErrTimeFormat = errors.New("time format error")
ErrParamsNotAdapted = errors.New("the number of params is not adapted")
ErrNotAFunction = errors.New("only functions can be schedule into the job queue")
ErrPeriodNotSpecified = errors.New("unspecified job period")
ErrParameterCannotBeNil = errors.New("nil paramaters cannot be used with reflection")
)
const (
seconds = "seconds"
minutes = "minutes"
hours = "hours"
days = "days"
weeks = "weeks"
)
const (
redisKey = "gocron:distributed:job:"
)
// ChangeLoc change default the time location
func ChangeLoc(newLocation *time.Location) {
loc = newLocation
}
// Job struct keeping information about job
type Job struct {
ShouldDoImmediately bool // indicates that jobs should start before scheduling
DistributedRedisClient *redis.Client // if the client is passed in the scheduler will check the redis client before running a job to corridinate with a distributed system
DistributedJobName string // name assigned to the distributed job, if empty and more than one job is running a redis name collusion will occur
mu *sync.Mutex
interval uint64 // pause interval * unit bettween runs
jobFunc string // the job jobFunc to run, func[jobFunc]
unit string // time units, ,e.g. 'minutes', 'hours'...
atTime time.Duration // optional time at which this job runs
lastRun time.Time // datetime of last run
nextRun time.Time // datetime of next run
startDay time.Weekday // Specific day of the week to start on
funcs map[string]interface{} // Map for the function task store
fparams map[string]([]interface{}) // Map for function and params of function
err error
}
// NewJob creates a new job with the time interval.
func NewJob(interval uint64, options ...func(*Job)) *Job {
j := &Job{
mu: new(sync.Mutex),
interval: interval,
jobFunc: "",
unit: "",
atTime: 0,
lastRun: time.Unix(0, 0),
nextRun: time.Unix(0, 0),
startDay: time.Sunday,
funcs: make(map[string]interface{}),
fparams: make(map[string]([]interface{})),
}
for _, option := range options {
option(j)
}
return j
}
// True if the job should be run now
func (j *Job) shouldRun() bool {
j.mu.Lock()
b := time.Now().After(j.nextRun)
j.mu.Unlock()
if j.DistributedRedisClient != nil && b {
go func() {
time.Sleep(time.Duration(j.interval*8) * time.Second)
j.DistributedRedisClient.SAdd(redisKey+j.DistributedJobName, "added")
}()
}
return b
}
// Run the job and immdiately reschedule it
func (j *Job) run() ([]reflect.Value, error) {
f := reflect.ValueOf(j.funcs[j.jobFunc])
params := j.fparams[j.jobFunc]
if len(params) != f.Type().NumIn() {
return nil, ErrParamsNotAdapted
}
var result []reflect.Value
if j.ShouldDoImmediately {
in := make([]reflect.Value, len(params))
for k, param := range params {
// should check for nil items to avoid a panic
if param == nil {
return nil, ErrParameterCannotBeNil
}
in[k] = reflect.ValueOf(param)
}
j.mu.Lock()
result = f.Call(in)
j.mu.Unlock()
}
j.mu.Lock()
j.lastRun = time.Now()
j.mu.Unlock()
err := j.scheduleNextRun(true)
if err != nil {
return result, err
}
return result, nil
}
// for given function fn , get the name of funciton.
func getFunctionName(fn interface{}) string {
return runtime.FuncForPC(reflect.ValueOf((fn)).Pointer()).Name()
}
// Err should be checked to ensure an error didn't occur creating the job
func (j *Job) Err() error {
return j.err
}
// Do specifies the jobFunc that should be called every time the job runs
func (j *Job) Do(jobFun interface{}, params ...interface{}) error {
if j.err != nil {
return j.err
}
typ := reflect.TypeOf(jobFun)
if typ.Kind() != reflect.Func {
return ErrNotAFunction
}
fname := getFunctionName(jobFun)
j.mu.Lock()
j.funcs[fname] = jobFun
j.fparams[fname] = params
j.jobFunc = fname
j.mu.Unlock()
j.scheduleNextRun(true)
return nil
}
func formatTime(t string) (int, int, error) {
var hour, min int
ts := strings.Split(t, ":")
if len(ts) != 2 {
return hour, min, ErrTimeFormat
}
var err error
if hour, err = strconv.Atoi(ts[0]); err != nil {
return hour, min, err
}
if min, err = strconv.Atoi(ts[1]); err != nil {
return hour, min, err
}
if hour < 0 || hour > 23 || min < 0 || min > 59 {
return hour, min, ErrTimeFormat
}
return hour, min, nil
}
// At schedules job at specific time of day
// 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, min, err := formatTime(t)
if err != nil {
j.err = err
return j
}
// save atTime start as duration from midnight
j.atTime = time.Duration(hour)*time.Hour + time.Duration(min)*time.Minute
return j
}
func (j *Job) periodDuration() (time.Duration, error) {
interval := time.Duration(j.interval)
switch j.unit {
case seconds:
return time.Duration(interval * time.Second), nil
case minutes:
return time.Duration(interval * time.Minute), nil
case hours:
return time.Duration(interval * time.Hour), nil
case days:
return time.Duration(interval * time.Hour * 24), nil
case weeks:
return time.Duration(interval * time.Hour * 24 * 7), nil
}
return interval, ErrPeriodNotSpecified
}
// roundToMidnight truncate time to midnight
func (j *Job) roundToMidnight(t time.Time) time.Time {
return time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, loc)
}
// scheduleNextRun Compute the instant when this job should run next
func (j *Job) scheduleNextRun(running bool) error {
now := time.Now()
if j.lastRun == time.Unix(0, 0) {
j.mu.Lock()
j.lastRun = now
j.mu.Unlock()
}
switch j.unit {
case days:
j.mu.Lock()
j.nextRun = j.roundToMidnight(j.lastRun)
j.nextRun = j.nextRun.Add(j.atTime)
j.mu.Unlock()
case weeks:
j.mu.Lock()
j.nextRun = j.roundToMidnight(j.lastRun)
dayDiff := int(j.startDay)
dayDiff -= int(j.nextRun.Weekday())
if dayDiff != 0 {
j.nextRun = j.nextRun.Add(time.Duration(dayDiff) * 24 * time.Hour)
}
j.nextRun = j.nextRun.Add(j.atTime)
j.mu.Unlock()
default:
j.mu.Lock()
j.nextRun = j.lastRun
j.mu.Unlock()
}
period, err := j.periodDuration()
if err != nil {
return err
}
j.ShouldDoImmediately = true
// advance to next possible schedule
for j.nextRun.Before(now) || j.nextRun.Before(j.lastRun) {
j.mu.Lock()
j.nextRun = j.nextRun.Add(period)
j.mu.Unlock()
}
return nil
}
// NextScheduledTime returns the time of when this job is to run next
func (j *Job) NextScheduledTime() time.Time {
j.mu.Lock()
next := j.nextRun
j.mu.Unlock()
return next
}
// the follow functions set the job's unit with seconds,minutes,hours...
func (j *Job) mustInterval(i uint64) error {
if j.interval != i {
return fmt.Errorf("interval must be %d", i)
}
return nil
}
// setUnit sets unit type
func (j *Job) setUnit(unit string) *Job {
j.mu.Lock()
j.unit = unit
j.mu.Unlock()
return j
}
// Seconds set the unit with seconds
func (j *Job) Seconds() *Job {
return j.setUnit(seconds)
}
// Minutes set the unit with minute
func (j *Job) Minutes() *Job {
return j.setUnit(minutes)
}
// Hours set the unit with hours
func (j *Job) Hours() *Job {
return j.setUnit(hours)
}
// Days set the job's unit with days
func (j *Job) Days() *Job {
return j.setUnit(days)
}
//Weeks sets the units as weeks
func (j *Job) Weeks() *Job {
return j.setUnit(weeks)
}
// Second set the unit with second
func (j *Job) Second() *Job {
j.mustInterval(1)
return j.Seconds()
}
// Minute set the unit with minute, which interval is 1
func (j *Job) Minute() *Job {
j.mustInterval(1)
return j.Minutes()
}
// Hour set the unit with hour, which interval is 1
func (j *Job) Hour() *Job {
j.mustInterval(1)
return j.Hours()
}
// Day set the job's unit with day, which interval is 1
func (j *Job) Day() *Job {
j.mustInterval(1)
return j.Days()
}
// Weekday start job on specific Weekday
func (j *Job) Weekday(startDay time.Weekday) *Job {
j.mustInterval(1)
j.startDay = startDay
return j.Weeks()
}
// Monday set the start day with Monday
// - s.Every(1).Monday().Do(task)
func (j *Job) Monday() (job *Job) {
return j.Weekday(time.Monday)
}
// Tuesday sets the job start day Tuesday
func (j *Job) Tuesday() *Job {
return j.Weekday(time.Tuesday)
}
// Wednesday sets the job start day Wednesday
func (j *Job) Wednesday() *Job {
return j.Weekday(time.Wednesday)
}
// Thursday sets the job start day Thursday
func (j *Job) Thursday() *Job {
return j.Weekday(time.Thursday)
}
// Friday sets the job start day Friday
func (j *Job) Friday() *Job {
return j.Weekday(time.Friday)
}
// Saturday sets the job start day Saturday
func (j *Job) Saturday() *Job {
return j.Weekday(time.Saturday)
}
// Sunday sets the job start day Sunday
func (j *Job) Sunday() *Job {
return j.Weekday(time.Sunday)
}
// Scheduler struct, the only data member is the list of jobs.
// - implements the sort.Interface{} for sorting jobs, by the time nextRun
type Scheduler struct {
err error
shouldClear bool
mu *sync.Mutex
jobs []*Job // Slice store jobs
}
func (s *Scheduler) Len() int {
s.mu.Lock()
l := len(s.jobs)
s.mu.Unlock()
return l
}
func (s *Scheduler) Swap(i, j int) {
s.mu.Lock()
s.jobs[i], s.jobs[j] = s.jobs[j], s.jobs[i]
s.mu.Unlock()
}
func (s *Scheduler) Less(i, j int) bool {
s.mu.Lock()
l := s.jobs[j].nextRun.After(s.jobs[i].nextRun)
s.mu.Unlock()
return l
}
// NewScheduler creates a new scheduler
func NewScheduler() *Scheduler {
return &Scheduler{mu: new(sync.Mutex), jobs: []*Job{}}
}
// Get the current runnable jobs, which shouldRun is True
func (s *Scheduler) getRunnableJobs() ([]*Job, int) {
var runnableJobs []*Job
sort.Sort(s)
for i := 0; i < len(s.jobs); i++ {
if !s.jobs[i].shouldRun() {
break
}
runnableJobs = append(runnableJobs, s.jobs[i])
}
return runnableJobs, len(runnableJobs)
}
// NextRun datetime when the next job should run.
func (s *Scheduler) NextRun() (*Job, time.Time) {
if len(s.jobs) <= 0 {
return nil, time.Now()
}
sort.Sort(s)
return s.jobs[0], s.jobs[0].nextRun
}
// Every schedule a new periodic job with interval
func (s *Scheduler) Every(interval uint64, options ...func(*Job)) *Job {
job := NewJob(interval)
for _, option := range options {
option(job)
}
s.mu.Lock()
s.jobs = append(s.jobs, job)
s.mu.Unlock()
return job
}
// Err should be checked to ensure an error didn't occur durning the scheduling
func (s *Scheduler) Err() error {
return s.err
}
// RunPending runs all the jobs that are scheduled to run.
func (s *Scheduler) RunPending() error {
var shouldClear bool
s.mu.Lock()
shouldClear = s.shouldClear
s.mu.Unlock()
if shouldClear {
s.mu.Lock()
s.jobs = []*Job{}
s.shouldClear = false
s.mu.Unlock()
return nil
}
runnableJobs, n := s.getRunnableJobs()
for i := 0; i < n; i++ {
// remove the item from the set, if something was removed then it was queued
if runnableJobs[i].DistributedRedisClient != nil {
res := runnableJobs[i].DistributedRedisClient.SRem(redisKey+runnableJobs[i].DistributedJobName, "added")
if res.Val() == 0 {
continue
}
}
s.mu.Lock()
_, err := runnableJobs[i].run()
s.mu.Unlock()
if err != nil {
return err
}
}
return nil
}
// RunAll run all jobs regardless if they are scheduled to run or not
func (s *Scheduler) RunAll() {
s.RunAllwithDelay(0)
}
// RunAllwithDelay runs all jobs with delay seconds
func (s *Scheduler) RunAllwithDelay(d int) {
for i := 0; i < len(s.jobs); i++ {
s.jobs[i].run()
if 0 != d {
time.Sleep(time.Duration(d))
}
}
}
// Remove specific job j
func (s *Scheduler) Remove(j interface{}) {
var nj []*Job
for i := 0; i < len(s.jobs); i++ {
if s.jobs[i].jobFunc == getFunctionName(j) {
continue
}
nj = append(nj, s.jobs[i])
}
s.mu.Lock()
s.jobs = nj
s.mu.Unlock()
}
// Clear delete all scheduled jobs
func (s *Scheduler) Clear() {
s.mu.Lock()
s.shouldClear = true
s.mu.Unlock()
}
// Start all the pending jobs
// Add seconds ticker
func (s *Scheduler) Start() chan bool {
stopped := make(chan bool, 1)
ticker := time.NewTicker(100 * time.Millisecond)
go func() {
defer ticker.Stop()
for {
select {
case <-ticker.C:
err := s.RunPending()
if err != nil {
s.err = err
return
}
case <-stopped:
return
}
}
}()
return stopped
}
// The following methods are shortcuts for not having to
// create a Schduler instance
var defaultScheduler = NewScheduler()
// Every schedules a new periodic job running in specific interval
func Every(interval uint64, options ...func(*Job)) *Job {
job := defaultScheduler.Every(interval)
for _, option := range options {
option(job)
}
return job
}
// RunPending 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()
}
// RunAll run all jobs regardless if they are scheduled to run or not.
func RunAll() {
defaultScheduler.RunAll()
}
// RunAllwithDelay 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)
}
// Start run all jobs that are scheduled to run
func Start() chan bool {
return defaultScheduler.Start()
}
// Clear all scheduled jobs
func Clear() {
defaultScheduler.Clear()
}
// Remove specific job
func Remove(j interface{}) {
defaultScheduler.Remove(j)
}
// NextRun gets the next running time
func NextRun() (job *Job, time time.Time) {
return defaultScheduler.NextRun()
}
// Len gets the amount of jobs in the scheduler
func Len() int {
return defaultScheduler.Len()
}