Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reset next job execution if the system time has been moved backwards #480

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 20 additions & 10 deletions cron.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,17 +97,17 @@ func (s byTime) Less(i, j int) bool {
//
// Available Settings
//
// Time Zone
// Description: The time zone in which schedules are interpreted
// Default: time.Local
// Time Zone
// Description: The time zone in which schedules are interpreted
// Default: time.Local
//
// Parser
// Description: Parser converts cron spec strings into cron.Schedules.
// Default: Accepts this spec: https://en.wikipedia.org/wiki/Cron
// Parser
// Description: Parser converts cron spec strings into cron.Schedules.
// Default: Accepts this spec: https://en.wikipedia.org/wiki/Cron
//
// Chain
// Description: Wrap submitted jobs to customize behavior.
// Default: A chain that recovers panics and logs them to stderr.
// Chain
// Description: Wrap submitted jobs to customize behavior.
// Default: A chain that recovers panics and logs them to stderr.
//
// See "cron.With*" to modify the default behavior.
func New(opts ...Option) *Cron {
Expand Down Expand Up @@ -256,7 +256,17 @@ func (c *Cron) run() {
// and stop requests.
timer = time.NewTimer(100000 * time.Hour)
} else {
timer = time.NewTimer(c.entries[0].Next.Sub(now))
entry := c.entries[0]
if !entry.Prev.IsZero() && entry.Prev.After(now) {
// time has moved back, we have to reset the next activation times for each entry.
for _, e := range c.entries {
c.logger.Info("reset schedule", "now", now, "entry", e.ID, "prev", e.Prev, "next", e.Next)
e.Prev = time.Time{}
e.Next = e.Schedule.Next(now)
c.logger.Info("new schedule", "now", now, "entry", e.ID, "next", e.Next)
}
}
timer = time.NewTimer(entry.Next.Sub(now))
}

for {
Expand Down