Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is the timezone off in delayed_job?

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:

  • Time.now gives 2014-03-04 23:26:55 -0600
  • Time.now.utc gives 2014-03-05 05:26:55 UTC
  • but delayed_job's idea of just a few seconds ago is 2014-03-04 17:26:53 -0600

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: output of index view

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)'
like image 794
joseph.hainline Avatar asked Oct 20 '22 13:10

joseph.hainline


1 Answers

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.

like image 56
joseph.hainline Avatar answered Oct 23 '22 02:10

joseph.hainline