-
-
Save justsee/5026661 to your computer and use it in GitHub Desktop.
Chef + runit + Unicorn + rbenv
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash -e | |
export RBENV_ROOT=/usr/local/rbenv | |
export PATH=/usr/local/rbenv/shims:/usr/local/rbenv/bin:/usr/local/bin:"$PATH" | |
APP="<%= @options[:app].application.name %>" | |
APP_PATH="<%= @options[:app].path %>" | |
RAILS_ENV="<%= @options[:rails_env] %>" | |
UNICORN_CONFIG="/etc/unicorn/${APP}.rb" | |
CUR_PID_FILE="${APP_PATH}/shared/pids/unicorn.pid" | |
OLD_PID_FILE="${PID_FILE}.oldbin" | |
function is_unicorn_alive { | |
set +e | |
if [ -n $1 ] && kill -0 $1 >/dev/null 2>&1; then | |
echo "yes" | |
fi | |
set -e | |
} | |
if [ -e $OLD_PID_FILE ]; then | |
OLD_PID=$(cat $OLD_PID_FILE) | |
echo "Waiting for existing master to exit. PID: ($OLD_PID)." | |
while [ -n "$(is_unicorn_alive $OLD_PID)" ]; do | |
/bin/echo -n '.' | |
sleep 2 | |
done | |
fi | |
if [ -e $CUR_PID_FILE ]; then | |
CUR_PID=$(cat $CUR_PID_FILE) | |
if [ -n "$(is_unicorn_alive $CUR_PID)" ]; then | |
echo "Unicorn master already running. PID: ($CUR_PID)." | |
RUNNING=true | |
fi | |
fi | |
if [ ! $RUNNING ]; then | |
echo "Starting unicorn." | |
$APP_PATH/current/bin/unicorn -E $RAILS_ENV -c $UNICORN_CONFIG -D | |
sleep 3 | |
CUR_PID=$(cat $CUR_PID_FILE) | |
fi | |
function restart { | |
echo "Initialize new master with USR2." | |
kill -USR2 $CUR_PID | |
# Make runit restart to pick up new unicorn pid | |
sleep 2 | |
echo "Restarting service to capture new pid." | |
exit | |
} | |
function graceful_shutdown { | |
echo "Initializing graceful shutdown." | |
kill -QUIT $CUR_PID | |
} | |
trap restart HUP QUIT | |
trap graceful_shutdown INT TERM | |
for sig in USR1 USR2 | |
do | |
trap 'kill -'$sig' $(cat $CUR_PID_FILE)' $sig | |
done | |
echo "Waiting for current master to die. PID: ($CUR_PID)" | |
while [ -n "$(is_unicorn_alive $CUR_PID)" ]; do | |
/bin/echo -n '.' | |
sleep 2 | |
done | |
echo "Unicorn process ($CUR_PID) has quit." |
Can I ask how sig is set on line 62? Is it set somewhere external to this script?
@MarkBennett I think that's if it receives a USR1/2 signal.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is a runit run template file to manage Unicorn within a Chef environment. It assumes:
--binstubs
are generated for an application on deployment (so we can call the unicorn binstub directly to start Unicorn)Calling a binstub directly guarantees that rbenv will execute the correct Ruby version (assuming a per-application version is specified within a
.ruby-version
file within the application repository), and that the correct Gems will be loaded. (Note: it is no longer necessary to cd into the application directory or modify Ruby shebangs to useruby-local-exec
, as rbenv has rolled the functionality into the ruby shim)