Skip to content

Instantly share code, notes, and snippets.

@merqlove
Last active December 28, 2015 06:39
Show Gist options
  • Save merqlove/7458755 to your computer and use it in GitHub Desktop.
Save merqlove/7458755 to your computer and use it in GitHub Desktop.
Another one Puma shell script. Here i can set path to Puma config. Very useful for multiple environments.
# Capistrano 3 tasks for Puma
namespace :puma do
%w[start stop restart status].each do |command|
desc "#{command} puma"
task command do
on roles (:app) do
within current_path do
execute "bin/puma.sh", "#{command} -c config/puma/#{fetch(:stage)}.rb"
end
end
end
end
end
#!/usr/bin/env bash
# Simple move this file into your Rails `script` folder. Also make sure you `chmod +x puma.sh`.
# Please modify the CONSTANT variables to fit your configurations.
# The script will start with config set by $PUMA_CONFIG_FILE by default
PUMA_CONFIG_FILE=config/puma.rb
PUMA_PID_FILE=tmp/pids/puma.pid
PUMA_SOCKET=tmp/sockets/puma.sock
# PUMA_ENVIRONMENT=development
# check if puma process is running
puma_is_running() {
if [ -S $PUMA_SOCKET ] ; then
if [ -e $PUMA_PID_FILE ] ; then
if kill -0 `cat $PUMA_PID_FILE` ; then
return 0
else
echo "No puma process found"
fi
else
echo "No puma pid file found"
fi
else
echo "No puma socket found"
fi
return 1
}
case "$2" in
# -e|--environment) PUMA_ENVIRONMENT="$3" ;;
-c|--config) PUMA_CONFIG_FILE="$3" ;;
--) ;;
*) ;;
esac
case "$1" in
start)
echo "Starting puma..."
rm -f $PUMA_SOCKET
if [ -e $PUMA_CONFIG_FILE ] ; then
bundle exec puma --config $PUMA_CONFIG_FILE #--environment $PUMA_ENVIRONMENT
else
bundle exec puma --daemon --bind unix://$PUMA_SOCKET --pidfile $PUMA_PID_FILE #--environment $PUMA_ENVIRONMENT
fi
echo "done"
;;
stop)
echo "Stopping puma..."
kill -s SIGTERM `cat $PUMA_PID_FILE`
rm -f $PUMA_PID_FILE
rm -f $PUMA_SOCKET
echo "done"
;;
restart)
if puma_is_running ; then
echo "Hot-restarting puma..."
kill -s SIGUSR2 `cat $PUMA_PID_FILE`
echo "Doublechecking the process restart..."
sleep 5
if puma_is_running ; then
echo "done"
exit 0
else
echo "Puma restart failed :/"
fi
fi
echo "Trying cold reboot"
bin/puma.sh start --config $PUMA_CONFIG_FILE
;;
status)
if puma_is_running ; then
echo "puma is running"
exit 0
else
echo "puma is not running"
exit 1 # return error
fi
;;
*)
echo "Usage: bin/puma.sh {start|stop|restart|status}" >&2
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment