Search code examples
ruby-on-railsrubyservicecronwhenever

Rails whenever not able to execute service methods


I have a NotificationService which has methods like this:

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

    notification.deliver
end

I want to have a Whenever runner job to execute this method ... bin/rails runner "NotificationService.new.trial" --silent, but if I try to do so I get an error Please specify a valid ruby command or the path of a script to run.. Yet if I try to use a model, not a service, it works (for instance, Notification.last.deliver)

And also that executes perfectly fine on development, but doesn't work on production. Seems like that service class doesn't exist for Cron

What am I missing?


Solution

  • Solved it! Turned out that the trouble was not with Whenever or Cron, but the exact method I was trying to run. And as it was production environment with constantly changing data, when I ran NotificationService.new.trial I just got lucky not to get an error. Then data changed and Cron's attempts to run were failing.

    Using safe navigation operator (&.) solved everything!