0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

unicorn 導入手順書

Last updated at Posted at 2015-12-28

unicorn 導入手順書

#!/usr/bin/ruby ruby
## config/unicorn.rb

# pidファイルの場所
path_pid = "unicorn.pid"

# ログ
path_stdout_log = "log/unicorn.log"
path_stderr_log = "log/unicorn.log"

# port
port = 8080
# socket
path_socket = "/tmp/unicorn.sock"


# ===== 以下は基本的にあたる必要なし =====
# from https://github.com/defunkt/unicorn/blob/master/examples/unicorn.conf.rb

rails_root = File.expand_path('../../', __FILE__)
ENV['BUNDLE_GEMFILE'] = rails_root + "/Gemfile"

# Use at least one worker per core if you're on a dedicated server,
# more will usually help for _short_ waits on databases/caches.
worker_processes 4

# 変数が存在している場合のみ、listenする
listen path_socket, :backlog => 64 if !defined?(path_socket).empty?
listen port,        :tcp_nopush => true   if !defined?(port).empty?

working_directory rails_root

# 接続タイムアウト時間
timeout 30

# unicornのタイムアウト時には、リクエストのログは一切production.logには出力されない。
# なので、それを強制的に吐き出す処理を追加する
# INTシグラルでトラップするようにしている
timeout_signal :INT
after_fork do |server, worker|
  trap (:INT) do
    Rails.logger.error "Unicorn Timeout (trapped SIGINT)"
    Rails.logger.flush
    Process.kill :KILL, $$
  end
end

pid path_pid

# unicornログの場所を指定する
stdout_path path_stdout_log
stderr_path path_stderr_log


# 基本的には`true`を指定する。Unicornの再起動時にダウンタイムなしで再起動が行われる。
preload_app true

# これをする理由は、
# http://techracho.bpsinc.jp/baba/2012_08_29/6001
# をみると、解りやすい
before_fork do |server, worker|
  defined?(ActiveRecord::Base) and
    ActiveRecord::Base.connection.disconnect!
end

after_fork do |server, worker|
  defined?(ActiveRecord::Base) and ActiveRecord::Base.establish_connection
end

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?