unicorn を再起動せずにワーカープロセスの数を1つ増やす/減らす
概要
unicorn のワーカープロセスを変更するのに再起動していたら、
「それ、再起動しないでも出来るよ」って先輩に言われたので検証してみました。
- TTIN - increment the number of worker processes by one
- TTOU - decrement the number of worker processes by one
環境
実行結果
- ワーカープロセスは 2 つ
[root@localhost example]# ps auxwwf | grep -w [u]nicorn root 23609 0.7 8.0 688632 82084 ? Sl 14:54 0:00 unicorn_rails master -c config/unicorn.rb -D -E development root 23619 0.0 7.2 689660 73660 ? Sl 14:54 0:00 \_ unicorn_rails worker[0] -c config/unicorn.rb -D -E development root 23623 0.0 7.2 689660 73864 ? Sl 14:54 0:00 \_ unicorn_rails worker[1] -c config/unicorn.rb -D -E development
- TTIN でワーカープロセスの数を1つ増やす
[root@localhost example]# kill -s TTIN 23609
- ワーカープロセスは 3 つ、1つ増えた
[root@localhost example]# ps auxwwf | grep -w [u]nicorn root 23609 0.4 8.0 688632 82096 ? Sl 14:54 0:00 unicorn_rails master -c config/unicorn.rb -D -E development root 23619 0.0 7.2 689660 73660 ? Sl 14:54 0:00 \_ unicorn_rails worker[0] -c config/unicorn.rb -D -E development root 23623 0.0 7.2 689660 73864 ? Sl 14:54 0:00 \_ unicorn_rails worker[1] -c config/unicorn.rb -D -E development root 23630 0.0 7.2 689660 73692 ? Sl 14:58 0:00 \_ unicorn_rails worker[2] -c config/unicorn.rb -D -E development
- TTIN でワーカープロセスの数を1つ減らす
[root@localhost example]# kill -s TTOU 23609
- ワーカープロセスは 2 つ、1つ減った
[root@localhost example]# ps auxwwf | grep -w [u]nicorn root 23609 0.3 8.0 688632 82100 ? Sl 14:54 0:00 unicorn_rails master -c config/unicorn.rb -D -E development root 23619 0.0 7.2 689660 73660 ? Sl 14:54 0:00 \_ unicorn_rails worker[0] -c config/unicorn.rb -D -E development root 23623 0.0 7.2 689660 73864 ? Sl 14:54 0:00 \_ unicorn_rails worker[1] -c config/unicorn.rb -D -E development
- TTIN でワーカープロセスの数を1つ増やす
[root@localhost example]# kill -s TTIN 23609 [root@localhost example]# ps auxwwf | grep -w [u]nicorn root 23609 0.3 8.0 688632 82104 ? Sl 14:54 0:00 unicorn_rails master -c config/unicorn.rb -D -E development root 23619 0.0 7.2 689660 73660 ? Sl 14:54 0:00 \_ unicorn_rails worker[0] -c config/unicorn.rb -D -E development root 23623 0.0 7.2 689660 73864 ? Sl 14:54 0:00 \_ unicorn_rails worker[1] -c config/unicorn.rb -D -E development root 23638 0.0 7.2 689660 73764 ? Sl 14:58 0:00 \_ unicorn_rails worker[2] -c config/unicorn.rb -D -E development
- unicorn を再起動する
[root@localhost example]# kill -s QUIT 23609 [root@localhost example]# ps auxwwf | grep -w [u]nicorn [root@localhost example]# bundle exec unicorn_rails -c config/unicorn.rb -D -E development
- ワーカープロセスは 2 つ(設定ファイルの値)
[root@localhost example]# ps auxwwf | grep -w [u]nicorn root 23679 22.0 6.9 676368 70204 ? Sl 15:00 0:00 unicorn_rails master -c config/unicorn.rb -D -E development root 23689 0.0 6.0 611860 61804 ? Sl 15:00 0:00 \_ unicorn_rails worker[0] -c config/unicorn.rb -D -E development root 23693 0.0 6.0 677396 61704 ? Sl 15:00 0:00 \_ unicorn_rails worker[1] -c config/unicorn.rb -D -E development
まとめ
コマンド再起動するより断然いいですね。
注意としては設定ファイルを更新してあげないと再起動すると戻ってしまうので、
オンラインで変更したら設定ファイルの修正も忘れずに。
その他
kill
コマンドについて誤解してました。
プロセスを文字通り kill するコマンドと思っていたのですが、
man
コマンドの DESCRIPTION を読むと指定されたシグナルを
指定されたプロセスまたはプロセスグループに送信すると書いてあるので、
kill するだけのコマンドではないんですね。
とはいえ man
コマンドの NAME だけ見ると勘違いするよね・・・
NAME kill - terminate a process
gunicorn は未検証ですがドキュメント見る限り同じことができそう。
Signal Handling — Gunicorn 19.9.0 documentation
参考
[何となく知ってたけど再確認] capistrano3-unicornがunicornに送るシグナルまとめ
検証してから先人のもっと詳しい記事を見つけました