LoginSignup
5
3

More than 5 years have passed since last update.

Rails + whenever で failed to load command: bin/rails エラー

Posted at

概要

gem whenever を使って Rails で定時処理を作る
という記事などを参考に、定時処理を実装していたら、
runner 実行時に bundler: failed to load command: bin/rails (bin/rails)
というエラーに襲われてはまったので解決法をメモ

環境

  • Docker for mac 17.03
  • ベースイメージ: rails/app Debian GNU/Linux 8
  • Rails 5.0.0.1
  • Whenever 0.9.7

症状

lib/tasks 以下に test.rb というタスクを作成
(単に "yakisoba" と出力する機能しかないタスク)

lib/tasks/test.rb
class Test
  def self.yakisoba
    puts "yakisoba"
  end
end

これを実行するようにスケジュールに設定

config/schedule.rb
set :output, '/usr/src/app/log/crontab.log'

every 1.minute do
  runner "Test.yakisoba", :environment => :development
end

こうすることで、 1分おきに /usr/src/app/log/crontab.log
yakisoba と出力してくれるはず。

っと思ったらログファイルにダラダラと以下のようなエラーが。

log/crontab.log
bundler: failed to load command: bin/rails (bin/rails)
Bundler::GemNotFound: Your bundle is locked to rake (12.0.0), but ...
/usr/local/lib/ruby/gems/2.3.0/gems/...
/usr/local/lib/ruby/gems/2.3.0/gems/...

(´・ω・` )

解決法

schedule.rb 内で環境変数を読み込ませる。

config/schedule.rb
set :output, '/usr/src/app/log/crontab.log'
ENV.each { |k, v| env(k, v) } #追加

every 1.minute do
  runner "Test.yakisoba", :environment => :development
end

これでちゃんと動いた(ノ_-。)

参考

Can I use Whenever to run scheduled tasks (cron jobs) on Enclave?

5
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
3