Skip to content

Instantly share code, notes, and snippets.

@cadwallion
Created June 5, 2012 03:35
Show Gist options
  • Save cadwallion/2872465 to your computer and use it in GitHub Desktop.
Save cadwallion/2872465 to your computer and use it in GitHub Desktop.
Unicorn config
rails_env = ENV['RAILS_ENV'] || 'production'
app = 'my_awesome_app'
# 4 workers and 1 master
worker_processes (rails_env == 'production' ? 4 : 1)
# Load rails+github.git into the master before forking workers
# for super-fast worker spawn times
preload_app true
# Restart any workers that haven't responded in 30 seconds
timeout 30
# Listen on a Unix data socket
case rails_env
when 'production' || 'staging'
listen "/var/rails/#{app}/tmp/sockets/#{rails_env}.sock", :backlog => 2048
else
listen "#{`pwd`.strip}/tmp/sockets/#{rails_env}.sock"
end
before_fork do |server, worker|
# When sent a USR2, Unicorn will suffix its pidfile with .oldbin and
# immediately start loading up a new version of itself (loaded with a new
# version of our app). When this new Unicorn is completely loaded
# it will begin spawning workers. The first worker spawned will check to
# see if an .oldbin pidfile exists. If so, this means we've just booted up
# a new Unicorn and need to tell the old one that it can now die. To do so
# we send it a QUIT.
#
# Using this method we get 0 downtime deploys.
old_pid = "#{Rails.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
defined?(ActiveRecord::Base) and ActiveRecord::Base.connection.disconnect!
end
after_fork do |server, worker|
# Unicorn master loads the app then forks off workers - because of the way
# Unix forking works, we need to make sure we aren't using any of the parent's
# sockets, e.g. db connection
defined?(ActiveRecord::Base) and ActiveRecord::Base.establish_connection
# Redis and Memcached would go here but their connections are established
# on demand, so the master never opens a socket
# $redis = Redis.connect
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment