Fix task may appear to be triggered more than 1 second late (#27)

* Fix task may appear to be triggered more than 1 second late

* Add comment

Co-authored-by: Per Malmberg <PerMalmberg@users.noreply.github.com>
This commit is contained in:
progheal 2022-08-27 15:06:52 +08:00 committed by GitHub
parent 5f8ecc9690
commit 0dd9df49d7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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);
}
}