Schedule Cron job for different timezones rails?

Viewed 47

I want to schedule a cron job that would send emails to all users as soon as deadline for their events ended. Note: Every user have different timezone.

The approach i was thinking about: I have a cron job that will run every day at 11 PM of UTC. This approach does work but every user will receive email according to UTC i.e at 1am, 3am or 4pm. Not according to their timezone.

* 23 * * * 'UTC'
1 Answers

You can just run the same job at an interval of every 30 minutes so that it covers all the timezones. This should work:

# run this every 30 minutes
time_zones_with_23 = ActiveSupport::TimeZone.all.select { |tz| tz.now.hour == 23 }.map { |tz| tz.tzinfo.name }
user_list = User.where(time_zone: time_zones_with_23)
Related