In my Rails app, when I create a background job using the delayed_job gem, I get all times offset by 6 hours.
My understanding is that delayed_job uses your timezone, but it's like it's using the wrong one. Instead of being -6 hours from UTC (CST is my time zone), it's -12 hours!
Here's a bit of view code to illustrate. Note:
My View:
#delayed_jobs/index.html.erb
<h1>All Background Jobs</h1>
<p>The time now is: <%= Time.now %> </p>
<p>The time UTC is: <%= Time.now.utc %> </p>
<table>
<tr>
<th>ID</th>
<th>Queue</th>
<th>Created At</th>
<th>Run At</th>
</tr>
<% @delayed_jobs.each do |dj| %>
<tr>
<td><%= dj.id %></td>
<td><%= dj.queue %></td>
<td><%= dj.created_at %></td>
<td><%= dj.run_at %></td>
</tr>
<% end %>
</table>
Output:
I can create jobs any of these three ways, and will get the same created_at time:
MyClass.delay.foo
MyClass.delay(run_at: 0.minutes.from_now).foo
MyClass.delay(run_at: 0.minutes.from_now.getutc).foo
My configuration has:
#config/application.rb
config.time_zone = 'Central Time (US & Canada)'
config.active_record.default_timezone = 'Central Time (US & Canada)'
The problem was in my config/application.rb as @rainkinz suggested, specifically the 2nd line:
config.time_zone = 'Central Time (US & Canada)'
config.active_record.default_timezone = 'Central Time (US & Canada)'
Apparently the default_timezone setting is deprecated after Rails 3.2.13, which I just upgraded from a few days ago.
When I changed it to only have the local time zone set:
config.time_zone = 'Central Time (US & Canada)'
#config.active_record.default_timezone = 'Central Time (US & Canada)'
This fixed the problem. All active record objects still seem to have the correct time when saved, as do delayed_jobs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With