@@ -12,21 +12,23 @@ import (
12
12
type Scheduler struct {
13
13
jobs []* Job
14
14
loc * time.Location
15
+ time timeHelper // an instance of timeHelper to interact with the time package
15
16
}
16
17
17
18
// NewScheduler creates a new scheduler
18
19
func NewScheduler (loc * time.Location ) * Scheduler {
19
20
return & Scheduler {
20
21
jobs : newEmptyJobSlice (),
21
22
loc : loc ,
23
+ time : newTimeHelper (),
22
24
}
23
25
}
24
26
25
27
// Start all the pending jobs
26
28
// Add seconds ticker
27
29
func (s * Scheduler ) Start () chan struct {} {
28
30
stopped := make (chan struct {})
29
- ticker := time .NewTicker (1 * time .Second )
31
+ ticker := s . time .NewTicker (1 * time .Second )
30
32
31
33
go func () {
32
34
for {
@@ -67,8 +69,8 @@ func (s *Scheduler) ChangeLoc(newLocation *time.Location) {
67
69
68
70
// scheduleNextRun Compute the instant when this job should run next
69
71
func (s * Scheduler ) scheduleNextRun (j * Job ) error {
70
- now := time .Now ().In (s .loc )
71
- if j .lastRun == time .Unix (0 , 0 ) {
72
+ now := s . time .Now ().In (s .loc )
73
+ if j .lastRun == s . time .Unix (0 , 0 ) {
72
74
j .lastRun = now
73
75
}
74
76
@@ -107,7 +109,7 @@ func (s *Scheduler) scheduleNextRun(j *Job) error {
107
109
108
110
// roundToMidnight truncate time to midnight
109
111
func (s * Scheduler ) roundToMidnight (t time.Time ) time.Time {
110
- return time .Date (t .Year (), t .Month (), t .Day (), 0 , 0 , 0 , 0 , s .loc )
112
+ return s . time .Date (t .Year (), t .Month (), t .Day (), 0 , 0 , 0 , 0 , s .loc )
111
113
}
112
114
113
115
// Get the current runnable jobs, which shouldRun is True
@@ -127,7 +129,7 @@ func (s *Scheduler) getRunnableJobs() []*Job {
127
129
// NextRun datetime when the next job should run.
128
130
func (s * Scheduler ) NextRun () (* Job , time.Time ) {
129
131
if len (s .jobs ) <= 0 {
130
- return nil , time .Now ()
132
+ return nil , s . time .Now ()
131
133
}
132
134
sort .Sort (s )
133
135
return s .jobs [0 ], s .jobs [0 ].nextRun
@@ -178,7 +180,7 @@ func (s *Scheduler) RunAllWithDelay(d int) {
178
180
if err != nil {
179
181
continue
180
182
}
181
- time .Sleep (time .Duration (d ) * time .Second )
183
+ s . time .Sleep (time .Duration (d ) * time .Second )
182
184
}
183
185
}
184
186
@@ -288,7 +290,7 @@ func (s *Scheduler) StartAt(t time.Time) *Scheduler {
288
290
// StartImmediately sets the jobs next run as soon as the scheduler starts
289
291
func (s * Scheduler ) StartImmediately () * Scheduler {
290
292
job := s .getCurrentJob ()
291
- job .nextRun = time .Now ().In (s .loc )
293
+ job .nextRun = s . time .Now ().In (s .loc )
292
294
job .startsImmediately = true
293
295
return s
294
296
}
0 commit comments