Skip to content

Commit

Permalink
Add readiness and liveness probe scripts.
Browse files Browse the repository at this point in the history
  • Loading branch information
GrahamDumpleton committed Mar 16, 2017
1 parent 66db496 commit 620e910
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 0 deletions.
1 change: 1 addition & 0 deletions .s2i/environment
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ S2I_SCRIPTS_PATH=/usr/libexec/s2i
S2I_BASH_ENV=/opt/app-root/etc/scl_enable
DISABLE_COLLECTSTATIC=1
DISABLE_MIGRATE=1
PYTHONUNBUFFERED=1
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ dj_database_url
mod_wsgi
Pillow
psycopg2
requests
30 changes: 30 additions & 0 deletions scripts/alive
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/bin/bash

set -eo pipefail

HERE=`dirname $0`

ready() {
echo " -----> Starting liveness script."

# Check web server.

echo " -----> Checking server is working."

python $HERE/check-server.py

# Check database.

echo " -----> Checking database is working."

if [ x"$DATABASE_URL" != x"" ]; then
(cat - | python manage.py shell --plain) << !
import runpy
_ = runpy.run_path('$HERE/check-database.py')
!
fi

echo " -----> Stopping liveness script."
}

ready 2>&1 | tee -a /proc/1/fd/1
37 changes: 37 additions & 0 deletions scripts/check-database.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from __future__ import print_function

import sys
import time

import django

django.setup()

from django.db import connection

remaining = 600.0
delay = 2.0

success = False

while remaining > 0.0:
try:
print('Check whether database is ready...')
cursor = connection.cursor()
with cursor:
cursor.execute('SELECT 1')
cursor.fetchall()

success = True

break

except Exception:
pass

time.sleep(delay)

if not success:
print('Failed to connect to database')

sys.exit(1)
7 changes: 7 additions & 0 deletions scripts/check-server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import os
import requests

hostname = os.environ['HOSTNAME']

response = requests.get('http://%s:8080/' % hostname)
response.raise_for_status()
30 changes: 30 additions & 0 deletions scripts/ready
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/bin/bash

set -eo pipefail

HERE=`dirname $0`

ready() {
echo " -----> Starting readiness script."

# Check web server.

echo " -----> Checking server is working."

python $HERE/check-server.py

# Check database.

echo " -----> Checking database is working."

if [ x"$DATABASE_URL" != x"" ]; then
(cat - | python manage.py shell --plain) << !
import runpy
_ = runpy.run_path('$HERE/check-database.py')
!
fi

echo " -----> Stopping readiness script."
}

ready 2>&1 | tee -a /proc/1/fd/1

0 comments on commit 620e910

Please sign in to comment.