Skip to content

Commit db24a8b

Browse files
committed
reset job execution time if the system time is moved back and forth
1 parent bc59245 commit db24a8b

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

cron.go

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -97,17 +97,17 @@ func (s byTime) Less(i, j int) bool {
9797
//
9898
// Available Settings
9999
//
100-
// Time Zone
101-
// Description: The time zone in which schedules are interpreted
102-
// Default: time.Local
100+
// Time Zone
101+
// Description: The time zone in which schedules are interpreted
102+
// Default: time.Local
103103
//
104-
// Parser
105-
// Description: Parser converts cron spec strings into cron.Schedules.
106-
// Default: Accepts this spec: https://en.wikipedia.org/wiki/Cron
104+
// Parser
105+
// Description: Parser converts cron spec strings into cron.Schedules.
106+
// Default: Accepts this spec: https://en.wikipedia.org/wiki/Cron
107107
//
108-
// Chain
109-
// Description: Wrap submitted jobs to customize behavior.
110-
// Default: A chain that recovers panics and logs them to stderr.
108+
// Chain
109+
// Description: Wrap submitted jobs to customize behavior.
110+
// Default: A chain that recovers panics and logs them to stderr.
111111
//
112112
// See "cron.With*" to modify the default behavior.
113113
func New(opts ...Option) *Cron {
@@ -256,7 +256,17 @@ func (c *Cron) run() {
256256
// and stop requests.
257257
timer = time.NewTimer(100000 * time.Hour)
258258
} else {
259-
timer = time.NewTimer(c.entries[0].Next.Sub(now))
259+
entry := c.entries[0]
260+
if !entry.Prev.IsZero() && entry.Prev.After(now) {
261+
// time has moved back, we have to reset the next activation times for each entry.
262+
for _, e := range c.entries {
263+
c.logger.Info("reset next", "now", now, "entry", e.ID, "prev", e.Prev, "next", e.Next)
264+
e.Prev = time.Time{}
265+
e.Next = e.Schedule.Next(now)
266+
c.logger.Info("new schedule", "now", now, "entry", e.ID, "next", e.Next)
267+
}
268+
}
269+
timer = time.NewTimer(entry.Next.Sub(now))
260270
}
261271

262272
for {

0 commit comments

Comments
 (0)