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

# はじめに

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

# officialでの説明

- [Using Initializer Files](http://guides.rubyonrails.org/configuring.html#using-initializer-files)

```
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.rb`と`config/initializers/zz.rb`と`config/initializers/1z.rb`と`config/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との比較のために空行を追加しています）

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

- 標準の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
```

## 補足４

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

- brewでgnu-sedを入れる。 [参照](http://qiita.com/toyama0919/items/661437d86a95b02484a2)

```
$ find . -name '*.rb' -exec gsed -i -e "2i \`echo #{__FILE__} pid: #{$$} >> /path/to/seq.log\`" {} \;
```

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