diff --git a/config/deploy.rb b/config/deploy.rb index 63a486bb2..fbaf4ad94 100644 --- a/config/deploy.rb +++ b/config/deploy.rb @@ -45,7 +45,8 @@ end namespace :deploy do desc "Restarting mod_rails with restart.txt" task :restart, :roles => :app, :except => { :no_release => true } do - run "cd #{deploy_to}/current ; ([ -f tmp/pids/unicorn.pid ] && kill -USR2 `cat tmp/pids/unicorn.pid`); true" + ## DISABLED: run "cd #{deploy_to}/current ; ([ -f tmp/pids/unicorn.pid ] && kill -USR2 `cat tmp/pids/unicorn.pid`); true" + run ["#{current_path}/script/unicorn reload"].join("; ") restart_dj end diff --git a/config/unicorn.rb b/config/unicorn.rb index b6837e879..019780134 100644 --- a/config/unicorn.rb +++ b/config/unicorn.rb @@ -53,22 +53,35 @@ before_fork do |server, worker| end end +before_fork do |server, worker| + ## + # When sent a USR2, Unicorn will suffix its pidfile with .oldbin and + # immediately start loading up a new version of itself (loaded with a new + # version of our app). When this new Unicorn is completely loaded + # it will begin spawning workers. The first worker spawned will check to + # see if an .oldbin pidfile exists. If so, this means we've just booted up + # a new Unicorn and need to tell the old one that it can now die. To do so + # we send it a QUIT. + # + # Using this method we get 0 downtime deploys. + old_pid = File.expand_path(File.join(File.dirname(__FILE__), "..")) + '/tmp/pids/unicorn.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 -#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 -# defined?(ActiveRecord::Base) and -# ActiveRecord::Base.connection.disconnect! -#end + # This option works in together with preload_app true setting + # What is does is prevent the master process from holding + # the database connection + defined?(ActiveRecord::Base) and ActiveRecord::Base.connection.disconnect! +end after_fork do |server, worker| - # per-process listener ports for debugging/admin/migrations - # addr = "127.0.0.1:#{9293 + worker.nr}" - # server.listen(addr, :tries => -1, :delay => 5, :tcp_nopush => true) - - # the following is *required* for Rails + "preload_app true", - defined?(ActiveRecord::Base) and - ActiveRecord::Base.establish_connection + # Here we are establishing the connection after forking worker processes + defined?(ActiveRecord::Base) and ActiveRecord::Base.establish_connection # if preload_app is true, then you may also want to check and # restart any other shared sockets/descriptors such as Memcached, diff --git a/script/unicorn b/script/unicorn new file mode 100644 index 000000000..8465bc7e8 --- /dev/null +++ b/script/unicorn @@ -0,0 +1,61 @@ +#!/bin/sh + +current_path="/var/www/rosa_build" + + +getpid() { + pid=`ps ax |grep "unicorn_rails master -c /var/www/rosa_build" \ + |grep -v grep \ + |awk '{print $1}'`; +} + + +start() { + export RUBY_HEAP_MIN_SLOTS=1400000 + export RUBY_HEAP_SLOTS_INCREMENT=500000 + export RUBY_HEAP_SLOTS_GROWTH_FACTOR=1 + export RUBY_GC_MALLOC_LIMIT=30000000 + export RUBY_HEAP_FREE_MIN=12500 + + cd $current_path && \ + bundle exec unicorn_rails -c $current_path/config/unicorn.rb -E production -D +} + + +stop() { + while : ; do + getpid; + if [ x"$pid" != x"" ] ; then + kill $pid; + sleep 1; + else + break; + fi + done +} + + +reload() { + getpid; + if [ x"$pid" != x"" ] ; then + kill -USR2 $pid; + else + start; + fi +} + + +case "$1" in + start) + start + ;; + stop) + stop; + ;; + reload) + reload + ;; + *) + echo $"Usage: $0 {start|stop|reload}" + exit 2 +esac