Rails how to schedule a rake email task from Development environment through Mailer for different timezones

38 Views Asked by At

I need to add a new email that can be sent to each user in the morning. We have a multi-tenant per client system, and each client has multiple users with their respective timezones. I have created a Rake task which loops through each user and sends the email. Is there any way I can schedule the task for each user at a particular time(say 10 a.m.) in their respective timezones? Can I add a trigger? I do not want to preferably use another gem, and I'm using Google cloud platform server.

1

There are 1 best solutions below

0
Brian Riley On

You might be able to use ActiveJob for this, but you would still need something external to kick off the rake task (like cron). With ActiveJob you could do something like:

class EmailJob < ApplicationJob
  def perform
    hour_offset = # Your calc to figure out the hours between the user's local time and your server time
    
    schedule_job(hour_offset)

    do_work
  end

  def do_work
    # Your mailer code
  end

  def schedule_job(hour_offset:)
    self.class.set(wait: hour_offset.hours).perform_later
  end
end

Depending on your setup, you may want to install an additional queue gem like DelayedJob or Requeue unless you're ok using the default