4

We're currently using 'whenever' to schedule jobs in a Rails project. The system is expanding to support users in multiple timezones (timezone is stored in User model) and we would like to send users an email at a specific time of THEIR day.

Any ideas on the best pattern to achieve this? I'm foreseeing 24 (or more - there are half timezones) 'whenever' tasks per email job each with a different timezone filter on the user table.

Any ideas for a cleaner solution? A better scheduler than whenever perhaps? Something that will create the 24/48 cronjobs and call a callback passing a timezone or a UTC offset? Something like that.

KrayzeeKev
  • 91
  • 1
  • 5

1 Answers1

1

I am not familiar with 'whenever' but I had a similar challenge and used clockwork

My problem was that I had to run a process at midnight for each of my customers. And like you, my customers can be anywhere in the world so it had to be ran at midnight their time.

The process of running the job at midnight looked something like this:

TZInfo::Timezone.all_country_zone_identifiers.each do |zone|                                                                                                                                                                                               
  every(1.day, {time_zone: zone}, at: '00:00', tz: "#{zone}") do |job_attr|                                                                                                                                  
    time_zone = job_attr[:time_zone]                                                                                                                                                                                   
    YourJob.perform_later(time_zone)                                                                                                                                                                                           
  end                                                                                                                                                                                                                                    
end

The class of 'YourJob' will just find all my customers with that time_zone and perform a job.

Hope that helps.

Ramon Carlos
  • 65
  • 1
  • 4
  • I am also doing the same exactly what you are doing. But I am facing a problem. When I have scheduled for specific time(ex: singapore time) it matches to other timezone(Asia/Hong_kong) so it is executing for hong_kong users but not for singapore users. Could you please help me to solve this. – anusha Nov 03 '17 at 06:28