LoginSignup
2
1

More than 5 years have passed since last update.

【Unicorn】unicornherderをつかってみる

Last updated at Posted at 2016-06-22

supervisord + unicornでhot restart (deploy) するを参考にunicornherderを使ってみる。

Install

pip install unicornherder

Unicorn

gem 'unicorn'
config/unicorn.rb
# -*- coding: utf-8 -*-
worker_processes Integer(ENV['WEB_CONCURRENCY'] || 2)
timeout 150
preload_app false

listen '/tmp/unicorn.sock'
pid '/tmp/unicorn.pid'

stderr_path File.expand_path('log/unicorn.log', ENV['RAILS_ROOT'])
stdout_path File.expand_path('log/unicorn.log', ENV['RAILS_ROOT'])

unicornherderで起動しない場合

bundle exec unicorn_rails -c config/unicorn.rb -D

プロセス確認(before)

vagrant@vagrant-ubuntu-trusty-64:~/sample$ ps -ef | grep unicorn 
vagrant   3857  1533  0 17:09 pts/4    00:00:00 tail -f log/unicorn.log
vagrant   3867     1  0 17:09 ?        00:00:00 unicorn_rails master -c config/unicorn.rb -D                                                   
vagrant   3923  3867  0 17:11 ?        00:00:01 unicorn_rails worker[1] -c config/unicorn.rb -D                                                
vagrant   3926  3867  0 17:11 ?        00:00:01 unicorn_rails worker[0] -c config/unicorn.rb -D                                                
vagrant   3979   551  0 17:18 pts/1    00:00:00 grep --color=auto unicorn

シグナル送ってみる

vagrant@vagrant-ubuntu-trusty-64:~/sample$ kill -HUP 3867

リロードされる

