Skip to content

Commit

Permalink
Merge pull request puma#977 from snow/master
Browse files Browse the repository at this point in the history
NEW: add phased-restart support to init.d script
  • Loading branch information
evanphx committed May 10, 2016
2 parents 3a0afd9 + b7161cd commit 7dfd7fa
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 8 deletions.
73 changes: 65 additions & 8 deletions tools/jungle/init.d/puma
Original file line number Diff line number Diff line change
Expand Up @@ -137,15 +137,49 @@ do_restart_one() {
PIDFILE=$1/tmp/puma/pid
i=`grep $1 $CONFIG`
dir=`echo $i | cut -d , -f 1`

if [ -e $PIDFILE ]; then
log_daemon_msg "--> About to restart puma $1"
if [ "$USE_LOCAL_BUNDLE" -eq 1 ]; then
cd $1 && bundle exec pumactl --state $dir/tmp/puma/state restart
else
pumactl --state $dir/tmp/puma/state restart
kill -s USR2 `cat $PIDFILE`
# TODO Check if process exist
else
log_daemon_msg "--> Your puma was never playing... Let's get it out there first"
user=`echo $i | cut -d , -f 2`
config_file=`echo $i | cut -d , -f 3`
if [ "$config_file" = "" ]; then
config_file="$dir/config/puma.rb"
fi
# kill -s USR2 `cat $PIDFILE`
log_file=`echo $i | cut -d , -f 4`
if [ "$log_file" = "" ]; then
log_file="$dir/log/puma.log"
fi
environment=`echo $i | cut -d , -f 5`
do_start_one $dir $user $config_file $log_file $environment
fi
return 0
}

#
# Function that phased restarts the jungle
#
do_phased_restart() {
for i in $JUNGLE; do
dir=`echo $i | cut -d , -f 1`
do_phased_restart_one $dir
done
}

#
# Function that sends a SIGUSR1 to the daemon/service
#
do_phased_restart_one() {
PIDFILE=$1/tmp/puma/pid
i=`grep $1 $CONFIG`
dir=`echo $i | cut -d , -f 1`

if [ -e $PIDFILE ]; then
log_daemon_msg "--> About to restart puma $1"
kill -s USR1 `cat $PIDFILE`
# TODO Check if process exist
else
log_daemon_msg "--> Your puma was never playing... Let's get it out there first"
Expand Down Expand Up @@ -256,16 +290,25 @@ do_remove() {

config_bundler() {
HOME="$(eval echo ~$(id -un))"
if [ -d "/usr/local/rbenv/bin" ]; then

if [ -d "$1/.rbenv/bin" ]; then
PATH="$1/.rbenv/bin:$1/.rbenv/shims:$1"
eval "$(rbenv init -)"
USE_LOCAL_BUNDLE=1
return 0

elif [ -d "/usr/local/rbenv/bin" ]; then
PATH="/usr/local/rbenv/bin:/usr/local/rbenv/shims:$PATH"
eval "$(rbenv init -)"
USE_LOCAL_BUNDLE=1
return 0

elif [ -d "$HOME/.rbenv/bin" ]; then
PATH="$HOME/.rbenv/bin:$HOME/.rbenv/shims:$PATH"
eval "$(rbenv init -)"
USE_LOCAL_BUNDLE=1
return 0

# TODO: test rvm
# elif [ -f /etc/profile.d/rvm.sh ]; then
# source /etc/profile.d/rvm.sh
Expand Down Expand Up @@ -355,6 +398,20 @@ case "$1" in
2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
esac
;;
phased-restart)
log_daemon_msg "Restarting (phased) $DESC" "$NAME"
if [ "$#" -eq 1 ]; then
do_phased_restart
else
i=`grep $2 $CONFIG`
dir=`echo $i | cut -d , -f 1`
do_phased_restart_one $dir
fi
case "$?" in
0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
esac
;;
add)
if [ "$#" -lt 3 ]; then
echo "Please, specify the app's directory and the user that will run it at least."
Expand Down Expand Up @@ -383,7 +440,7 @@ case "$1" in
;;
*)
echo "Usage:" >&2
echo " Run the jungle: $SCRIPTNAME {start|stop|status|restart}" >&2
echo " Run the jungle: $SCRIPTNAME {start|stop|status|restart|phased-restart}" >&2
echo " Add a Puma: $SCRIPTNAME add /path/to/app user /path/to/app/config/puma.rb /path/to/app/config/log/puma.log"
echo " config and log are optionals."
echo " Remove a Puma: $SCRIPTNAME remove /path/to/app"
Expand Down
11 changes: 11 additions & 0 deletions tools/jungle/init.d/run-puma
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
#!/bin/bash

# on system boot, and root have no rbenv installed,
# after start-stop-daemon switched to current user, we have to init rbenv
if [ -d "$HOME/.rbenv/bin" ]; then
PATH="$HOME/.rbenv/bin:$HOME/.rbenv/shims:$PATH"
eval "$(rbenv init -)"
elif [ -d "/usr/local/rbenv/bin" ]; then
PATH="/usr/local/rbenv/bin:/usr/local/rbenv/shims:$PATH"
eval "$(rbenv init -)"
fi

app=$1; config=$2; log=$3;
cd $app && exec bundle exec puma -C $config 2>&1 >> $log

0 comments on commit 7dfd7fa

Please sign in to comment.