@@ -70,14 +70,7 @@ func (s *Scheduler) SetLocation(newLocation *time.Location) {
70
70
71
71
// scheduleNextRun Compute the instant when this Job should run next
72
72
func (s * Scheduler ) scheduleNextRun (j * Job ) error {
73
- now := s .time .Now ().In (s .loc )
74
- if j .lastRun == s .time .Unix (0 , 0 ) {
75
- j .lastRun = now
76
- }
77
-
78
- if j .nextRun .After (now ) {
79
- return nil
80
- }
73
+ now := s .time .Now (s .loc )
81
74
82
75
periodDuration , err := j .periodDuration ()
83
76
if err != nil {
@@ -89,7 +82,7 @@ func (s *Scheduler) scheduleNextRun(j *Job) error {
89
82
j .nextRun = j .lastRun .Add (periodDuration )
90
83
case days :
91
84
j .nextRun = s .roundToMidnight (j .lastRun )
92
- j .nextRun = j .nextRun .Add (j .atTime )
85
+ j .nextRun = j .nextRun .Add (j .atTime ). Add ( periodDuration )
93
86
case weeks :
94
87
j .nextRun = s .roundToMidnight (j .lastRun )
95
88
dayDiff := int (j .startDay )
@@ -118,7 +111,7 @@ func (s *Scheduler) getRunnableJobs() []*Job {
118
111
var runnableJobs []* Job
119
112
sort .Sort (s )
120
113
for _ , job := range s .jobs {
121
- if job .shouldRun () {
114
+ if s .shouldRun (job ) {
122
115
runnableJobs = append (runnableJobs , job )
123
116
} else {
124
117
break
@@ -130,7 +123,7 @@ func (s *Scheduler) getRunnableJobs() []*Job {
130
123
// NextRun datetime when the next Job should run.
131
124
func (s * Scheduler ) NextRun () (* Job , time.Time ) {
132
125
if len (s .jobs ) <= 0 {
133
- return nil , s .time .Now ()
126
+ return nil , s .time .Now (s . loc )
134
127
}
135
128
sort .Sort (s )
136
129
return s .jobs [0 ], s .jobs [0 ].nextRun
@@ -147,11 +140,21 @@ func (s *Scheduler) Every(interval uint64) *Scheduler {
147
140
func (s * Scheduler ) RunPending () {
148
141
runnableJobs := s .getRunnableJobs ()
149
142
for _ , job := range runnableJobs {
150
- s .runJob (job )
143
+ s .runAndReschedule (job ) // we should handle this error somehow
151
144
}
152
145
}
153
146
154
- func (s * Scheduler ) runJob (job * Job ) error {
147
+ func (s * Scheduler ) runAndReschedule (job * Job ) error {
148
+ if err := s .run (job ); err != nil {
149
+ return err
150
+ }
151
+ if err := s .scheduleNextRun (job ); err != nil {
152
+ return err
153
+ }
154
+ return nil
155
+ }
156
+
157
+ func (s * Scheduler ) run (job * Job ) error {
155
158
if job .lock {
156
159
if locker == nil {
157
160
return fmt .Errorf ("trying to lock %s with nil locker" , job .jobFunc )
@@ -161,11 +164,10 @@ func (s *Scheduler) runJob(job *Job) error {
161
164
locker .Lock (key )
162
165
defer locker .Unlock (key )
163
166
}
164
- job .run ()
165
- err := s .scheduleNextRun (job )
166
- if err != nil {
167
- return err
168
- }
167
+
168
+ job .lastRun = s .time .Now (s .loc )
169
+ go job .run ()
170
+
169
171
return nil
170
172
}
171
173
@@ -177,7 +179,7 @@ func (s *Scheduler) RunAll() {
177
179
// RunAllWithDelay runs all Jobs with delay seconds
178
180
func (s * Scheduler ) RunAllWithDelay (d int ) {
179
181
for _ , job := range s .jobs {
180
- err := s .runJob (job )
182
+ err := s .run (job )
181
183
if err != nil {
182
184
continue
183
185
}
@@ -253,6 +255,19 @@ func (s *Scheduler) Do(jobFun interface{}, params ...interface{}) (*Job, error)
253
255
j .jobFunc = fname
254
256
255
257
if ! j .startsImmediately {
258
+ periodDuration , err := j .periodDuration ()
259
+ if err != nil {
260
+ return nil , err
261
+ }
262
+
263
+ if j .lastRun == s .time .Unix (0 , 0 ) {
264
+ j .lastRun = s .time .Now (s .loc )
265
+
266
+ if j .atTime != 0 {
267
+ j .lastRun = j .lastRun .Add (- periodDuration )
268
+ }
269
+ }
270
+
256
271
if err := s .scheduleNextRun (j ); err != nil {
257
272
return nil , err
258
273
}
@@ -283,11 +298,16 @@ func (s *Scheduler) StartAt(t time.Time) *Scheduler {
283
298
// StartImmediately sets the Jobs next run as soon as the scheduler starts
284
299
func (s * Scheduler ) StartImmediately () * Scheduler {
285
300
job := s .getCurrentJob ()
286
- job .nextRun = s .time .Now (). In ( s .loc )
301
+ job .nextRun = s .time .Now (s .loc )
287
302
job .startsImmediately = true
288
303
return s
289
304
}
290
305
306
+ // shouldRun returns true if the Job should be run now
307
+ func (s * Scheduler ) shouldRun (j * Job ) bool {
308
+ return s .time .Now (s .loc ).Unix () >= j .nextRun .Unix ()
309
+ }
310
+
291
311
// setUnit sets the unit type
292
312
func (s * Scheduler ) setUnit (unit timeUnit ) {
293
313
currentJob := s .getCurrentJob ()
0 commit comments