Last active
August 29, 2015 13:59
-
-
Save 1987yama3/10648453 to your computer and use it in GitHub Desktop.
unicornの導入
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace :unicorn do | |
## | |
# Tasks | |
desc "Start unicorn" | |
task(:start) { | |
config = rails_root + "config/unicorn.rb" | |
env = ENV['RAILS_ENV'] || "development" | |
sh "bundle exec unicorn_rails -D -c #{config} -E #{env}" | |
} | |
desc "Stop unicorn" | |
task(:stop) { unicorn_signal :QUIT } | |
desc "Restart unicorn with USR2" | |
task(:restart) { unicorn_signal :USR2 } | |
desc "Increment number of worker processes" | |
task(:increment) { unicorn_signal :TTIN } | |
desc "Decrement number of worker processes" | |
task(:decrement) { unicorn_signal :TTOU } | |
desc "Unicorn pstree (depends on pstree command)" | |
task(:pstree) do | |
sh "pstree '#{unicorn_pid}'" | |
end | |
## | |
# Helpers | |
def unicorn_signal signal | |
Process.kill signal, unicorn_pid | |
end | |
def unicorn_pid | |
begin | |
File.read(rails_root + "tmp/pids/unicorn.pid").to_i | |
rescue Errno::ENOENT | |
raise "Unicorn doesn't seem to be running" | |
end | |
end | |
def rails_root | |
require "pathname" | |
Pathname.new(__FILE__) + "../" | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
worker_processes Integer(ENV["WEB_CONCURRENCY"] || 3) # 子プロセスいくつ立ち上げるか | |
timeout 15 #15秒Railsが反応しなければWorkerをkillしてタイムアウト | |
preload_app true #後述 | |
# 同一マシンでNginxとプロキシ組むならsocketのが高速ぽい(後述ベンチ) | |
# listen /path/to/rails/tmp/unicorn.sock | |
listen 3000 | |
# pid file path Capistranoとか使う時は要設定か | |
# pid /path/to/rails/tmp/pids/unicorn.pid | |
# ログの設定方法. | |
#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| | |
old_pid = "#{ server.config[: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 | |
defined?(ActiveRecord::Base) and | |
ActiveRecord::Base.connection.disconnect! | |
end | |
after_fork do |server, worker| | |
Signal.trap 'TERM' do | |
puts 'Unicorn worker intercepting TERM and doing nothing. Wait for master to send QUIT' | |
end | |
defined?(ActiveRecord::Base) and | |
ActiveRecord::Base.establish_connection | |
end | |
def rails_root | |
require "pathname" | |
Pathname.new(__FILE__) + "../../" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment