【发布时间】:2020-03-05 04:52:01
【问题描述】:
我是 CRON 工作的新手,但我认为我的设置正确。
最终我要做的是每天早上 8:00 向过去 3 天内未登录、未收到电子邮件且已标记的用户(和其他几个人)发送一封电子邮件作为活动或临时状态。
所以通过在控制台中查询数据库我知道我可以做到:
- first = User.where(status: 'active').or(User.where(status: 'temp'))
- second = first.where("last_login_at
- 第三个 = second.where(notified: false)
这不一定是干净的,但我正在努力寻找一个包含所有数据的包含查询。 有没有更简洁的方法来做这个查询?
我相信我已经使用跑步者正确设置了我的 cron 作业。我已经安装了,并且在我的 schedule.rb 中我有:
every 1.day, at: '8:00 am' do
runner 'ReminderMailer.agent_mailer.deliver'
end
所以在 app > mailer 我创建了 ReminderMailer
class ReminderMailer < ApplicationMailer
helper ReminderHelper
def agent_reminder(user)
@user = user
mail(to: email_recipients(user), subject: 'This is your reminder')
end
def email_recipients(agent)
email_address = ''
email_addresses += agent.notification_emails + ',' if agent.notification_emails
email_addresses += agent.manager
email_address += agent.email
end
end
我真正苦苦挣扎的地方是我应该将查询发送到邮件程序的地方,这就是我构建 ReminderHelper 的原因。
module ReminderHelper
def applicable_agents(user)
agent = []
first = User.where(status: 'active').or(User.where(status: 'temp'))
second = first.where("last_login_at < ? ", Time.now-3.days)
third = second.where(notified: false)
agent << third
return agent
end
end
编辑:所以我知道理论上我可以进行一系列 where 查询。一定有更好的方法吧?
所以我需要帮助的是:我的结构是否正确?是否有更简洁的方法可以在 ActiveRecord 中为 CRON 作业查询这些数据?有没有办法测试这个?
【问题讨论】:
标签: ruby-on-rails cron