Skip to content

Instantly share code, notes, and snippets.

@banjarey
Created November 6, 2012 08:14
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 banjarey/4023431 to your computer and use it in GitHub Desktop.
Save banjarey/4023431 to your computer and use it in GitHub Desktop.
Sample unicorn config excluding staging
# Minimal sample configuration file for Unicorn (not Rack) when used
# with daemonization (unicorn -D) started in your working directory.
#
# See http://unicorn.bogomips.org/Unicorn/Configurator.html for complete
# documentation.
# See also http://unicorn.bogomips.org/examples/unicorn.conf.rb for
# a more verbose configuration using more features.
env = ENV["RAILS_ENV"] || "development"
if env == "production"
worker_processes 8
listen "/tmp/unicorn.banjarey.socket", :backlog => 64
preload_app true
user "deploy", "deploy"
working_directory "/var/www/banjarey.com/current"
shared_path = "/var/www/banjarey.com/shared"
stderr_path "#{shared_path}/log/unicorn.stderr.log"
stdout_path "#{shared_path}/log/unicorn.stdout.log"
pid "#{shared_path}/tmp/pids/unicorn.banjarey.pid"
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 = "#{shared_path}/tmp/pids/unicorn.banjarey.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
elsif env == "development"
listen 2007 # by default Unicorn listens on port 8080
worker_processes 2 # this should be >= nr_cpus
pid "/var/www/banjarey/tmp/pids/unicorn.pid"
stderr_path "/var/www/banjarey/log/unicorn.log"
stdout_path "/var/www/banjarey/log/unicorn.log"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment