LoginSignup
15
9

More than 3 years have passed since last update.

【Rails】puma.rbの気になるあれこれ

Posted at

背景

Railsを起動する時に出てくる「puma」。気になったところを設定ファイルの「puma.rb」を中心に調べてみました。

 rails s
#railsを起動した時に目にする画面。Booting Pumaと書いてある。
=> Booting Puma
=> Rails 6.0.3.5 application starting in development
=> Run `rails server --help` for more startup options
Puma starting in single mode...
* Version 3.12.6 (ruby 2.6.5-p114), codename: Llamas in Pajamas
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://localhost:3000
Use Ctrl-C to stop
^C- Gracefully stopping, waiting for requests to finish
=== puma shutdown: 2021-04-17 07:49:47 +0900 ===
- Goodbye!

※内容に間違いなどがある場合はご指摘をよろしくお願いします。

そもそもPumaって何?

Railsを動かすアプリケーションサーバーの一つ。他にもMongrel、Unicorn、Thin、Rainbowsなど様々な種類がある。どれもアプリケーションを動かすために必要であり、書いたコードを読み込んで処理を行います。あくまでもアプリケーション内での動きに関わるらしく、Web上でアプリを起動させるためには、webサーバーが必要になります。

puma.rbの中身

max_threads_count = ENV.fetch("RAILS_MAX_THREADS") { 5 }
min_threads_count = ENV.fetch("RAILS_MIN_THREADS") { max_threads_count }

threads min_threads_count, max_threads_count

port        ENV.fetch("PORT") { 3000 }
environment ENV.fetch("RAILS_ENV") { "development" }
pidfile ENV.fetch("PIDFILE") { "tmp/pids/server.pid" }

plugin :tmp_restart

threads(スレッド)?

プログラムの処理の実行単位。これの数が増えると同時に実行できる処理の数が増えるみたいです。

max_threads_count = ENV.fetch("RAILS_MAX_THREADS") { 5 }
min_threads_count = ENV.fetch("RAILS_MIN_THREADS") { max_threads_count }

threads min_threads_count, max_threads_count

「max_threads_count」と「min_threads_count」に環境変数の値を代入し、「threads min_threads_count, max_threads_count」と書かれている記述によってpuma実行時に最大のスレッド数と最小のスレッド数を指定することができる。

port?

pumaを起動した時にlocal環境のport番号を指定する。最初に「3000」と書いてありますが、この番号を変更するとport番号をrailsを実行する時にport番号を変えられます。

#port番号を3000から3001に変更
port        ENV.fetch("PORT") { 3001 }

port番号3001にrailsが起動しています。
スクリーンショット 2021-04-17 8.37.57.png

environment?

Pumaが起動する実行環境を指定します。

environment ENV.fetch("RAILS_ENV") { "development" }

「ENV.fetch("RAILS_ENV"){"development"}」の記述で「RAILS_ENV」と言う名前の環境変数を読み込むかそれがない場合には「development」という値が指定されます。
「development」は開発環境を意味し、他に「test」、「production」などがあります。それぞれテスト環境と本番環境を意味します。

pidfile?

pid(Process ID)のことで、ここではpumaが起動した際の処理番号です。

pidfile ENV.fetch("PIDFILE") { "tmp/pids/server.pid" }

環境変数の「PIDFILE」の値を探し、なければ"tmp/pids/server.pid"の値が指定されます。場所は「railsアプリ名/tmp/pids/」です。server.pidをvscodeなどのエディターで開いてみる以下のように番号が書いてありました。

3157

コマンドラインで「ps -ef | grep puma」と入力し、pidを確認してみました。

  ps -ef | grep puma
  501  3157  2422   0  8:37AM ttys000    1:04.38 puma 3.12.6 (tcp://localhost:3001) [アプリ名]
  501  3700  3321   0  8:54AM ttys002    0:00.01 grep --color=auto --exclude-dir=.bzr --exclude-dir=CVS --exclude-dir=.git --exclude-dir=.hg --exclude-dir=.svn --exclude-dir=.idea --exclude-dir=.tox puma

先ほどのserver.pidファイルに書かれていた「3157」のところをみてみると、「8時37分にlocalhost:3001でアプリケーションが実行されている記録が確認できます。
railsをstopし、再度確認してみると、

  ps -ef | grep puma
  501  3792  3321   0  8:55AM ttys002    0:00.00 grep --color=auto --exclude-dir=.bzr --exclude-dir=CVS --exclude-dir=.git --exclude-dir=.hg --exclude-dir=.svn --exclude-dir=.idea --exclude-dir=.tox puma

pid「3157」という番号はありませんでした。どうやらプロセス(pumaを起動)がstopするとpidも削除されるみたいです。念のため、「railsアプリ名/tmp/pids/」のフォルダーをみてみたら、server.pidファイルが削除されていることが分かりました。

plugin

pumaに機能を追加する時に使うらしいです。最初から「tmp_restart」というプラグインが記述してあります。「tmp/restart.txtをtouchするとリスタートする」とか「rails restart」コマンドでpumaを再起動できるようにするプラグイン。実際にどういう使い方をするのかについてはあまり知られていません。(というか見つかりませんでした)
他には「puma-heroku」というheroku用の設定を用意してくれるプラグインもあります。

# Gemfile
gem 'puma-heroku'

# gemfileをインストール
bundle install

# config/puma.rb
plugin :heroku

参考サイト・記事

https://qiita.com/jnchito/items/3884f9a2ccc057f8f3a3
https://qiita.com/Orangina1050/items/9d816017217614bc3ce7
https://wa3.i-3-i.info/word12453.html
https://normalse.hatenablog.jp/entry/2019/04/09/083022
https://site-builder.wiki/posts/13878
https://github.com/puma/puma/blob/v3.12.0/lib/puma/plugin/tmp_restart.rb
http://nekorails.hatenablog.com/entry/2018/10/12/101011

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