Search code examples
ruby-on-railsrubysidekiq

Sidekiq to execute at specific time in every timezones


Using Sidekiq, what are the strategy to have it execute at a particular time in every time zone?

For example, I need Sidekiq to perform a task every day at the following time and time zones across the world:

... 8:00 PM EST 8:00 PM CST 8:00 PM MST 8:00 PM PST ...


Solution

  • I would just run the same job at the start of each hour. You might want to use cron to run the job each hour or have a look at this list on how to create recurring jobs in Sidekiq.

    The first step within the job is to figure out all time zones in which it is 8AM right now:

    8am_time_zones = ActiveSupport::TimeZone.all
                                            .select { |tz| tz.now.hour == 8 }
                                            .map { |tz| tz.tzinfo.name }
    #=> ["America/Los_Angeles", "America/Tijuana", "America/Phoenix"]
    

    With this list you can easily load users from your database who live in one of the time zones (assuming you stored the users' time zone in this format) and send them the email:

    User.where(time_zone: 8am_time_zones).find_each do |user|
      # email_newsletter(recipient: user)
    end