Rails 每当无法执行服务方法时
Rails whenever not able to execute service methods
我有一个 NotificationService,它有这样的方法:
def trial teacher
notification = Notification.create(recipient: teacher, tag: 'trial', text: 'text', title: 'title')
notification.deliver
end
我想要一个 Whenever runner 作业来执行此方法 ... bin/rails runner "NotificationService.new.trial" --silent
,但如果我尝试这样做,我会收到错误 Please specify a valid ruby command or the path of a script to run.
。然而,如果我尝试使用模型而不是服务,它会起作用(例如,Notification.last.deliver
)
而且这在开发中执行得很好,但在生产中不起作用。似乎 Cron
的服务 class 不存在
我错过了什么?
解决了!事实证明,问题不在于 Whenever 或 Cron,而在于我尝试 运行 的确切方法。
由于它是数据不断变化的生产环境,所以当我 运行 NotificationService.new.trial 时,我很幸运没有出错。然后数据发生变化,Cron 对 运行 的尝试失败了。
使用安全导航运算符 (&.) 解决了所有问题!
我有一个 NotificationService,它有这样的方法:
def trial teacher
notification = Notification.create(recipient: teacher, tag: 'trial', text: 'text', title: 'title')
notification.deliver
end
我想要一个 Whenever runner 作业来执行此方法 ... bin/rails runner "NotificationService.new.trial" --silent
,但如果我尝试这样做,我会收到错误 Please specify a valid ruby command or the path of a script to run.
。然而,如果我尝试使用模型而不是服务,它会起作用(例如,Notification.last.deliver
)
而且这在开发中执行得很好,但在生产中不起作用。似乎 Cron
的服务 class 不存在我错过了什么?
解决了!事实证明,问题不在于 Whenever 或 Cron,而在于我尝试 运行 的确切方法。 由于它是数据不断变化的生产环境,所以当我 运行 NotificationService.new.trial 时,我很幸运没有出错。然后数据发生变化,Cron 对 运行 的尝试失败了。
使用安全导航运算符 (&.) 解决了所有问题!