Ruby on rails 使用带有Rails活动作业的gem计划批处理电子邮件作业
我试图理解如何正确地使用它,或者我是否将它用于正确的事情。我创造了一份工作:Ruby on rails 使用带有Rails活动作业的gem计划批处理电子邮件作业,ruby-on-rails,ruby,scheduling,Ruby On Rails,Ruby,Scheduling,我试图理解如何正确地使用它,或者我是否将它用于正确的事情。我创造了一份工作: class ScheduleSendNotificationsJob < ActiveJob::Base queue_as :notification_emails def perform(*args) user_ids = User. joins(:receipts). where(receipts: {is_read:
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
当我在控制台中运行where-I时,我得到以下信息:
Lorenzs-MacBook-Pro:Heartbeat-pods lorenzsell$ whenever -i
config/schedule.rb:13:in `block in initialize': uninitialized constant Whenever::JobList::ScheduleSendNotifications (NameError)
我做错了什么?我应该用别的东西吗?我正在学习ruby和rails,非常感谢您的帮助。谢谢。每当gem将字符串作为runner函数的参数时。where没有实际加载Rails环境,因此它不知道ScheduleSendNotifications类 下面的代码应该正确设置crontab以运行作业
every 1.minute do
runner "ScheduleSendNotifications.create"
end
从项目目录运行
where-w
以设置crontab文件。运行crontab-l
查看写入的crontab文件。系统每分钟都会执行Rails runner。从那里,您可能需要调试您的ScheduleSendNotifications。如果某些东西不起作用,请创建代码。确定。这就解决了错误。如何让它实际运行?在答案中添加了一些信息。如果有更多的错误,请随意发布。我用我调用的完整函数体更新了我的问题。那里有一个Rails logger的电话,似乎根本没有被呼叫。我假设cron作业不是每分钟都在运行。我在这里遗漏了什么吗?只要crontab-l
显示正确的cron,您的问题可能就不存在了。从这里进行调试的最佳方法是首先检查/var/log/where.log
是否存在任何异常。如果这没有帮助,那么我将尝试从命令行手动执行运行程序,方法是复制并粘贴在crontab文件(crontab-l)中生成的命令。ScheduleSendNotifications.create
…您的类不是名为ScheduleSendNotificationsJob
?另外,查看ActiveJob
API,您希望稍后对其调用perform\u
,而不是create
,是吗?
every 1.minute do
runner "ScheduleSendNotifications.create"
end