I have a cronjob starting a ruby script (in a rails environment) at the beginning of each hour. The ruby script then makes a series of SQL queries, and looks like this:
def hourly_job time = Time.now - 1.hour date = time.to_date hour = time.hour start_time = date.to_time + hour.hours end_time = start_time + 1.hour Log.where(date: date, hour: hour).delete_all count = Event.where('created_at >= ?', start_time). where('created_at < ?', end_time).count Log.create(date: date, hour: hour, count: count) end
The cronjob looks like this (generated by the
0 * * * * /bin/bash -l -c 'cd /path/to/myproj && script/rails runner '\''Log.hourly_job'\'''
This seemed to work fine for a few weeks, but this morning I found that the log table is empty -- something has erased all of my past data, in addition to not logging hourly data until a few hours ago.
An inspection of the MySQL error log revealed no events in the last week, and all of the other table's data seems to be fine, so I don't think it's a DBMS problem.
I'm suspecting some sort of discrepancy between the time that
crontab is using and the time given by ruby's
Time.now. I'm also exploring the idea that there is something subtly and horribly wrong with my code.
Are there other possible explanations?
Additionally, I am running this project on two separate servers. This data erasing bug has only hit one of them.