LoginSignup
33
35

More than 5 years have passed since last update.

Capistrano+Nginx+UnicornのUnicornの設定

Last updated at Posted at 2014-05-11

ほとんどはここ通りにやればいいんですが、unicornの設定がなかったので追加しておきます。

unicorn + rails 用 Capistrano 3 の設定ファイル - Qiita

知っといたほうがいいこと

Unicornは1秒たりともサービスを停止させずにdeployできる気の利いた再起動方法があります。
詳しくはここらへんで。

ruby on rails - Unicorn not reloading with USR2 - Stack Overflow

んで、古いサービスはどうやって落とすかというとUnicorn自体の設定で古いのを見つけたら殺すようにします。

その設定

config/unicorn/production.rb
root = "/var/www/example.com/current"
working_directory root
pid "#{root}/tmp/pids/unicorn.pid"
stderr_path "#{root}/log/unicorn.log"
stdout_path "#{root}/log/unicorn.log"

listen "/tmp/unicorn.example.sock"
worker_processes 3
timeout 30

before_exec do |server|
  ENV['BUNDLE_GEMFILE'] = "#{root}/Gemfile"
end

before_fork do |server, worker|
  # the following is highly recomended for Rails + "preload_app true"
  # as there's no need for the master process to hold a connection
  if defined?(ActiveRecord::Base)
    ActiveRecord::Base.connection.disconnect!
  end

  # Before forking, kill the master process that belongs to the .oldbin PID.
  # This enables 0 downtime deploys.
  old_pid = "#{root}/tmp/pids/unicorn.pid.oldbin"
  if File.exists?(old_pid) && server.pid != old_pid
    begin
      Process.kill("QUIT", File.read(old_pid).to_i)
    rescue Errno::ENOENT, Errno::ESRCH
      # someone else did our job for us
    end
  end
end

after_fork do |server, worker|
  # the following is *required* for Rails + "preload_app true",
  if defined?(ActiveRecord::Base)
    ActiveRecord::Base.establish_connection
  end

  # if preload_app is true, then you may also want to check and
  # restart any other shared sockets/descriptors such as Memcached,
  # and Redis.  TokyoCabinet file handles are safe to reuse
  # between any number of forked children (assuming your kernel
  # correctly implements pread()/pwrite() system calls)
end

参考

全体の流れを知りたい場合はここ。

"Railsのdeployは簡単さ!"とか偉い人は言ってるそうですが、それは出来杉君が「勉強なんてやればできるさ!」とのび太に言うようなもんだと思います。

複数の要素が絡みまくってる上にしょっちゅう新しいのに移行するからややこしいんだよ!クリックひとつでデプロイしたい!

(Herokuは簡単だけど詰まった時にCapistranoとかよりヤバそうなので使ってません。)

33
35
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
33
35