| 1 |
################## |
| 2 |
Write healthchecks |
| 3 |
################## |
| 4 |
|
| 5 |
In the previous :doc:`testing </testing>` topic, you made sure the views and |
| 6 |
middlewares work as expected... within a test environment. |
| 7 |
|
| 8 |
One common issue when deploying in production is that the reverse-proxy's |
| 9 |
configuration does not fit. You cannot check that within test environment. |
| 10 |
|
| 11 |
**Healthchecks are made to diagnose issues in live (production) environments**. |
| 12 |
|
| 13 |
|
| 14 |
************************ |
| 15 |
Introducing healthchecks |
| 16 |
************************ |
| 17 |
|
| 18 |
Healthchecks (sometimes called "smoke tests" or "diagnosis") are assertions you |
| 19 |
run on a live (typically production) service, as opposed to fake/mock service |
| 20 |
used during tests (unit, integration, functional). |
| 21 |
|
| 22 |
See `hospital`_ and `django-doctor`_ projects about writing healthchecks for |
| 23 |
Python and Django. |
| 24 |
|
| 25 |
|
| 26 |
******************** |
| 27 |
Typical healthchecks |
| 28 |
******************** |
| 29 |
|
| 30 |
Here is a typical healthcheck setup for download views with reverse-proxy |
| 31 |
optimizations. |
| 32 |
|
| 33 |
When you run this healthcheck suite, you get a good overview if a problem |
| 34 |
occurs: you can compare expected results and learn which part (Django, |
| 35 |
reverse-proxy or remote storage) is guilty. |
| 36 |
|
| 37 |
.. note:: |
| 38 |
|
| 39 |
In the examples below, we use "localhost" and ports "80" (reverse-proxy) or |
| 40 |
"8000" (Django). Adapt them to your configuration. |
| 41 |
|
| 42 |
Check storage |
| 43 |
============= |
| 44 |
|
| 45 |
Put a dummy file on the storage Django uses. |
| 46 |
|
| 47 |
The write a healthcheck that asserts you can read the dummy file from storage. |
| 48 |
|
| 49 |
**On success, you know remote storage is ok.** |
| 50 |
|
| 51 |
Issues may involve permissions or communications (remote storage). |
| 52 |
|
| 53 |
.. note:: |
| 54 |
|
| 55 |
This healthcheck may be outside Django. |
| 56 |
|
| 57 |
Check Django VS storage |
| 58 |
======================= |
| 59 |
|
| 60 |
Implement a download view dedicated to healthchecks. It is typically a public |
| 61 |
(but not referenced) view that streams a dummy file from real storage. |
| 62 |
Let's say you register it as ``/healthcheck-utils/download/`` URL. |
| 63 |
|
| 64 |
Write a healthcheck that asserts ``GET |
| 65 |
http://localhost:8000/healtcheck-utils/download/`` (notice the `8000` port: |
| 66 |
local Django server) returns the expected reverse-proxy response (X-Accel, |
| 67 |
X-Sendfile...). |
| 68 |
|
| 69 |
**On success, you know there is no configuration issue on the Django side.** |
| 70 |
|
| 71 |
Check reverse proxy VS storage |
| 72 |
============================== |
| 73 |
|
| 74 |
Write a location in your reverse-proxy's configuration that proxy-pass to a |
| 75 |
dummy file on storage. |
| 76 |
|
| 77 |
Write a healthcheck that asserts this location returns the expected dummy file. |
| 78 |
|
| 79 |
**On success, you know the reverse proxy can serve files from storage.** |
| 80 |
|
| 81 |
Check them all together |
| 82 |
======================= |
| 83 |
|
| 84 |
We just checked all parts separately, so let's make sure they can work |
| 85 |
together. |
| 86 |
Configure the reverse-proxy so that `/healthcheck-utils/download/` is proxied |
| 87 |
to Django. Then write a healthcheck that asserts ``GET |
| 88 |
http://localhost:80/healthcheck-utils/download`` (notice the `80` port: |
| 89 |
reverse-proxy server) returns the expected dummy file. |
| 90 |
|
| 91 |
**On success, you know everything is ok.** |
| 92 |
|
| 93 |
On failure, there is an issue in the X-Accel/X-Sendfile configuration. |
| 94 |
|
| 95 |
.. note:: |
| 96 |
|
| 97 |
This last healthcheck should be the first one to run, i.e. if it passes, |
| 98 |
others should pass too. The others are useful when this one fails. |
| 99 |
|
| 100 |
|
| 101 |
.. rubric:: Notes & references |
| 102 |
|
| 103 |
.. target-notes:: |
| 104 |
|
| 105 |
.. _`hospital`: https://pypi.python.org/pypi/hospital |
| 106 |
.. _`django-doctor`: https://pypi.python.org/pypi/django-doctor |