开发者

Rufus-scheduler variable for time zone - ruby on rails

开发者 https://www.devze.com 2023-04-01 06:17 出处:网络
I am running a rails app that uses rufus-scheduler to send out a daily e-mail to all users at noon, however, it currently sends it out at noon for the apps timezone \"Taipei\".

I am running a rails app that uses rufus-scheduler to send out a daily e-mail to all users at noon, however, it currently sends it out at noon for the apps timezone "Taipei".

I am capturing user timezones on signup, but I am unable to put开发者_运维技巧 a variable into a rufus-scheduler task

scheduler.cron('0 15 * * * @time_zone') do
  Account.all.each do |account|
    CODE
    account.users.each do |user|
      DELIVER EMAIL CODE
    end
  end
end

I guess I'm just not sure where to define the @time_zone variable so that rufus will read it properly.

Thanks in advance!


The scheduler doesn't need to care about timezones, it is better off thinking in UTC. What you want to do is run your task every hour and look for only those accounts where it is currently midnight. So your code would look more like this:

scheduler.cron('0 * * * *') do
  tz = THE TIMEZONE WHERE IT IS NOW MIDNIGHT
  Account.where(:timezone => tz).each do |account|
    CODE
    account.users.each do |user|
      DELIVER EMAIL CODE
    end
  end
end


scheduler.cron("0 15 * * * #{@time_zone}") do
  # ...
end

and the rufus-scheduler README says

The timezones are the ones supported by the ‘tzinfo’ rubygem (https://github.com/tzinfo/tzinfo).

0

精彩评论

暂无评论...
验证码 换一张
取 消