2013-11-13 12:28:39 +08:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
# This is a helper script you can use to supervise unicorn, it allows you to perform a live restart
|
|
|
|
# by sending it a USR2 signal
|
|
|
|
|
|
|
|
LOCAL_WEB="http://127.0.0.1:3000/"
|
|
|
|
|
2018-10-05 11:48:32 +08:00
|
|
|
function log()
|
|
|
|
{
|
|
|
|
echo "($$) $1"
|
|
|
|
}
|
|
|
|
|
2013-11-13 12:28:39 +08:00
|
|
|
function on_exit()
|
|
|
|
{
|
|
|
|
kill $UNICORN_PID
|
2018-10-05 11:48:32 +08:00
|
|
|
log "exiting"
|
2013-11-13 12:28:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function on_reload()
|
|
|
|
{
|
2018-11-21 09:52:36 +08:00
|
|
|
log "Stopping Sidekiq"
|
|
|
|
kill -s TSTP $UNICORN_PID
|
2018-10-05 12:12:54 +08:00
|
|
|
log "Reloading unicorn ($UNICORN_PID)"
|
2013-11-13 12:28:39 +08:00
|
|
|
kill -s USR2 $UNICORN_PID
|
2018-10-04 17:13:17 +08:00
|
|
|
unset NEW_UNICORN_PID
|
|
|
|
|
2018-10-05 12:12:54 +08:00
|
|
|
count=0
|
|
|
|
while [ "$count" -lt 30 -a -z "$NEW_UNICORN_PID" ]; do
|
2018-10-04 17:13:17 +08:00
|
|
|
NEW_UNICORN_PID=`ps -f --ppid $UNICORN_PID | grep 'unicorn master' | grep -v old | grep -v worker | awk '{ print $2 }'`
|
2018-10-05 11:48:32 +08:00
|
|
|
log "Waiting for new unicorn master pid... $NEW_UNICORN_PID"
|
2018-10-05 12:12:54 +08:00
|
|
|
count=$((count+1))
|
2018-10-04 17:13:17 +08:00
|
|
|
sleep 1
|
|
|
|
done
|
|
|
|
|
2018-10-05 12:12:54 +08:00
|
|
|
count=0
|
2018-10-05 12:21:07 +08:00
|
|
|
while [ "$count" -lt 30 -a -z "$(ps -f --ppid $NEW_UNICORN_PID | grep worker | head -1 | awk '{ print $2 }')" ]; do
|
2018-10-05 11:48:32 +08:00
|
|
|
log "Waiting for new unicorn workers under $NEW_UNICORN_PID to start up..."
|
2018-10-05 12:12:54 +08:00
|
|
|
count=$((count+1))
|
2018-10-04 17:13:17 +08:00
|
|
|
sleep 1
|
|
|
|
done
|
|
|
|
|
2013-11-13 12:28:39 +08:00
|
|
|
curl $LOCAL_WEB &> /dev/null
|
2018-10-03 14:26:11 +08:00
|
|
|
kill -s QUIT $UNICORN_PID
|
2018-10-05 11:48:32 +08:00
|
|
|
log "Old pid is: $UNICORN_PID New pid is: $NEW_UNICORN_PID"
|
2013-11-13 12:28:39 +08:00
|
|
|
UNICORN_PID=$NEW_UNICORN_PID
|
|
|
|
}
|
|
|
|
|
2014-08-25 08:48:48 +08:00
|
|
|
function on_reopenlogs()
|
|
|
|
{
|
|
|
|
|
2018-10-05 11:48:32 +08:00
|
|
|
log "Reopening logs"
|
2014-08-25 08:48:48 +08:00
|
|
|
kill -s USR1 $UNICORN_PID
|
|
|
|
}
|
|
|
|
|
2013-11-13 12:28:39 +08:00
|
|
|
export UNICORN_SUPERVISOR_PID=$$
|
|
|
|
|
|
|
|
trap on_exit EXIT
|
2014-05-23 19:01:11 +08:00
|
|
|
trap on_reload USR2 HUP
|
2014-08-25 08:48:48 +08:00
|
|
|
trap on_reopenlogs USR1
|
2013-11-13 12:28:39 +08:00
|
|
|
|
2013-11-14 09:54:41 +08:00
|
|
|
unicorn $@ &
|
2013-11-13 12:28:39 +08:00
|
|
|
UNICORN_PID=$!
|
|
|
|
|
|
|
|
echo "supervisor pid: $UNICORN_SUPERVISOR_PID unicorn pid: $UNICORN_PID"
|
|
|
|
|
2014-02-05 07:48:36 +08:00
|
|
|
while kill -0 $UNICORN_PID
|
2013-11-13 12:28:39 +08:00
|
|
|
do
|
2014-02-05 07:48:36 +08:00
|
|
|
sleep 1
|
2013-11-13 12:28:39 +08:00
|
|
|
done
|