LoginSignup
2
1

More than 5 years have passed since last update.

unicorn を再起動せずにワーカープロセスの数を1つ増やす/減らす

Posted at

概要

unicorn のワーカープロセスを変更するのに再起動していたら、
「それ、再起動しないでも出来るよ」って先輩に言われたので検証してみました。

unicorn/SIGNALS

  • TTIN - increment the number of worker processes by one
  • TTOU - decrement the number of worker processes by one

環境

  • CentOS 7.5
  • ruby 2.5.1
  • nginx 1.14.0
  • unicorn 5.4.1

実行結果

  1. ワーカープロセスは 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
    
  2. TTIN でワーカープロセスの数を1つ増やす

    [root@localhost example]# kill -s TTIN 23609
    
  3. ワーカープロセスは 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
    
  4. TTIN でワーカープロセスの数を1つ減らす

    [root@localhost example]# kill -s TTOU 23609
    
  5. ワーカープロセスは 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
    
  6. 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
    
  7. 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
    
  8. ワーカープロセスは 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 は未検証ですがドキュメント見る限り同じことができそう。

Gunicorn >> Docs » Signal Handling
https://docs.gunicorn.org/en/stable/signals.html

参考

[何となく知ってたけど再確認] capistrano3-unicornがunicornに送るシグナルまとめ
検証してから先人のもっと詳しい記事を見つけました

2
1
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
2
1