Skip to content

Instantly share code, notes, and snippets.

@le0pard
Created November 9, 2012 21:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save le0pard/4048375 to your computer and use it in GitHub Desktop.
Save le0pard/4048375 to your computer and use it in GitHub Desktop.
Unicorn config for chef-solo
app_root = '<%= node['app']['web_dir'] %>'
rails_root = "#{app_root}/current"
rails_env = '<%= node['app']['env'] %>'
pid_file = "#{app_root}/shared/tmp/pids/unicorn.pid"
socket_file= "#{app_root}/shared/tmp/sockets/unicorn.sock"
log_file = "#{rails_root}/log/unicorn.log"
username = '<%= node['user']['name'] %>'
group = '<%= node['user']['name'] %>'
old_pid = "#{pid_file}.oldbin"
timeout <%= node['unicorn']['timeout'] %>
worker_processes <%= node['unicorn']['worker_processes'] %>
# Listen on a Unix data socket
listen socket_file, :backlog => 1024
pid pid_file
stdout_path log_file
preload_app true
##
# REE
GC.copy_on_write_friendly = true if GC.respond_to?(:copy_on_write_friendly=)
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
defined?(ActiveRecord::Base) and
ActiveRecord::Base.connection.disconnect!
##
# 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.
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|
if defined?(ActiveRecord::Base)
ActiveRecord::Base.establish_connection
end
worker.user(username, group) if Process.euid == 0 && rails_env == '<%= node['app']['env'] %>'
child_pid = server.config[:pid].sub(".pid", ".#{worker.nr}.pid")
system("echo #{Process.pid} > #{child_pid}")
end
before_exec do |server|
ENV["BUNDLE_GEMFILE"] = "#{rails_root}/Gemfile"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment