Skip to content

Instantly share code, notes, and snippets.

@colinrymer
Last active December 20, 2015 05:59
Show Gist options
  • Save colinrymer/6082650 to your computer and use it in GitHub Desktop.
Save colinrymer/6082650 to your computer and use it in GitHub Desktop.
environment = ENV['RACK_ENV'] || ENV['RAILS_ENV'] || 'production'
app_path = "/var/www/webapp"
bundle_path = "#{working_directory}/../shared/bundle"
#listen "/tmp/.sock", :backlog => 64
listen 8080, :tcp_nopush => true
timeout 30
worker_processes 12
preload_app true
# Hard-set the CWD & pidfile to ensure app-reloading consistency
working_directory "#{app_path}/current"
pid "/var/www/webapp/shared/pids/unicorn.pid"
# Helps ensure the correct unicorn binary is used when upgrading with USR2
# See http://unicorn.bogomips.org/Sandbox.html
Unicorn::HttpServer::START_CTX[0] = "#{bundle_path}/bin/unicorn"
stderr_path "/var/www/webapp/shared/log/unicorn.stderr.log"
stdout_path "/var/www/webapp/shared/log/unicorn.stdout.log"
if GC.respond_to?(:copy_on_write_friendly=)
GC.copy_on_write_friendly = true
end
# Rails breaks unicorn's logger formatting, reset it
# http://rubyforge.org/pipermail/mongrel-unicorn/2010-October/000732.html
Unicorn::Configurator::DEFAULTS[:logger].formatter = Logger::Formatter.new
# Forcibly clean environment variables between bundlings
# http://www.mail-archive.com/mongrel-unicorn@rubyforge.org/msg00276.html
before_exec do |_|
ENV["BUNDLE_GEMFILE"] = "#{app_path}/current/Gemfile"
end
before_fork do |server, worker|
ActiveRecord::Base.connection.disconnect! if defined?(ActiveRecord::Base)
# Incremental kill-off
old_pid = "#{server.config[:pid]}.oldbin"
if old_pid != server.pid
begin
sig = (worker.nr + 1) >= server.worker_processes ? :QUIT : :TTOU
puts "Sending #{sig} signal to old unicorn master..."
Process.kill(sig, File.read(old_pid).to_i)
rescue Errno::ENOENT, Errno::ESRCH
end
end
# Throttle the master from forking too quickly (for incremental kill-off only)
sleep 1
end
after_fork do |server, worker|
ActiveRecord::Base.establish_connection if defined?(ActiveRecord::Base)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment