tomykaira/clockwork — pull request #135 by brianhempel (2014-11-15 00:00:52+00:00)
Title: Don't Let Start Time Drift

With the default `sleep_timeout` of 1 second, we observed a drift of about 0.6 seconds per period. On average, each hourly job started 1 hour + 0.6 seconds later than the last. This drift compounded over time. We observed that an hourly job that once started 0 seconds after the hour would, after about 18 hours, start at 10 seconds after the hour. Here's a graph of how soon after the hour our jobs started (in milliseconds):

![screen shot 2014-11-14 at 6 34 43 pm](https://cloud.githubusercontent.com/assets/506950/5055245/9ddafdb8-6c2e-11e4-81fc-3db61b452f37.png)
(The graph is little jagged because there's actually multiple hourly jobs aggregated there.)

The only reason this didn't get out of control was because Heroku restarts dynos at least once a day.

This PR fixes the problem with two changes:

1. When calculating if the entire period has passed, the start time of the last run is rounded down to the beginning of the second. (For an hourly job, if the last job ran at 10:00:00.5 it is safe to run again at 11:00:00.0)
2. The default `sleep_timeout` is changed from 1 second to 0.5 seconds to avoid skipping over any seconds. (Because sleep will sleep _at least_ the time ask for, sleeping for 1 second could sleep a bit more and skip a second. Changing the default to 0.5 seconds avoids this problem.)