Rails如何从开发环境通过Mailer调度不同时区的rake电子邮件任务



我需要添加一个新的电子邮件,可以在早上发送给每个用户。每个客户端系统都有一个多租户,每个客户端都有多个具有各自时区的用户。我创建了一个Rake任务,通过每个用户循环并发送电子邮件。我是否可以在每个用户各自时区的特定时间(比如上午10点)为他们安排任务?我可以添加触发器吗?我不想最好使用另一个gem,我使用谷歌云平台服务器。

您可能可以使用ActiveJob来完成这个任务,但是您仍然需要一些外部的东西来启动rake任务(比如cron)。使用ActiveJob,你可以这样做:

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

根据您的设置,您可能需要安装额外的队列gem,如DelayedJob或Requeue,除非您可以使用默认的

最新更新