log/unicorn.log
I, [2016-06-22T17:20:09.214116 #3867]  INFO -- : reloading config_file=config/unicorn.rb
I, [2016-06-22T17:20:09.220745 #3867]  INFO -- : done reloading config_file=config/unicorn.rb
I, [2016-06-22T17:20:09.245587 #3867]  INFO -- : reaped #<Process::Status: pid 3923 exit 0> worker=1
I, [2016-06-22T17:20:09.246027 #3867]  INFO -- : worker=1 spawning...
I, [2016-06-22T17:20:09.246809 #3867]  INFO -- : reaped #<Process::Status: pid 3926 exit 0> worker=0
I, [2016-06-22T17:20:09.247190 #3867]  INFO -- : worker=0 spawning...
I, [2016-06-22T17:20:09.248149 #4014]  INFO -- : worker=1 spawned pid=4014
I, [2016-06-22T17:20:09.248491 #4014]  INFO -- : Refreshing Gem list
I, [2016-06-22T17:20:09.253035 #4016]  INFO -- : worker=0 spawned pid=4016
I, [2016-06-22T17:20:09.253528 #4016]  INFO -- : Refreshing Gem list
I, [2016-06-22T17:20:11.482024 #4014]  INFO -- : worker=1 ready
I, [2016-06-22T17:20:11.485565 #4016]  INFO -- : worker=0 ready

プロセス確認(after)
リロードされてworkerのプロセスIDだけ変わる(masterのプロセスIDは同じ)

vagrant@vagrant-ubuntu-trusty-64:~/sample$ ps -ef | grep unicorn 
vagrant   3857  1533  0 17:09 pts/4    00:00:00 tail -f log/unicorn.log
vagrant   3867     1  0 17:09 ?        00:00:00 unicorn_rails master -c config/unicorn.rb -D                                                   
vagrant   4014  3867  4 17:20 ?        00:00:01 unicorn_rails worker[1] -c config/unicorn.rb -D                                                
vagrant   4016  3867  4 17:20 ?        00:00:01 unicorn_rails worker[0] -c config/unicorn.rb -D                                                
vagrant   4021   551  0 17:20 pts/1    00:00:00 grep --color=auto unicorn

unicornherderで起動した場合

bundle exec unicornherder -u unicorn_rails -p /tmp/unicorn.pid -- -c config/unicorn.rb

--のあとにunicorn_railsに渡していたパラメータつければいいみたい

プロセス確認(before)

vagrant   4085  1462  2 17:34 pts/3    00:00:00 /usr/bin/python /usr/local/bin/unicornherder -u unicorn_rails -p /tmp/unicorn.pid -- -c config/unicorn.rb
vagrant   4092     1  0 17:34 ?        00:00:00 unicorn_rails master -D -c config/unicorn.rb                                                   
vagrant   4095  4092 12 17:34 ?        00:00:01 unicorn_rails worker[0] -D -c config/unicorn.rb                                                
vagrant   4097  4092 12 17:34 ?        00:00:01 unicorn_rails worker[1] -D -c config/unicorn.rb  

シグナル送ってみる(unicornherderの方に送る

vagrant@vagrant-ubuntu-trusty-64:~/sample$ kill -HUP 4085

Unicorn_Rails changed PID (was 4092, now 4104)って出る。

vagrant@vagrant-ubuntu-trusty-64:~/sample$ bundle exec unicornherder -u unicorn_rails -p /tmp/unicorn.pid -- -c config/unicorn.rb
2016-06-22 17:34:26,527  INFO      Unicorn_Rails booted (PID 4092)
2016-06-22 17:35:11,476  INFO      Caught HUP: gracefully restarting PID 4092
2016-06-22 17:35:13,479  INFO      Unicorn_Rails changed PID (was 4092, now 4104)

ログを見るとリロードじゃなくてforked child re-executing...になる

log/unicorn.log
I, [2016-06-22T17:35:11.481850 #4104]  INFO -- : executing ["/home/vagrant/sample/vendor/bundle/ruby/2.3.0/bin/unicorn_rails", "-D", "-c", "config/unicorn.rb", {10=>#<Kgio::UNIXServer:/tmp/unicorn.sock>}] (in /home/vagrant/sample)
I, [2016-06-22T17:35:11.482666 #4104]  INFO -- : forked child re-executing...
I, [2016-06-22T17:35:11.736018 #4104]  INFO -- : inherited addr=/tmp/unicorn.sock fd=10
I, [2016-06-22T17:35:11.737805 #4104]  INFO -- : worker=0 spawning...
I, [2016-06-22T17:35:11.739196 #4104]  INFO -- : worker=1 spawning...
I, [2016-06-22T17:35:11.739949 #4104]  INFO -- : master process ready
I, [2016-06-22T17:35:11.740738 #4108]  INFO -- : worker=0 spawned pid=4108
I, [2016-06-22T17:35:11.741065 #4108]  INFO -- : Refreshing Gem list
I, [2016-06-22T17:35:11.747837 #4110]  INFO -- : worker=1 spawned pid=4110
I, [2016-06-22T17:35:11.748172 #4110]  INFO -- : Refreshing Gem list
I, [2016-06-22T17:35:13.840608 #4108]  INFO -- : worker=0 ready
I, [2016-06-22T17:35:13.843696 #4110]  INFO -- : worker=1 ready

masterのプロセスが二つになる(oldとnew)

vagrant   4085  1462  0 17:34 pts/3    00:00:00 /usr/bin/python /usr/local/bin/unicornherder -u unicorn_rails -p /tmp/unicorn.pid -- -c config/unicorn.rb
vagrant   4092     1  0 17:34 ?        00:00:00 unicorn_rails master (old) -D -c config/unicorn.rb                                             
vagrant   4095  4092  1 17:34 ?        00:00:01 unicorn_rails worker[0] -D -c config/unicorn.rb                                                
vagrant   4097  4092  1 17:34 ?        00:00:01 unicorn_rails worker[1] -D -c config/unicorn.rb                                                
vagrant   4101  1533  0 17:34 pts/4    00:00:00 tail -f log/unicorn.log
vagrant   4104  4092  3 17:35 ?        00:00:00 unicorn_rails master -D -c config/unicorn.rb                                                   
vagrant   4108  4104 12 17:35 ?        00:00:01 unicorn_rails worker[0] -D -c config/unicorn.rb                                                
vagrant   4110  4104 12 17:35 ?        00:00:01 unicorn_rails worker[1] -D -c config/unicorn.rb   

しばらくするとoldが消えて新しい方のmasterだけ残る

vagrant   4085  1462  0 17:34 pts/3    00:00:00 /usr/bin/python /usr/local/bin/unicornherder -u unicorn_rails -p /tmp/unicorn.pid -- -c config/unicorn.rb
vagrant   4101  1533  0 17:34 pts/4    00:00:00 tail -f log/unicorn.log
vagrant   4104     1  0 17:35 ?        00:00:00 unicorn_rails master -D -c config/unicorn.rb                                                   
vagrant   4108  4104  0 17:35 ?        00:00:01 unicorn_rails worker[0] -D -c config/unicorn.rb                                                
vagrant   4110  4104  0 17:35 ?        00:00:01 unicorn_rails worker[1] -D -c config/unicorn.rb     

hot restartできそう。

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