namespace :deploy do after :finished, 'puma:smart_restart' end namespace :load do task :defaults do set :puma_role, :app set :puma_env, -> { fetch(:rack_env, fetch(:rails_env, 'production')) } # Configure "min" to be the minimum number of threads to use to answer # requests and "max" the maximum. set :puma_threads, [0, 16] set :puma_workers, 0 set :puma_state, -> { File.join(shared_path, 'tmp', 'pids', 'puma.state') } set :puma_pid, -> { File.join(shared_path, 'tmp', 'pids', 'puma.pid') } set :puma_bind, -> { File.join('unix://', shared_path, 'tmp', 'sockets', 'puma.sock') } set :puma_conf, -> { File.join(shared_path, 'puma.rb') } set :puma_access_log, -> { File.join(shared_path, 'log', 'puma_error.log') } set :puma_error_log, -> { File.join(shared_path, 'log', 'puma_access.log') } # Rbenv and RVM integration set :rbenv_map_bins, fetch(:rbenv_map_bins).to_a.concat(%w{ puma pumactl }) set :rvm_map_bins, fetch(:rvm_map_bins).to_a.concat(%w{ puma pumactl }) end end namespace :puma do desc 'Setup Puma config file' task :config do on roles(fetch(:puma_role)) do template_puma 'puma.rb.erb', fetch(:puma_conf) end end desc 'Start puma' task :start do on roles (fetch(:puma_role)) do within current_path do execute :bundle, 'exec', :puma, "-C #{fetch(:puma_conf)}" end end end %w[halt stop phased-restart status].each do |command| desc "#{command} puma" task command do on roles (fetch(:puma_role)) do within current_path do execute :bundle, 'exec', :pumactl, "-S #{fetch(:puma_state)} #{command}" end end end end desc 'Restart puma' task :restart do on roles (fetch(:puma_role)) do within current_path do if test "[ -f #{fetch(:puma_state)} ]" execute :bundle, 'exec', :pumactl, "-S #{fetch(:puma_state)} restart" else # Puma is not running or state file is not present : Run it # TODO check for pid execute :bundle, 'exec', :puma, "-C #{fetch(:puma_conf)}" end end end end task :check do on roles (fetch(:puma_role)) do #Create puma.rb for new deployments unless test "[ -f #{fetch(:puma_conf)} ]" warn 'puma.rb NOT FOUND!' #TODO DRY template_puma 'puma.rb.erb', fetch(:puma_conf) info 'puma.rb generated' end end end after 'deploy:check', 'puma:check' task :smart_restart do if puma_workers.to_i > 1 invoke 'puma:phased-restart' else invoke 'puma:restart' end end def processors_count #TODO , will be used to warn if # of workers is > of available cores capture 'grep -c processor /proc/cpuinfo' end def puma_workers fetch(:puma_workers) || 0 end def template_puma(from, to) erb = File.read(File.expand_path("../../templates/#{from}", __FILE__)) upload! StringIO.new(ERB.new(erb).result(binding)), to end end