Skip to content

Instantly share code, notes, and snippets.

@maruware
Created March 17, 2016 02:47
Show Gist options
  • Save maruware/bf2cac099636fe1554c9 to your computer and use it in GitHub Desktop.
Save maruware/bf2cac099636fe1554c9 to your computer and use it in GitHub Desktop.
config/unicorn.rb
rails_root = File.expand_path('../../', __FILE__)
listen "#{rails_root}/tmp/sockets/unicorn.sock"
pid "#{rails_root}/tmp/pids/unicorn.pid"
stdout_path "#{rails_root}/log/#{:stage}.log"
stderr_path "#{rails_root}/log/#{:stage}.log"
timeout 60
worker_processes Integer(ENV["WEB_CONCURRENCY"] || 2)
#no downtime
preload_app true
GC.respond_to?(:copy_on_write_friendly=) and GC.copy_on_write_friendly = true
before_fork do |server, worker|
defined?(ActiveRecord::Base) and ActiveRecord::Base.connection.disconnect!
old_pid = "#{server.config[:pid]}.oldbin"
if old_pid != server.pid
begin
sig = (worker.nr + 1) >= server.worker_processes ? :QUIT : :TTOU
Process.kill(sig, File.read(old_pid).to_i)
rescue Errno::ENOENT, Errno::ESRCH
end
end
sleep 1
end
after_fork do |server, worker|
defined?(ActiveRecord::Base) and ActiveRecord::Base.establish_connection
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment