From 44cbda1c9b737e518901b22e326f446ffe7fc8eb Mon Sep 17 00:00:00 2001 From: "Juan M. Cuello" Date: Tue, 9 Jul 2013 14:52:42 -0300 Subject: [PATCH] Add phased restart to capistrano recipe. Add phased restart task. Little refactor. Do not use fetch when no needed. --- README.md | 1 + lib/puma/capistrano.rb | 29 ++++++++++++++++++----------- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 8af73dd3..d1904e06 100644 --- a/README.md +++ b/README.md @@ -212,6 +212,7 @@ and then $ bundle exec cap puma:start $ bundle exec cap puma:restart $ bundle exec cap puma:stop +$ bundle exec cap puma:phased_restart ``` diff --git a/lib/puma/capistrano.rb b/lib/puma/capistrano.rb index e93f0286..4d38a5ce 100644 --- a/lib/puma/capistrano.rb +++ b/lib/puma/capistrano.rb @@ -8,34 +8,41 @@ Capistrano::Configuration.instance.load do # v2 but is fixed in v3. shared_children.push('tmp/sockets') - _cset(:puma_cmd) { "#{fetch(:bundle_cmd, 'bundle')} exec puma" } + _cset(:puma_cmd) { "#{fetch(:bundle_cmd, 'bundle')} exec puma" } _cset(:pumactl_cmd) { "#{fetch(:bundle_cmd, 'bundle')} exec pumactl" } - _cset(:puma_state) { "#{shared_path}/sockets/puma.state" } + _cset(:puma_env) { fetch(:rack_env, fetch(:rails_env, 'production')) } + _cset(:puma_state) { "#{shared_path}/sockets/puma.state" } _cset(:puma_socket) { "unix://#{shared_path}/sockets/puma.sock" } - _cset(:puma_role) { :app } + _cset(:puma_role) { :app } namespace :puma do desc 'Start puma' - task :start, :roles => lambda { fetch(:puma_role) }, :on_no_matching_servers => :continue do - run "cd #{current_path} && #{fetch(:puma_cmd)} #{start_options}", :pty => false + task :start, :roles => lambda { puma_role }, :on_no_matching_servers => :continue do + run "cd #{current_path} && #{puma_cmd} #{start_options}", :pty => false end desc 'Stop puma' - task :stop, :roles => lambda { fetch(:puma_role) }, :on_no_matching_servers => :continue do - run "cd #{current_path} && #{fetch(:pumactl_cmd)} -S #{state_path} stop" + task :stop, :roles => lambda { puma_role }, :on_no_matching_servers => :continue do + run "cd #{current_path} && #{pumactl_cmd} -S #{state_path} stop" end desc 'Restart puma' - task :restart, :roles => lambda { fetch(:puma_role) }, :on_no_matching_servers => :continue do - run "cd #{current_path} && #{fetch(:pumactl_cmd)} -S #{state_path} restart" + task :restart, :roles => lambda { puma_role }, :on_no_matching_servers => :continue do + run "cd #{current_path} && #{pumactl_cmd} -S #{state_path} restart" end + + desc 'Restart puma (phased restart)' + task :phased_restart, :roles => lambda { puma_role }, :on_no_matching_servers => :continue do + run "cd #{current_path} && #{pumactl_cmd} -S #{state_path} phased-restart" + end + end def start_options if config_file "-q -d -e #{puma_env} -C #{config_file}" else - "-q -d -e #{puma_env} -b '#{fetch(:puma_socket)}' -S #{state_path} --control 'unix://#{shared_path}/sockets/pumactl.sock'" + "-q -d -e #{puma_env} -b '#{puma_socket}' -S #{state_path} --control 'unix://#{shared_path}/sockets/pumactl.sock'" end end @@ -52,7 +59,7 @@ Capistrano::Configuration.instance.load do end def state_path - (config_file ? configuration.options[:state] : nil) || fetch(:puma_state) + (config_file ? configuration.options[:state] : nil) || puma_state end def configuration