Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

zero down time on unicorn restart #41

Merged
merged 2 commits into from
Jan 19, 2013
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions lib/capistrano-unicorn/capistrano_integration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ class CapistranoIntegration
def self.load_into(capistrano_config)
capistrano_config.load do
before(CapistranoIntegration::TASKS) do
_cset(:app_env) { (fetch(:rails_env) rescue 'production') }
_cset(:unicorn_pid) { "#{fetch(:current_path)}/tmp/pids/unicorn.pid" }
_cset(:unicorn_env) { fetch(:app_env) }
_cset(:unicorn_bin) { "unicorn" }
_cset(:unicorn_bundle) { fetch(:bundle_cmd) rescue 'bundle' }
_cset(:app_env) { (fetch(:rails_env) rescue 'production') }
_cset(:unicorn_pid) { "#{fetch(:current_path)}/tmp/pids/unicorn.pid" }
_cset(:unicorn_env) { fetch(:app_env) }
_cset(:unicorn_bin) { "unicorn" }
_cset(:unicorn_bundle) { fetch(:bundle_cmd) rescue 'bundle' }
_cset(:unicorn_restart_sleep_time) { 2 }
end

# Check if a remote process exists using its pid file
Expand Down Expand Up @@ -69,6 +70,7 @@ def unicorn_send_signal(signal, pid=get_unicorn_pid)
#
def kill_unicorn(signal)
script = <<-END
set -x;
if #{unicorn_is_running?}; then
echo "Stopping Unicorn...";
#{unicorn_send_signal(signal)};
Expand All @@ -87,6 +89,7 @@ def start_unicorn
secondary_config_path = "#{current_path}/config/unicorn/#{unicorn_env}.rb"

script = <<-END
set -x;
if [ -e #{primary_config_path} ]; then
UNICORN_CONFIG_PATH=#{primary_config_path};
else
Expand Down Expand Up @@ -136,14 +139,15 @@ def start_unicorn
desc 'Restart Unicorn'
task :restart, :roles => :app, :except => {:no_release => true} do
run <<-END
set -x;
if #{unicorn_is_running?}; then
echo "Restarting Unicorn...";
#{unicorn_send_signal('USR2')};
else
#{start_unicorn}
fi;

sleep 2; # in order to wait for the (old) pidfile to show up
sleep #{unicorn_restart_sleep_time}; # in order to wait for the (old) pidfile to show up

if #{old_unicorn_is_running?}; then
#{unicorn_send_signal('QUIT', get_old_unicorn_pid)};
Expand All @@ -154,6 +158,7 @@ def start_unicorn
desc 'Reload Unicorn'
task :reload, :roles => :app, :except => {:no_release => true} do
run <<-END
set -x;
if #{unicorn_is_running?}; then
echo "Reloading Unicorn...";
#{unicorn_send_signal('HUP')};
Expand All @@ -166,6 +171,7 @@ def start_unicorn
desc 'Add a new worker'
task :add_worker, :roles => :app, :except => {:no_release => true} do
run <<-END
set -x;
if #{unicorn_is_running?}; then
echo "Adding a new Unicorn worker...";
#{unicorn_send_signal('TTIN')};
Expand All @@ -178,6 +184,7 @@ def start_unicorn
desc 'Remove amount of workers'
task :remove_worker, :roles => :app, :except => {:no_release => true} do
run <<-END
set -x;
if #{unicorn_is_running?}; then
echo "Removing a Unicorn worker...";
#{unicorn_send_signal('TTOU')};
Expand Down