Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Upstart + Bluepill + Unicorn with hot restart
# /etc/init/project-web-reload.conf
pre-start script
initctl restart project-web
sleep 15
end script
exec /usr/local/rvm/bin/default_bluepill restart
# /etc/init/project-web.conf
respawn
expect daemon
pre-start script
bash <<'EOS'
source /usr/local/rvm/scripts/rvm
ruby -e 'begin; Gem::Specification.find_by_name("bluepill"); rescue Gem::LoadError; exit 1; end; exit 0'
retval=$?
if [ "$retval" -ne 0 ]; then
gem install bluepill --no-rdoc --no-ri
rvm wrapper default default bluepill
fi
EOS
mkdir -p /var/www/project
mkdir -p /var/log/project
mkdir -p /var/run/project
chown -R www /var/www/project
chown -R www /var/log/project
chown -R www /var/run/project
end script
exec /usr/local/rvm/bin/default_bluepill load /var/www/project/config/project-web.pill
# /var/www/project/config/project-web.pill
Bluepill.application('project-web', log_file: '/var/log/project/bluepill.log') do |app|
app.working_dir = '/var/www/project'
app.uid = 'www'
app.gid = 'rvm'
app.process('unicorn') do |process|
process.start_command = 'bundle exec unicorn -E production -c /var/www/project/config/unicorn.rb -D'
process.stop_command = 'start-stop-daemon --signal QUIT --stop --pidfile /var/run/project/web.pid'
process.restart_command = 'start-stop-daemon --signal USR2 --stop --pidfile /var/run/project/web.pid'
process.pid_file = '/var/run/project/web.pid'
process.start_grace_time = 30.seconds
process.stop_grace_time = 30.seconds
end
end
# /var/www/project/config/unicorn.rb
worker_processes 4
working_directory '/var/www/project'
listen '/tmp/project.web.sock', backlog: 2048
timeout 60
pid '/var/run/project/web.pid'
stderr_path '/var/log/project/unicorn.stderr.log'
stdout_path '/var/log/project/unicorn.stdout.log'
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
defined?(Rails) and Rails.cache.respond_to?(:reconnect) and
Rails.cache.reconnect
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment