Skip to content

Commit

Permalink
Fix task may appear to be triggered more than 1 second late (#27)
Browse files Browse the repository at this point in the history
* Fix task may appear to be triggered more than 1 second late

* Add comment

Co-authored-by: Per Malmberg <[email protected]>
  • Loading branch information
progheal and PerMalmberg authored Aug 27, 2022
1 parent 5f8ecc9 commit 0dd9df4
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions libcron/src/CronSchedule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,16 @@ namespace libcron
}
}

// Discard fraction seconds in the calculated schedule time
// that may leftover from the argument `from`, which in turn comes from `now()`.
// Fraction seconds will potentially make the task be triggered more than 1 second late
// if the `tick()` within the same second is earlier than schedule time,
// in that the task will not trigger until the next `tick()` next second.
// By discarding fraction seconds in the scheduled time,
// the `tick()` within the same second will never be earlier than schedule time,
// and the task will trigger in that `tick()`.
curr -= curr.time_since_epoch() % seconds{1};

return std::make_tuple(max_iterations > 0, curr);
}
}

0 comments on commit 0dd9df4

Please sign in to comment.