Created
March 31, 2014 11:28
-
-
Save tsechingho/9890311 to your computer and use it in GitHub Desktop.
capistrano 3 examples
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace :load do | |
task :defaults do | |
set :unicorn_bin, :unicorn_rails | |
set :unicorn_pid, -> { shared_path.join('tmp', 'pids', 'unicorn.pid') } | |
set :unicorn_config, -> { current_path.join('config', 'unicorn.rb') } | |
set :unicorn_gemfile, -> { current_path.join('Gemfile') } | |
set :unicorn_env, -> { fetch(:rails_env) || fetch(:stage) } | |
set :unicorn_roles, :app | |
set :unicorn_sleep, 2 | |
end | |
end | |
namespace :unicorn do | |
desc 'Start unicorn' | |
task :start do | |
on roles fetch(:unicorn_roles) do | |
within current_path do | |
with bundle_gemfile: fetch(:unicorn_gemfile) do | |
if test "[ -e #{fetch(:unicorn_pid)} ] && kill -0 #{pid}" | |
info 'Unicorn is running...' | |
else | |
execute fetch(:unicorn_bin), '-D', | |
'-c', fetch(:unicorn_config), | |
'-E', fetch(:unicorn_env) | |
end | |
end | |
end | |
end | |
end | |
desc 'Stop unicorn (QUIT)' | |
task :stop do | |
on roles fetch(:unicorn_roles) do | |
if test "[ -e #{fetch(:unicorn_pid)} ]" | |
if test "kill -0 #{pid}" | |
info 'Stopping unicorn...' | |
execute :kill, '-s QUIT', pid | |
else | |
info "Cleaning up dead unicorn pid..." | |
execute :rm, fetch(:unicorn_pid) | |
end | |
else | |
info 'Unicorn is not running...' | |
end | |
end | |
end | |
desc 'Restart unicorn (USR2 + QUIT); Use this when preload_app: true' | |
task :restart do | |
invoke 'unicorn:start' | |
on roles fetch(:unicorn_roles) do | |
with bundle_gemfile: fetch(:unicorn_gemfile) do | |
info 'Restarting unicorn...' | |
execute :kill, '-s USR2', pid | |
execute :sleep, fetch(:unicorn_sleep) | |
if test "[ -e #{fetch(:unicorn_pid)}.oldbin ]" | |
execute :kill, '-s QUIT', pid_oldbin | |
end | |
end | |
end | |
end | |
desc 'Reload unicorn (HUP); Use this when preload_app: false' | |
task :reload do | |
invoke 'unicorn:start' | |
on roles fetch(:unicorn_roles) do | |
info 'Reloading unicorn...' | |
execute :kill, '-s HUP', pid | |
end | |
end | |
desc 'Add an unicorn worker (TTIN)' | |
task :add_worker do | |
on roles fetch(:unicorn_roles) do | |
info 'Adding an unicorn worker...' | |
execute :kill, '-s TTIN', pid | |
end | |
end | |
desc 'Remove an unicorn worker (TTOU)' | |
task :remove_worker do | |
on roles fetch(:unicorn_roles) do | |
info 'Removing an unicorn worker...' | |
execute :kill, '-s TTOU', pid | |
end | |
end | |
def pid | |
"`cat #{fetch(:unicorn_pid)}`" | |
end | |
def pid_oldbin | |
"`cat #{fetch(:unicorn_pid)}.oldbin`" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment