bundler: not executable: bin/rails in rails production while using whenever gem

1.5k Views Asked by At

I'm using whenever gem for cronjobs in rails application - production. I'm getting an error bundler: not executable: bin/rails

scheduler.rb

every 15.minute do
 runner 'TestJob.perform_later()'
end

crontab

0,15,30,45 * * * * /bin/bash -l -c 'cd /home/deploy/my-app/releases/20190719103116 && bundle exec bin/rails runner -e production '\''TestJob.perform_later()'\'''

but when i run /bin/bash -l -c 'cd /home/deploy/my-app/releases/20190719103116 && bundle exec bin/rails runner -e production '\''TestJob.perform_later()'\''' in my bash replacing bin/rails with just rails this work fine.How to fix this?

2

There are 2 best solutions below

2
On BEST ANSWER

Try running it via a rake task:

config/schedule.rb

every 15.minutes do
  rake 'testing:run_tests'
end

lib/tasks/testing.rake

namespace :testing do
  desc 'Run tests'
  task run_tests: :environment do
    TestJob.perform_later
  end
end
0
On

Quick Answer

With the intent of still using the original runner job type, simply add this to your schedule.rb:

set :runner_command, "rails runner"

Long Answer

The documentation isn't so clear about it, but you can see the default runner job type being set as such in Whenever's /lib/whenever/setup.rb:

job_type :runner, "cd :path && :bundle_command :runner_command -e :environment ':task' :output"

In that same file you can see that this :runner_command setting is set with:

set :runner_command, case
  when Whenever.bin_rails?
    "bin/rails runner"
  when Whenever.script_rails?
    "script/rails runner"
  else
    "script/runner"
  end

So by default your cron command will be created using bin/rails runner unless you overide it with the above answer.