2

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!

mu is too short
  • 426,620
  • 70
  • 833
  • 800
Steven Thomas
  • 111
  • 1
  • 3

2 Answers2

3

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
mu is too short
  • 426,620
  • 70
  • 833
  • 800
2
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).

jmettraux
  • 3,511
  • 3
  • 31
  • 30
  • @jmettraux I am trying to run the same scheduler for different Timezones but ended up with infinite loop and continuously running scheduler. Could you please help me out. – anusha Jun 21 '17 at 18:04
  • @anusha describe carefully your issue at https://github.com/jmettraux/rufus-scheduler/issues – jmettraux Jun 21 '17 at 20:23