This repository has been archived by the owner on Jan 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10761 from alphagov/unicorn-reloader
Add a script to monitor unicornherder reloads
- Loading branch information
Showing
3 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
#!/bin/bash | ||
|
||
# This script is a wrapper around the upstart reload command intended for a | ||
# an application that is run via unicornherder. | ||
# | ||
# This script supervises the termination and creation of the processes | ||
# unicornherder manages. This is so that we can know when a deployment of | ||
# an application has flipped from the previous version to the new version, | ||
# which can be used to determine when it is safe to test a new deployment | ||
# of an app. | ||
|
||
if [ "$#" -ne "1" ]; then | ||
echo "Usage: sudo govuk_unicorn_reload <APP_NAME>" | ||
exit 1 | ||
fi | ||
|
||
set -eu | ||
|
||
status () { | ||
echo "---> ${@}" | ||
} | ||
|
||
error () { | ||
echo "ERROR: ${@}" >&2 | ||
exit 1 | ||
} | ||
|
||
pid_running () { | ||
ps -p ${@} &> /dev/null | ||
} | ||
|
||
read_pid () { | ||
cat /var/run/${@}/app.pid | ||
} | ||
|
||
app=$1 | ||
|
||
status "Checking ${app} is running" | ||
|
||
if [ -e "/var/run/${app}/app.pid" ]; then | ||
old_pid=$(read_pid $app) | ||
else | ||
error "No pid file at /var/run/${app}/app.pid" | ||
fi | ||
|
||
pid_running $old_pid || error "Process ${old_pid} doesn't appear to be running" | ||
|
||
status "${app} is running with process ${old_pid}. Reloading..." | ||
initctl reload $app | ||
|
||
waiting_for=0 | ||
new_pid="" | ||
|
||
while true; do | ||
sleep 10 | ||
waiting_for=$(($waiting_for+10)) | ||
current_pid=$(read_pid $app) || error "Couldn't establish the current pid" | ||
|
||
if [ -z $new_pid ] && [ $old_pid -ne $current_pid ]; then | ||
new_pid=$current_pid | ||
status "New process ${new_pid} started" | ||
fi | ||
|
||
if ! $(pid_running $old_pid); then | ||
status "Process ${old_pid} has exited" | ||
break | ||
elif [ $waiting_for -gt 180 ]; then | ||
error "Process ${old_pid} is still running after 3 minutes, something has gone wrong" | ||
else | ||
status "Waiting for processes to swap over" | ||
fi | ||
done | ||
|
||
if $(pid_running $new_pid); then | ||
status "Successfully reloaded ${app}" | ||
else | ||
error "The new process for ${app} doesn't appear to be running" | ||
fi |
This file contains 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