Skip to content

Commit

Permalink
Merge pull request #1771 from kobotoolbox/health-check-issue-202
Browse files Browse the repository at this point in the history
Health check issue 202
  • Loading branch information
jnm authored May 15, 2018
2 parents 379b153 + 02dd326 commit 71330fa
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 25 deletions.
63 changes: 38 additions & 25 deletions kobo/apps/service_health/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,34 @@

from kpi.models import Asset


def get_response(url_):

message = "OK"
failure = False
content = None

try:
response_ = requests.get(url_, timeout=45)
response_.raise_for_status()
content = response_.content
except Exception as e:
response_ = None
message = repr(e)
failure = True
else:
# Response can be something else than 200. We need to validate this.
# For example: if domain name doesn't match, nginx returns a 204 status code.
status_code = response_.status_code
if status_code != 200:
response_ = None
content = None
failure = True
message = "Response status code is {}".format(status_code)

return failure, message, content


def service_health(request):
''' Return a HTTP 200 if some very basic runtime tests of the application
pass. Otherwise, return HTTP 500 '''
Expand All @@ -32,52 +60,37 @@ def service_health(request):
postgres_time = time.time() - t0

t0 = time.time()
try:
enketo_response = requests.get(settings.ENKETO_SERVER, timeout=45)
enketo_response.raise_for_status()
except Exception as e:
enketo_response = None
enketo_message = repr(e)
any_failure = True
else:
enketo_message = 'OK'
failure, enketo_message, enketo_content = get_response(settings.ENKETO_INTERNAL_URL)
any_failure = True if failure else any_failure
enketo_time = time.time() - t0

t0 = time.time()
try:
kobocat_response = requests.get(
settings.KOBOCAT_URL + '/service_health/', timeout=45)
kobocat_response.raise_for_status()
except Exception as e:
kobocat_response = None
kobocat_message = repr(e)
any_failure = True
else:
kobocat_message = 'OK'
failure, kobocat_message, kobocat_content = get_response(settings.KOBOCAT_INTERNAL_URL + '/service_health/')
any_failure = True if failure else any_failure
kobocat_time = time.time() - t0

output = (
u'{}\r\n\r\n'
u'Mongo: {} in {:.3} seconds\r\n'
u'Postgres: {} in {:.3} seconds\r\n'
u'Enketo: {} in {:.3} seconds\r\n'
u'KoBoCAT: {} in {:.3} seconds\r\n'
u'Enketo [{}]: {} in {:.3} seconds\r\n'
u'KoBoCAT [{}]: {} in {:.3} seconds\r\n'
).format(
'FAIL' if any_failure else 'OK',
mongo_message, mongo_time,
postgres_message, postgres_time,
enketo_message, enketo_time,
kobocat_message, kobocat_time
settings.ENKETO_INTERNAL_URL, enketo_message, enketo_time,
settings.KOBOCAT_INTERNAL_URL, kobocat_message, kobocat_time
)

if kobocat_response:
if kobocat_content:
output += (
u'\r\n'
u'----BEGIN KOBOCAT RESPONSE----\r\n'
u'{}\r\n'
u'---- END KOBOCAT RESPONSE ----\r\n'
).format(
kobocat_response.content
kobocat_content
)

return HttpResponse(
Expand Down
2 changes: 2 additions & 0 deletions kobo/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,8 @@ def get_native_language_name(lang_code):
ENKETO_SERVER = os.environ.get('ENKETO_URL') or os.environ.get('ENKETO_SERVER', 'https://enketo.org')
ENKETO_SERVER= ENKETO_SERVER + '/' if not ENKETO_SERVER.endswith('/') else ENKETO_SERVER
ENKETO_VERSION= os.environ.get('ENKETO_VERSION', 'Legacy').lower()
ENKETO_INTERNAL_URL = os.environ.get('ENKETO_INTERNAL_URL', ENKETO_SERVER)

assert ENKETO_VERSION in ['legacy', 'express']
ENKETO_PREVIEW_URI = 'webform/preview' if ENKETO_VERSION == 'legacy' else 'preview'
# The number of hours to keep a kobo survey preview (generated for enketo)
Expand Down

0 comments on commit 71330fa

Please sign in to comment.