Skip to content

Instantly share code, notes, and snippets.

@jellybob
Created June 18, 2013 12:40
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 jellybob/3789c3f3a00989b63d74 to your computer and use it in GitHub Desktop.
Save jellybob/3789c3f3a00989b63d74 to your computer and use it in GitHub Desktop.
listen "/srv/hubbub/shared/customer_workers.sock", :tcp_nodelay => true
working_directory '/srv/hubbub/current'
timeout 60
preload_app true
worker_processes 4
before_fork do |server, worker|
defined?(ActiveRecord::Base) and ActiveRecord::Base.connection.disconnect!
# Gracefully handle restarts. If the PID file has changed, then a new version
# has been deployed with a new master. Start gracefully shutting down the old
# Unicorn workers as they finish requests, and then shut down the master once
# there are no workers left.
old_pid = '/srv/hubbub/shared/pids/customer_workers.pid.oldbin'
if server.pid != old_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
# someone else did our job for us
end
end
sleep 0.5
end
after_fork do |server, worker|
defined?(ActiveRecord::Base) and ActiveRecord::Base.establish_connection
end
pid '/srv/hubbub/shared/pids/customer_workers.pid'
# http://www.rubyenterpriseedition.com/faq.html#adapt_apps_for_cow
if GC.respond_to?(:copy_on_write_friendly=)
GC.copy_on_write_friendly = true
end
# https://newrelic.com/docs/ruby/ruby-gc-instrumentation
if GC.respond_to?(:enable_stats)
GC.enable_stats
end
if defined?(GC::Profiler) and GC::Profiler.respond_to?(:enable)
GC::Profiler.enable
end
description "Customer Site Unicorns"
start on filesystem or runlevel [2345]
stop on runlevel [!2345]
respawn
respawn limit 10 5
setuid hubbub
setgid hubbub
chdir /srv/hubbub/current
script
while true; do
if [ ! -f /srv/hubbub/shared/pids/customer_workers.pid ]; then
# Run the unicorn master process (this won't return until it exits).
bundle1.8 exec /srv/hubbub/shared/vendor_bundle/ruby/1.8/bin/unicorn -c /etc/unicorn/customer_workers.conf.rb
else
# Someone restarted the master; wait for the new master to exit.
PID=`cat /srv/hubbub/shared/pids/customer_workers.pid`
while [ -d /proc/$PID ]; do
sleep 40
done
# If we get here, the master has exited, either because someone restarted
# it again (in which case there's already a new master running), or
# it died for real (in which case we'll need to start a new process).
# The sleep above is a tradeoff between polling load and mimizing the
# restart delay when the master dies for real (which should hopefully be
# rare).
fi
done
end script
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment