Sidekiq to execute at specific time in every timezones

三世轮回 提交于 2021-02-18 17:21:22

问题


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 ...


回答1:


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



回答2:


Sidekiq has no scheduling mechanism for your use case of 'every day'. You need a cron scheduler like whenever.

Assuming your server is configured to UTC, you'd just create your schedule:

# EST 8pm
every 1.day, :at => '1:00 am' do
  runner "MyModel.task_to_run_at_1_am_UTC"
end
# CST 8pm
every 1.day, :at => '2:00 am' do
  runner "MyOtherModel.task_to_run_at_2_am_UTC"
end

To do it in one line:

every '* 6,7,8,9,10 1 * *' do
  runner "MyOtherModel.task_to_run_at_2_am_UTC"
end

Take a look at how cron syntax is formulated.



来源:https://stackoverflow.com/questions/43373389/sidekiq-to-execute-at-specific-time-in-every-timezones

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!