为什么在delay_job中关闭了时区? [英] Why is the timezone off in delayed_job?

查看:62
本文介绍了为什么在delay_job中关闭了时区?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Rails应用程序中,当我使用之后,不推荐使用= http://apidock.com/rails/ActiveRecord/Base/default_timezone/class rel = nofollow> default_timezone设置。



当我将其更改为仅设置本地时区时:

  config.time_zone ='中央时间(美国&加拿大)'
#config.active_record.default_timezone ='中部时间(美国和加拿大)'

这解决了问题。保存所有活动记录对象的时间似乎仍然正确,与delay_jobs一样。


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:

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.

这篇关于为什么在delay_job中关闭了时区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