Skip to content

Instantly share code, notes, and snippets.

@mdarby
Forked from acrookston/config_puma.rb
Created August 15, 2016 18:01
Show Gist options
  • Save mdarby/8133f7886492c155b79c084a5ff3dd84 to your computer and use it in GitHub Desktop.
Save mdarby/8133f7886492c155b79c084a5ff3dd84 to your computer and use it in GitHub Desktop.
puma config for rvm, capistrano 3 and upstart. Tested on Ubuntu 12.04 LTS and 14.04 LTS
#!/usr/bin/env puma
app_path = '/var/www/YOUR_APP_NAME/current'
directory app_path
environment 'production'
daemonize true
pidfile "#{app_path}/tmp/pids/puma.pid"
state_path "#{app_path}/tmp/pids/puma.state"
stdout_redirect "#{app_path}/log/puma_error.log", "#{app_path}/log/puma_access.log", true
threads 0,16
bind "unix:#{app_path}/tmp/sockets/puma.sock"
workers 0
preload_app!
on_restart do
puts 'Refreshing Gemfile'
ENV["BUNDLE_GEMFILE"] = "#{app_path}/Gemfile"
end
SSHKit.config.command_map[:run] = "cd #{fetch(:deploy_to)}/current && /usr/local/rvm/bin/rvm default do"
set :puma_conf, "#{shared_path}/config/puma.rb"
set :puma_state, "#{shared_path}/tmp/pids/puma.state"
set :puma_pid, "#{shared_path}/tmp/pids/puma.pid"
set :puma_socket, "#{shared_path}/tmp/sockets/puma.sock"
set :linked_files, %w{config/puma.rb}
set :linked_dirs, %w{tmp/pids tmp/sockets}
# /etc/init/puma-manager.conf - manage a set of Pumas
# This example config should work with Ubuntu 12.04+. It
# allows you to manage multiple Puma instances with
# Upstart, Ubuntu's native service management tool.
#
# See puma.conf for how to manage a single Puma instance.
#
# Use "stop puma-manager" to stop all Puma instances.
# Use "start puma-manager" to start all instances.
# Use "restart puma-manager" to restart all instances.
# Crazy, right?
#
description "Manages the set of puma processes"
# This starts upon bootup and stops on shutdown
start on runlevel [2345]
stop on runlevel [06]
# Set this to the number of Puma processes you want
# to run on this machine
env PUMA_CONF="/etc/puma.conf"
pre-start script
for i in `cat $PUMA_CONF`; do
app=`echo $i | cut -d , -f 1`
logger -t "puma-manager" "Starting $app"
start puma app=$app
done
end script
# /etc/init/puma.conf - Puma config
# This example config should work with Ubuntu 12.04+. It
# allows you to manage multiple Puma instances with
# Upstart, Ubuntu's native service management tool.
#
# See workers.conf for how to manage all Puma instances at once.
#
# Save this config as /etc/init/puma.conf then manage puma with:
# sudo start puma app=PATH_TO_APP
# sudo stop puma app=PATH_TO_APP
# sudo status puma app=PATH_TO_APP
#
# or use the service command:
# sudo service puma {start,stop,restart,status}
#
description "Puma Background Worker"
# no "start on", we don't want to automatically start
stop on (stopping puma-manager or runlevel [06])
# change apps to match your deployment user if you want to use this as a less privileged user (recommended!)
setuid deploy
setgid deploy
respawn
respawn limit 3 30
instance ${app}
script
# this script runs in /bin/sh by default
# respawn as bash so we can source in rbenv/rvm
# quoted heredoc to tell /bin/sh not to interpret
# variables
exec /bin/bash <<'EOT'
# set HOME to the setuid user's home, there doesn't seem to be a better, portable way
export HOME="$(eval echo ~$(id -un))"
if [ -d "$HOME/.rbenv/bin" ]; then
export PATH="$HOME/.rbenv/bin:$HOME/.rbenv/shims:$PATH"
elif [ -f /etc/profile.d/rvm.sh ]; then
source /etc/profile.d/rvm.sh
elif [ -f /usr/local/rvm/scripts/rvm ]; then
source /etc/profile.d/rvm.sh
elif [ -f "$HOME/.rvm/scripts/rvm" ]; then
source "$HOME/.rvm/scripts/rvm"
elif [ -f /usr/local/share/chruby/chruby.sh ]; then
source /usr/local/share/chruby/chruby.sh
if [ -f /usr/local/share/chruby/auto.sh ]; then
source /usr/local/share/chruby/auto.sh
fi
# if you aren't using auto, set your version here
# chruby 2.0.0
fi
# cd helps setting ruby version and local environment variables
cd $app
logger -t puma "Starting server: $app"
exec bundle exec $app/bin/puma --config $app/config/puma.rb
EOT
end script
/var/www/YOUR_APP_NAME/current
namespace :puma do
desc 'Start the puma server'
task :start do
on roles(:web) do
execute :run, "#{fetch(:deploy_to)}/current/bin/puma --config #{fetch(:puma_conf)}"
end
end
desc 'Restart the puma server'
task :restart do
on roles(:web) do
if capture("[ -S #{fetch(:puma_socket)} ] && echo '1' || echo '0'") == '1' &&
capture("[ -f #{fetch(:puma_pid)} ] && echo '1' || echo '0'") == '1'
debug "Hot-restarting puma..."
execute "kill -s SIGUSR2 `cat #{fetch(:puma_pid)}`"
debug "Doublechecking the process restart..."
sleep 5
if capture("[ -S #{fetch(:puma_socket)} ] && echo '1' || echo '0'") == '1'
debug "Restart success!"
else
debug "Failed! Trying to cold reboot"
execute :run, "#{fetch(:deploy_to)}/current/bin/puma --config #{fetch(:puma_conf)}"
end
else
debug "Server not running. Starting..."
execute :run, "#{fetch(:deploy_to)}/current/bin/puma --config #{fetch(:puma_conf)}"
end
end
end
desc 'Stop the puma server'
task :stop do
on roles(:web) do
pid = if capture("[ -f #{fetch(:puma_pid)} ] && echo '1' || echo '0'") == '1'
capture "cat #{fetch(:puma_pid)}"
elsif capture("[ -f #{fetch(:puma_state)} ] && echo '1' || echo '0'") == '1'
capture "cat #{fetch(:puma_state)} | sed -n 2p | awk -F ' ' '{print $2}'"
end
execute "rm -f #{fetch(:puma_pid)}"
execute "rm -f #{fetch(:puma_socket)}"
execute "rm -f #{fetch(:puma_state)}"
execute "kill -s SIGTERM #{pid}"
end
end
end
before 'puma:start', 'rvm:hook'
before 'deploy:restart', 'puma:restart'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment