9
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

rails4のinitializerの読み込み順序を検証

Last updated at Posted at 2015-10-26

はじめに

この記事は、rails4のinitializerの読み込み順序を検証するものです。補足でinitializer以外の呼び出し順も明記しています。

officialでの説明

If you have any ordering dependency in your initializers, you can control the load order through naming. Initializer files are loaded in alphabetical order by their path. For example, 01_critical.rb will be loaded before 02_normal.rb.

もしinitializersに依存順位を持たせたかったら、ファイル名によって制御できます。Initializerファイルはそのパスのアルファベット順で読み込まれます。例えば、01_ciritcal.rbは02_normal.rbより先に読み込まれます。

実際の挙動

各イニシャライザの実行タイミングを知るため、config/initializers/*.rbの最終行に下記を追加しました。

echo #{__FILE__} >> /users/imasami/work/rails_app/order/log/seq.log

標準のイニシャライザの読み込みタイミングを知るため、具体的には下記コマンドを叩きました。

echo '1' >> /users/imasami/work/rails_app/order/log/seq.log
bundle exec rails s

次に、config/initializers/aa.rbconfig/initializers/zz.rbconfig/initializers/1z.rbconfig/initializers/0/zzzzzzz.rbを追加し、同様に最終行に前述の一行を追加して下記を実行。

echo '2' >> /users/imasami/work/rails_app/order/log/seq.log
bundle exec rails s

出力結果

config/initializers/以下のパスがアルファベット順(数字優先)で読み込まれたことを確認出来ました。

1
/users/imasami/work/rails_app/order/config/initializers/assets.rb
/users/imasami/work/rails_app/order/config/initializers/backtrace_silencers.rb
/users/imasami/work/rails_app/order/config/initializers/cookies_serializer.rb
/users/imasami/work/rails_app/order/config/initializers/filter_parameter_logging.rb
/users/imasami/work/rails_app/order/config/initializers/inflections.rb
/users/imasami/work/rails_app/order/config/initializers/mime_types.rb
/users/imasami/work/rails_app/order/config/initializers/session_store.rb
/users/imasami/work/rails_app/order/config/initializers/wrap_parameters.rb
2
/users/imasami/work/rails_app/order/config/initializers/0/zzzzzzz.rb
/users/imasami/work/rails_app/order/config/initializers/1z.rb
/users/imasami/work/rails_app/order/config/initializers/aa.rb
/users/imasami/work/rails_app/order/config/initializers/assets.rb
/users/imasami/work/rails_app/order/config/initializers/backtrace_silencers.rb
/users/imasami/work/rails_app/order/config/initializers/cookies_serializer.rb
/users/imasami/work/rails_app/order/config/initializers/filter_parameter_logging.rb
/users/imasami/work/rails_app/order/config/initializers/inflections.rb
/users/imasami/work/rails_app/order/config/initializers/mime_types.rb
/users/imasami/work/rails_app/order/config/initializers/session_store.rb
/users/imasami/work/rails_app/order/config/initializers/wrap_parameters.rb
/users/imasami/work/rails_app/order/config/initializers/zz.rb

補足1: SECRET_KEY_BASEを参照するタイミングはinitializerを全部読み込んでその後

SECRET_KEY_BASEをconfig/initializers/zz.rbの最終行で定義してやり、RAILS_ENV=productionで起動後、localhost:3000に接続しても、secret_key_baseが見つからないというエラーはログに出てきませんでした(代わりに、ActionController::RoutingError (No route matches [GET] "/"):が出力されました)。

補足2

RAILS_ENV=production bundle exec rails s実行のみでlocalhost:3000へのGETがない場合の呼び出し順ログは下記の通りでした。(unicorn実行時の比較のために空行を追加しています)

bin/rails
/users/imasami/work/rails_app/order/bin/spring
/users/imasami/work/rails_app/order/bin/rails
/users/imasami/work/rails_app/order/bin/spring

/users/imasami/work/rails_app/order/config/boot.rb
/users/imasami/work/rails_app/order/config/application.rb
/users/imasami/work/rails_app/order/config/environment.rb

/users/imasami/work/rails_app/order/config/environments/production.rb
/users/imasami/work/rails_app/order/config/initializers/0/zzzzzzz.rb
/users/imasami/work/rails_app/order/config/initializers/1z.rb
/users/imasami/work/rails_app/order/config/initializers/aa.rb
/users/imasami/work/rails_app/order/config/initializers/assets.rb
/users/imasami/work/rails_app/order/config/initializers/backtrace_silencers.rb
/users/imasami/work/rails_app/order/config/initializers/cookies_serializer.rb
/users/imasami/work/rails_app/order/config/initializers/filter_parameter_logging.rb
/users/imasami/work/rails_app/order/config/initializers/inflections.rb
/users/imasami/work/rails_app/order/config/initializers/mime_types.rb
/users/imasami/work/rails_app/order/config/initializers/session_store.rb
/users/imasami/work/rails_app/order/config/initializers/wrap_parameters.rb
/users/imasami/work/rails_app/order/config/initializers/zz.rb

/users/imasami/work/rails_app/order/app/controllers/application_controller.rb
/users/imasami/work/rails_app/order/app/helpers/application_helper.rb

/users/imasami/work/rails_app/order/config/routes.rb

config/environments/*.rbでloggerを定義するだろうから、initializerからRails.loggerは参照可能なのかもしれませんね。

補足3

unicornを実行した場合は下記の通りでした。(webrickとの比較のために空行を追加しています)

各行の末尾の数字は、1行目はmaster pid、2行目以降はworker pidです。workerは3つ起動する設定です。

  • 標準のwebrickとくらべて下記の読み込み順が違いますね。
    • config/boot.rb
    • config/application.rb
    • config/environment.rb
  • 他には下記が読み込まれてません。
    • app/controllers/application_controller.rb
    • app/helpers/application_helper.rb
config/unicorn.rb pid: 95309

/Users/imasami/work/rails_app/order/config/environment.rb pid: 95711
/Users/imasami/work/rails_app/order/config/environment.rb pid: 95712
/Users/imasami/work/rails_app/order/config/environment.rb pid: 95713
/Users/imasami/work/rails_app/order/config/application.rb pid: 95713
/Users/imasami/work/rails_app/order/config/application.rb pid: 95712
/Users/imasami/work/rails_app/order/config/application.rb pid: 95711
/Users/imasami/work/rails_app/order/config/boot.rb pid: 95713
/Users/imasami/work/rails_app/order/config/boot.rb pid: 95712
/Users/imasami/work/rails_app/order/config/boot.rb pid: 95711

/Users/imasami/work/rails_app/order/config/environments/production.rb pid: 95713
/Users/imasami/work/rails_app/order/config/environments/production.rb pid: 95712
/Users/imasami/work/rails_app/order/config/environments/production.rb pid: 95711
/Users/imasami/work/rails_app/order/config/initializers/0/zzzzzzz.rb pid: 95711
/Users/imasami/work/rails_app/order/config/initializers/0/zzzzzzz.rb pid: 95713
/Users/imasami/work/rails_app/order/config/initializers/0/zzzzzzz.rb pid: 95712
/Users/imasami/work/rails_app/order/config/initializers/1z.rb pid: 95713
/Users/imasami/work/rails_app/order/config/initializers/1z.rb pid: 95711
/Users/imasami/work/rails_app/order/config/initializers/1z.rb pid: 95712
/Users/imasami/work/rails_app/order/config/initializers/aa.rb pid: 95711
/Users/imasami/work/rails_app/order/config/initializers/aa.rb pid: 95712
/Users/imasami/work/rails_app/order/config/initializers/aa.rb pid: 95713
/Users/imasami/work/rails_app/order/config/initializers/assets.rb pid: 95711
/Users/imasami/work/rails_app/order/config/initializers/assets.rb pid: 95712
/Users/imasami/work/rails_app/order/config/initializers/assets.rb pid: 95713
/Users/imasami/work/rails_app/order/config/initializers/backtrace_silencers.rb pid: 95711
/Users/imasami/work/rails_app/order/config/initializers/backtrace_silencers.rb pid: 95712
/Users/imasami/work/rails_app/order/config/initializers/backtrace_silencers.rb pid: 95713
/Users/imasami/work/rails_app/order/config/initializers/cookies_serializer.rb pid: 95713
/Users/imasami/work/rails_app/order/config/initializers/cookies_serializer.rb pid: 95711
/Users/imasami/work/rails_app/order/config/initializers/cookies_serializer.rb pid: 95712
/Users/imasami/work/rails_app/order/config/initializers/filter_parameter_logging.rb pid: 95713
/Users/imasami/work/rails_app/order/config/initializers/filter_parameter_logging.rb pid: 95712
/Users/imasami/work/rails_app/order/config/initializers/filter_parameter_logging.rb pid: 95711
/Users/imasami/work/rails_app/order/config/initializers/inflections.rb pid: 95711
/Users/imasami/work/rails_app/order/config/initializers/inflections.rb pid: 95713
/Users/imasami/work/rails_app/order/config/initializers/inflections.rb pid: 95712
/Users/imasami/work/rails_app/order/config/initializers/mime_types.rb pid: 95712
/Users/imasami/work/rails_app/order/config/initializers/mime_types.rb pid: 95711
/Users/imasami/work/rails_app/order/config/initializers/mime_types.rb pid: 95713
/Users/imasami/work/rails_app/order/config/initializers/session_store.rb pid: 95712
/Users/imasami/work/rails_app/order/config/initializers/session_store.rb pid: 95713
/Users/imasami/work/rails_app/order/config/initializers/session_store.rb pid: 95711
/Users/imasami/work/rails_app/order/config/initializers/wrap_parameters.rb pid: 95712
/Users/imasami/work/rails_app/order/config/initializers/wrap_parameters.rb pid: 95713
/Users/imasami/work/rails_app/order/config/initializers/wrap_parameters.rb pid: 95711
/Users/imasami/work/rails_app/order/config/initializers/zz.rb pid: 95711
/Users/imasami/work/rails_app/order/config/initializers/zz.rb pid: 95713
/Users/imasami/work/rails_app/order/config/initializers/zz.rb pid: 95712

/Users/imasami/work/rails_app/order/config/routes.rb pid: 95713
/Users/imasami/work/rails_app/order/config/routes.rb pid: 95711
/Users/imasami/work/rails_app/order/config/routes.rb pid: 95712

補足4

他のケースの読み込み順を確認したい場合は次のようにするといい。

  • brewでgnu-sedを入れる。 参照
$ find . -name '*.rb' -exec gsed -i -e "2i \`echo #{__FILE__} pid: #{$$} >> /path/to/seq.log\`" {} \;

配下の*.rbなファイルの2行目にフルパス入りのログ吐きコードを追加する。1番目にしないのは、shebangが使われてると影響を受けるかもしれないし、そうでないかもしれない不確かさが書いてる人の頭の中にあるから。

9
7
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
9
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?