Skip to content

Commit f9402aa

Browse files
committed
modified condition to select jobs to run in current loop
Select all exprired jobs instead of exact match job next time running time.
1 parent 65ba8ec commit f9402aa

File tree

2 files changed

+32
-8
lines changed

2 files changed

+32
-8
lines changed

cron.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -165,11 +165,11 @@ func (c *Cron) runWithRecovery(j Job) {
165165
j.Run()
166166
}
167167

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
169169
// access to the 'running' state variable.
170170
func (c *Cron) run() {
171171
// Figure out the next activation times for each entry.
172-
now := time.Now().In(c.location)
172+
now := c.now()
173173
for _, entry := range c.entries {
174174
entry.Next = entry.Schedule.Next(now)
175175
}
@@ -178,22 +178,21 @@ func (c *Cron) run() {
178178
// Determine the next entry to run.
179179
sort.Sort(byTime(c.entries))
180180

181-
var effective time.Time
181+
var timer *time.Timer
182182
if len(c.entries) == 0 || c.entries[0].Next.IsZero() {
183183
// If there are no entries yet, just sleep - it still handles new entries
184184
// and stop requests.
185-
effective = now.AddDate(10, 0, 0)
185+
timer = time.NewTimer(100000 * time.Hour)
186186
} else {
187-
effective = c.entries[0].Next
187+
timer = time.NewTimer(c.entries[0].Next.Sub(now))
188188
}
189189

190-
timer := time.NewTimer(effective.Sub(now))
191190
select {
192191
case now = <-timer.C:
193192
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
195194
for _, e := range c.entries {
196-
if e.Next != effective {
195+
if e.Next.After(now) || e.Next.IsZero() {
197196
break
198197
}
199198
go c.runWithRecovery(e.Job)
@@ -251,3 +250,8 @@ func (c *Cron) entrySnapshot() []*Entry {
251250
}
252251
return entries
253252
}
253+
254+
// now returns current time in c location
255+
func (c *Cron) now() time.Time {
256+
return time.Now().In(c.location)
257+
}

cron_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,26 @@ func TestJob(t *testing.T) {
316316
}
317317
}
318318

319+
type ZeroSchedule struct{}
320+
321+
func (*ZeroSchedule) Next(time.Time) time.Time {
322+
return time.Time{}
323+
}
324+
325+
// Tests that job without time does not run
326+
func TestJobWithZeroTimeDoesNotRun(t *testing.T) {
327+
cron := New()
328+
calls := 0
329+
cron.AddFunc("* * * * * *", func() { calls += 1 })
330+
cron.Schedule(new(ZeroSchedule), FuncJob(func() { t.Error("expected zero task will not run") }))
331+
cron.Start()
332+
defer cron.Stop()
333+
<-time.After(OneSecond)
334+
if calls != 1 {
335+
t.Errorf("called %d times, expected 1\n", calls)
336+
}
337+
}
338+
319339
func wait(wg *sync.WaitGroup) chan bool {
320340
ch := make(chan bool)
321341
go func() {

0 commit comments

Comments
 (0)