@@ -165,11 +165,11 @@ func (c *Cron) runWithRecovery(j Job) {
165
165
j .Run ()
166
166
}
167
167
168
- // Run the scheduler.. this is private just due to the need to synchronize
168
+ // Run the scheduler. this is private just due to the need to synchronize
169
169
// access to the 'running' state variable.
170
170
func (c * Cron ) run () {
171
171
// Figure out the next activation times for each entry.
172
- now := time . Now (). In ( c . location )
172
+ now := c . now ( )
173
173
for _ , entry := range c .entries {
174
174
entry .Next = entry .Schedule .Next (now )
175
175
}
@@ -178,22 +178,21 @@ func (c *Cron) run() {
178
178
// Determine the next entry to run.
179
179
sort .Sort (byTime (c .entries ))
180
180
181
- var effective time.Time
181
+ var timer * time.Timer
182
182
if len (c .entries ) == 0 || c .entries [0 ].Next .IsZero () {
183
183
// If there are no entries yet, just sleep - it still handles new entries
184
184
// and stop requests.
185
- effective = now . AddDate ( 10 , 0 , 0 )
185
+ timer = time . NewTimer ( 100000 * time . Hour )
186
186
} else {
187
- effective = c .entries [0 ].Next
187
+ timer = time . NewTimer ( c .entries [0 ].Next . Sub ( now ))
188
188
}
189
189
190
- timer := time .NewTimer (effective .Sub (now ))
191
190
select {
192
191
case now = <- timer .C :
193
192
now = now .In (c .location )
194
- // Run every entry whose next time was this effective time.
193
+ // Run every entry whose next time was less than now
195
194
for _ , e := range c .entries {
196
- if e .Next != effective {
195
+ if e .Next . After ( now ) || e . Next . IsZero () {
197
196
break
198
197
}
199
198
go c .runWithRecovery (e .Job )
@@ -251,3 +250,8 @@ func (c *Cron) entrySnapshot() []*Entry {
251
250
}
252
251
return entries
253
252
}
253
+
254
+ // now returns current time in c location
255
+ func (c * Cron ) now () time.Time {
256
+ return time .Now ().In (c .location )
257
+ }
0 commit comments