Herokuでrails runner が実行されない

local の develop 環境では動いているがheroku(production)では実行されない

結論

  • Rails5 では production 環境の autoload で lib を対象としない
  • application.rb に eager_load を指定し、lib を対象とするようにした

やろうとしていた事

  • rails runner で lib/tasks/xxx.rb の実行
  • xxx.rb の中では puts “debug!” のような処理
  • heroku logs には 以下のログがあり、exit 0 にならない
2017-02-11T17:51:30.739318+00:00 heroku[run.7502]: Awaiting client
2017-02-11T17:51:30.766742+00:00 heroku[run.7502]: Starting process with command `rails runner Tasks::Batch.execute`
2017-02-11T17:51:30.959538+00:00 heroku[run.7502]: State changed from starting to up
2017-02-11T17:51:37.230180+00:00 heroku[run.7502]: Process exited with status 1
2017-02-11T17:51:37.277912+00:00 heroku[run.7502]: State changed from up to complete

試した事

  • develop では 正常に処理が走る
    • rails runner Tasks::Batch.execute
  • production では NG
    • heroku run rails runner Tasks::Batch.execute
  • app の lib ではない directory に配置したファイルであれば、実行出来る
  • puts なども叩ける
    • heroku run rails runner -e production “p\(1\)”
Please specify a valid ruby command or the path of a script to run.
Run 'bin/rails runner -h' for help.

雑感

少しずつ切り分けをしてproduction だけで発生する事から環境依存という事はつかめていたが、
application.rb や config したのファイルをキーにググる事数時間。

参照

Rails runnerを使ってファイルを実行しました - Qiita

rails runnerを使ってみた - Qiita

Railsでlib以下を読み込ませる方法とその注意点 〜名前重複の死を避けるために〜 - Qiita

Rails5: production環境でのAutoloadの廃止 - Qiita