简体   繁体   English

在Heroku上使用config.time_zone的Rails,Rake计划的任务和DateTime

[英]Rails, Rake scheduled tasks and DateTime with config.time_zone on Heroku

I seem to always struggle understanding this... 我似乎总是很难理解这一点...

I have the Heroku scheduler running every 10m looking for new scheduled jobs to be run but it is not running when I expect it to - the difference is the time zone but I'm unclear exactly how I should do the comparison so it works the way I expect. 我让Heroku调度程序每10m运行一次,以寻找要运行的新调度作业,但我期望的时候它没有运行-时区不同,但是我不清楚我应该如何进行比较,因此它无法正常工作我预计。

in the rake task we have: 在rake任务中,我们有:

schedules = Schedule.where('status in (?) and next_run < ?', ['new', nil, ''], DateTime.current)

in application.rb is 在application.rb中是

config.time_zone = 'Auckland'
config.active_record.default_timezone = :local

Now, the exact example I have just seen that didn't do what was expected was this: 现在,我刚刚看到的并没有达到预期效果的确切示例是:

  1. A user in NZ set up a schedule with a next_run time of Wednesday 18 February at 10am 新西兰的用户设置了一个计划,其下一个运行时间为2月18日(星期三)上午10点
  2. Based on the task above, this was actually run just after 9pm on Tuesday 17 February 根据上述任务,该操作实际上是在2月17日星期二晚上9点之后执行的

Difference is the +13 hours for the NZ time zone. 差异是NZ时区+13小时。

Question is, how should I do this comparison to get the schedule to fire when I am expecting? 问题是,我应该如何进行比较以使计划如期启动?

UPDATE 1: So after a bit of messing about, a few specific items worked for me: 更新1:在经过一番混乱之后,一些特定的项目对我有用:

  1. as per the answer below, I set the server timezone to the one I want 按照下面的答案,我将服务器时区设置为我想要的一个
  2. I have removed the default_timezone entry form the config file - THIS WAS REALLY MESSING UP MY HEAD! 我已经从配置文件中删除了default_timezone条目-这真是让我头晕了头脑!
  3. Replaced all the various combinations of Date.now, Date.current, Time.now, Time.current, DateTime.now, Date.today etc. etc. with two specific replacements: Time.zone.today (and similar) and Time.zone.now (see this blog for all the juicy details http://danilenko.org/2012/7/6/rails_timezones/ ) 将Date.now,Date.current,Time.now,Time.current,DateTime.now,Date.today等的所有各种组合替换为两个特定的替换:Time.zone.today(和类似名称)和Time。 zone.now(有关所有多汁的详细信息,请参见此博客http://danilenko.org/2012/7/6/rails_timezones/

Use heroku env variable to set the default time zone: 使用heroku env变量设置默认时区:

heroku config:add TZ="America/Los_Angeles" # example for Los Angeles

I suppose , for your case it would be: 对于您的情况,我是:

heroku config:add TZ="Pacific/Auckland"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM