robfig / cron

a cron library for go
MIT License
13.15k stars 1.63k forks source link

During the execution process of scheduled tasks, a change in the system time can result in incorrect execution timing. #509

Closed ssongliu closed 8 months ago

ssongliu commented 1 year ago

Hello @robfig.

Thank you very much for providing such excellent code! but I have encountered an issue while using it.

Suppose the current time is 2023-11-02 09:00:00, and I've enabled a scheduled task to run at 10:30 every day. (Based on the code and the current time, a 1-hour timer will be activated.)

for {
        // Determine the next entry to run.
        sort.Sort(byTime(c.entries))

        var timer *time.Timer
        if len(c.entries) == 0 || c.entries[0].Next.IsZero() {
            // If there are no entries yet, just sleep - it still handles new entries
            // and stop requests.
            timer = time.NewTimer(100000 * time.Hour)
        } else {
            timer = time.NewTimer(c.entries[0].Next.Sub(now))
        }
...

Change the system time to 2023-11-05 06:00:00. (When the timer runs one hour later, it starts execution, and at that time, the system time is 2023-11-05 07:00:00.) At this point, the execution condition only relies on the following code:

if e.Next.After(now) || e.Next.IsZero() {
    break
}

It will pass this condition and execute the scheduled task without matching the originally set execution time of 10:30.

Thanks.

jiangzhongqiang commented 10 months ago

Hello @robfig.

Thank you very much for providing such excellent code! but I have encountered an issue while using it.

Suppose the current time is 2023-11-02 09:00:00, and I've enabled a scheduled task to run at 10:30 every day. (Based on the code and the current time, a 1-hour timer will be activated.)

for {
      // Determine the next entry to run.
      sort.Sort(byTime(c.entries))

      var timer *time.Timer
      if len(c.entries) == 0 || c.entries[0].Next.IsZero() {
          // If there are no entries yet, just sleep - it still handles new entries
          // and stop requests.
          timer = time.NewTimer(100000 * time.Hour)
      } else {
          timer = time.NewTimer(c.entries[0].Next.Sub(now))
      }
...

Change the system time to 2023-11-05 06:00:00. (When the timer runs one hour later, it starts execution, and at that time, the system time is 2023-11-05 07:00:00.) At this point, the execution condition only relies on the following code:

if e.Next.After(now) || e.Next.IsZero() {
    break
}

It will pass this condition and execute the scheduled task without matching the originally set execution time of 10:30.

Thanks.

系统时间都改了,这种表现是正常的啊,我觉得没啥问题

elbert-chan commented 9 months ago

Hello @robfig.

Thank you very much for providing such excellent code! but I have encountered an issue while using it.

Suppose the current time is 2023-11-02 09:00:00, and I've enabled a scheduled task to run at 10:30 every day. (Based on the code and the current time, a 1-hour timer will be activated.)

for {
      // Determine the next entry to run.
      sort.Sort(byTime(c.entries))

      var timer *time.Timer
      if len(c.entries) == 0 || c.entries[0].Next.IsZero() {
          // If there are no entries yet, just sleep - it still handles new entries
          // and stop requests.
          timer = time.NewTimer(100000 * time.Hour)
      } else {
          timer = time.NewTimer(c.entries[0].Next.Sub(now))
      }
...

Change the system time to 2023-11-05 06:00:00. (When the timer runs one hour later, it starts execution, and at that time, the system time is 2023-11-05 07:00:00.) At this point, the execution condition only relies on the following code:

if e.Next.After(now) || e.Next.IsZero() {
    break
}

It will pass this condition and execute the scheduled task without matching the originally set execution time of 10:30.

Thanks.

This doesn't seem to be a problem for this library to deal with. The issues should be closed.