【问题标题】:Rails whenever not able to execute service methods每当无法执行服务方法时,Rails
【发布时间】:2018-02-05 16:40:28
【问题描述】:

我有一个 NotificationService,它有这样的方法:

def trial teacher
    notification = Notification.create(recipient: teacher, tag: 'trial', text: 'text', title: 'title') 

    notification.deliver
end

我想要一个每当运行器作业来执行此方法... 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 不存在该服务类

我错过了什么?

【问题讨论】:

  • 我可能遗漏了一些东西,但 NotificationService 是您自己定义的类?
  • 你能在生产环境的rails控制台上运行NotificationService.new.trial吗?
  • @MikeSzyndel 是的,没错
  • @Tai 是的,我可以!
  • 我可以在生产环境的rails控制台上运行NotificationService的所有方法。而且我还发现我可以通过 Cron 运行一些方法,而另一些我不能,并且不断收到“请指定有效的 ruby​​ 命令或要运行的脚本的路径。”

标签: ruby-on-rails ruby service cron whenever


【解决方案1】:

解决了!事实证明,问题不在于无论何时或 Cron,而是我试图运行的确切方法。 由于它是数据不断变化的生产环境,当我运行 NotificationService.new.trial 时,我很幸运没有出错。然后数据发生了变化,Cron 的运行尝试失败了。

使用安全导航运算符 (&.) 解决了所有问题!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-27
    • 2015-07-01
    相关资源
    最近更新 更多