Skip to content

Instantly share code, notes, and snippets.

@takkkun
Last active December 25, 2015 10:59
Show Gist options
  • Save takkkun/6965515 to your computer and use it in GitHub Desktop.
Save takkkun/6965515 to your computer and use it in GitHub Desktop.
lib/capistrano/tasksにでも置いて使う
set :unicorn_config_path, -> { current_path.join('config', 'unicorn.rb') }
set :unicorn_pid_path, -> { current_path.join('tmp', 'pids', 'unicorn.pid') }
set :unicorn_env, -> { fetch(:rails_env) || fetch(:stage) }
SSHKit.config.command_map[:unicorn_stop] = 'kill -QUIT'
SSHKit.config.command_map[:unicorn_restart] = 'kill -USR2'
namespace :deploy do
def unicorn_running?
test "[ -f #{fetch(:unicorn_pid_path)} ]"
end
def unicorn_pid
capture "cat #{fetch(:unicorn_pid_path)}"
end
desc 'Start application'
task :start do
SSHKit.config.command_map.prefix[:unicorn_rails].tap do |prefix|
rbenv_prefix = fetch(:rbenv_prefix, proc { "#{fetch(:rbenv_path)}/bin/rbenv exec" })
prefix.unshift(rbenv_prefix)
prefix.push('bundle exec')
end
on roles(:app), in: :parallel do
within current_path do
if unicorn_running?
info 'unicorn is already running'
else
execute :unicorn_rails, '-c', fetch(:unicorn_config_path), '-E', fetch(:unicorn_env), '-D'
end
end
end
end
desc 'Stop application'
task :stop do
on roles(:app), in: :parallel do
if unicorn_running?
execute :unicorn_stop, unicorn_pid
else
info 'unicorn is not running'
end
end
end
desc 'Restart application'
task :restart do
started = true
on roles(:app), in: :parallel do
if unicorn_running?
execute :unicorn_restart, unicorn_pid
else
info 'unicorn is not running, so start unicorn'
started = false
end
end
invoke 'deploy:start' unless started
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment