rails(3.2.18)アプリunicornで起動しているときに、そのunicornのプロセスをeyeでモニタリングする方法。
準備
Railsプロジェクト上であること前提。
unicorn.conf を置く
https://github.com/defunkt/unicorn/blob/master/examples/unicorn.conf.rb
あたりを参考に
config/unicorn.rb
worker_processes 3
preload_app true
timeout 15
listen 3000
pid File.expand_path('tmp/unicorn.pid', ENV['RAILS_ROOT'])
stderr_path File.expand_path('log/unicorn.log', ENV['RAILS_ROOT'])
stdout_path File.expand_path('log/unicorn.log', ENV['RAILS_ROOT'])
before_fork do |server, worker|
defined?(ActiveRecord::Base) and
ActiveRecord::Base.connection.disconnect!
old_pid = "#{server.config[:pid]}.oldbin"
begin
sig = (worker.nr + 1) >= server.worker_processes ? :QUIT : :TTOU
Process.kill(sig, File.read(old_pid).to_i)
rescue Errno::ENOENT, Errno::ESRCH
end
end
after_fork do |server, worker|
defined?(ActiveRecord::Base) and
ActiveRecord::Base.establish_connection
end
eyeのconfigファイルの作成
https://github.com/kostya/eye/blob/master/examples/unicorn.eye を参考に
unicorn.eye
# Example: now to run unicorn, and monitor its child processes
RUBY = '/usr/bin/ruby'
RAILS_ENV = 'development'
working_dir = File.expand_path(File.join(File.dirname(__FILE__), '..'))
# Eye self-configuration section
Eye.config do
logger "#{working_dir}/log/eye.log"
end
Eye.application "rails_unicorn” do
env "RAILS_ENV" => RAILS_ENV
# unicorn requires to be `ruby` in path (for soft restart)
env "PATH" => "#{File.dirname(RUBY)}:#{ENV['PATH']}"
working_dir "#{working_dir}"
process("unicorn") do
pid_file "tmp/unicorn.pid"
start_command "bundle exec unicorn_rails -c ./config/unicorn.rb -E #{RAILS_ENV} -D"
stdall "log/unicorn.log"
# stop signals:
# http://unicorn.bogomips.org/SIGNALS.html
stop_signals [:TERM, 10.seconds]
# soft restart
restart_command "kill -USR2 {PID}"
check :cpu, :every => 30, :below => 80, :times => 3
check :memory, :every => 30, :below => 150.megabytes, :times => [3,5]
start_timeout 100.seconds
restart_grace 30.seconds
monitor_children do
stop_command "kill -QUIT {PID}"
check :cpu, :every => 30, :below => 80, :times => 3
check :memory, :every => 30, :below => 150.megabytes, :times => [3,5]
end
end
end
eyeの起動
% bundle exec eye load config/unicorn.eye
正常に立ち上がると、以下のような表示に。
% bundle exec eye info
rails_unicorn
unicorn ......................... up (17:14, 0%, 3Mb, <64019>)
child-64023 ................... up (17:14, 0%, 1Mb, <64023>)
child-64022 ................... up (17:14, 0%, 1Mb, <64022>)
child-64021 ................... up (17:14, 0%, 1Mb, <64021>)
eye trace
でログ確認も可能。
% bundle exec eye trace unicorn
21.06.2014 17:27:45 INFO -- [rails_unicorn:unicorn:child-64021] check:cpu(<80%) [0%, 0%, 0%] => OK
21.06.2014 17:27:45 INFO -- [rails_unicorn:unicorn:child-64021] check:memory(<150Mb) [1Mb, 1Mb, 1Mb, 1Mb, 1Mb] => OK
21.06.2014 17:28:03 INFO -- [rails_unicorn:unicorn] check:cpu(<80%) [0%, 0%] => OK
21.06.2014 17:28:03 INFO -- [rails_unicorn:unicorn] check:memory(<150Mb) [2Mb, 2Mb] => OK
21.06.2014 17:28:15 INFO -- [rails_unicorn:unicorn:child-64023] check:cpu(<80%) [0%, 0%, 0%] => OK
21.06.2014 17:28:15 INFO -- [rails_unicorn:unicorn:child-64022] check:cpu(<80%) [0%, 0%, 0%] => OK
他にも eye xinfo -c
で、現在の設定も確認出来る。