Skip to content
This repository has been archived by the owner on Jan 13, 2022. It is now read-only.

Commit

Permalink
Add healthz endpoint for health checks
Browse files Browse the repository at this point in the history
  • Loading branch information
pmac committed Apr 26, 2016
1 parent d4844d7 commit dc816e8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
17 changes: 13 additions & 4 deletions news/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
from time import time

from django.conf import settings
from django.http import HttpResponse
from django.shortcuts import render
from django.utils.encoding import force_unicode
from django.views.decorators.cache import cache_control, never_cache
from django.views.decorators.cache import cache_page, never_cache
from django.views.decorators.csrf import csrf_exempt
from django.views.decorators.http import require_POST, require_safe

Expand Down Expand Up @@ -508,11 +509,10 @@ def custom_update_phonebook(request, token):

# Get data about current newsletters
@require_safe
@cache_control(max_age=300)
@cache_page(300)
def newsletters(request):
# Get the newsletters as a dictionary of dictionaries that are
# easily jsonified

result = {}
for newsletter in Newsletter.objects.all().values():
newsletter['languages'] = newsletter['languages'].split(",")
Expand All @@ -526,6 +526,15 @@ def newsletters(request):
})


@require_safe
@cache_page(10)
def healthz(request):
"""for use with healthchecks. wrapped with `cache_page` to test redis cache."""
# here to make sure DB is working
assert Newsletter.objects.exists(), 'no newsletters exist'
return HttpResponse('OK')


@never_cache
def lookup_user(request):
"""Lookup a user in Exact Target given email or token (not both).
Expand Down Expand Up @@ -610,7 +619,7 @@ def lookup_user(request):


@require_safe
@cache_control(max_age=300)
@cache_page(300)
def list_newsletters(request):
"""
Public web page listing currently active newsletters.
Expand Down
5 changes: 4 additions & 1 deletion urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
from django.contrib import admin
from django.views.generic import RedirectView

from news.views import healthz


home_redirect = '/admin/' if settings.ADMIN_ONLY_MODE else 'https://www.mozilla.org/'

urlpatterns = [
url(r'^$', RedirectView.as_view(url=home_redirect))
url(r'^$', RedirectView.as_view(url=home_redirect)),
url('^healthz/$', healthz, name='healthz'),
]

if not settings.ADMIN_ONLY_MODE:
Expand Down

0 comments on commit dc816e8

Please sign in to comment.