Skip to content

Instantly share code, notes, and snippets.

@pmeinhardt
Last active December 10, 2015 00:59
Show Gist options
  • Save pmeinhardt/4355259 to your computer and use it in GitHub Desktop.
Save pmeinhardt/4355259 to your computer and use it in GitHub Desktop.
capistrano unicorn integration
# Unicorn itself is configured in `config/unicorn.rb`. These are the tasks,
# that allow you to control the server through Capistrano's `cap` command.
# To understand how Unicorn reacts to each signal, you should really read
# http://unicorn.bogomips.org/SIGNALS.html
namespace :unicorn do
task :start do
env = fetch(:rails_env, "production")
run <<-EOC
cd #{current_path} && \
bundle exec unicorn -c config/unicorn.rb -E #{env} -D
EOC
end
task :stop do
signal "QUIT"
end
task :restart do
signal "USR2"
end
task :reload do
signal "HUP"
end
namespace :workers do
task :count do
puts capture("ps -o ppid x | grep `cat #{pid_path}` | wc -l")
end
task :inc do
signal "TTIN"
end
task :dec do
signal "TTOU"
end
end
def pid_path
"#{shared_path}/pids/unicorn.pid"
end
def signal(sig)
run "test -e #{pid_path} && kill -s #{sig} `cat #{pid_path}`"
end
end
# Overwrite blank tasks to install our own environment specific behaviour.
# Here we have the `start`, `stop` and `restart` tasks, delegate to the
# unicorn specific implementations.
namespace :deploy do
desc "Start unicorn"
task :start, roles: :app do
unicorn.start
end
desc "Stop unicorn"
task :stop, roles: :app do
unicorn.stop
end
desc "Restart unicorn, zero-downtime (server must be running)"
task :restart, roles: :app do
unicorn.restart
end
desc "Reload unicorn config files and restart workers"
task :reload, roles: :app do
unicorn.reload
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment