Lor*_*enz 3 ruby scheduling ruby-on-rails
我试图理解如何正确使用,或者如果我甚至将它用于正确的事情.我创造了一份工作:
class ScheduleSendNotificationsJob < ActiveJob::Base
queue_as :notification_emails
def perform(*args)
user_ids = User.
joins(:receipts).
where(receipts: {is_read: false}).
select('DISTINCT users.id').
map(&:id)
user_ids.each do |user_id|
SendNotificationsJob.create(id: user_id)
Rails.logger.info "Scheduled a job to send notifications to user #{user_id}"
end
end
end
Run Code Online (Sandbox Code Playgroud)
我想在一定时间内完成这项工作.作业轮询以查看是否有任何未完成的通知,批量处理,然后将它们发送给用户,以便用户可以收到一封包含大量通知的电子邮件,而不是每封电子邮件一封通知的电子邮件.我尝试使用延迟作业执行此操作,但似乎并非设计为定期安排某些内容.所以现在我正在尝试使用随时随地的宝石,但我似乎无法弄清楚如何正确设置它.
这是我在config/schedule.rb文件中的内容:
every 1.minute do
runner ScheduleSendNotifications.create
end
Run Code Online (Sandbox Code Playgroud)
当我在控制台中每当-i运行时,我得到以下内容:
Lorenzs-MacBook-Pro:Heartbeat-pods lorenzsell$ whenever -i
config/schedule.rb:13:in `block in initialize': uninitialized constant Whenever::JobList::ScheduleSendNotifications (NameError)
Run Code Online (Sandbox Code Playgroud)
我在这做错了什么?我应该使用其他东西吗?我只是学习ruby和rails所以非常感谢任何帮助.谢谢.
when gem将字符串作为runner函数的参数.每当实际上没有加载Rails环境,因此它不知道您的ScheduleSendNotifications类.
下面的代码应正确设置crontab以运行您的作业.
every 1.minute do
runner "ScheduleSendNotifications.create"
end
Run Code Online (Sandbox Code Playgroud)
从项目目录运行whenever -w以设置crontab文件.运行crontab -l以查看写入的crontab文件.系统每分钟都会执行一次Rails运行程序.如果出现问题,您可能需要调试ScheduleSendNotifications.create代码.