Skip to content

Commit 3793b22

Browse files
committed
Add SIMPLE_HEALTH_CHECK_ERROR_CODE
1 parent cd8b9c9 commit 3793b22

File tree

7 files changed

+38
-13
lines changed

7 files changed

+38
-13
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ urlpatterns = [
5656
```python
5757
SIMPLE_HEALTH_CHECKS = {
5858
'simple_health_check.checks.migrations.Migrations': [
59-
dict(alias='db1'),
59+
dict(alias='default'),
6060
dict(alias='db2'),
6161
],
6262
'simple_health_check.checks.db.Databases': None,

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
setup(
99
name='django-simple-health-check',
10-
version='0.3.1',
10+
version='0.4.0',
1111
description='Simple Django health check',
1212
long_description=open('README.md').read(),
1313
long_description_content_type='text/markdown',

simple_health_check/apps.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from django.apps import AppConfig
22
from django.conf import settings
3-
from django.core.signals import setting_changed
43
from django.utils.module_loading import import_string
54

65

@@ -10,7 +9,7 @@ class SimpleHealthCheckConfig(AppConfig):
109
checks = {}
1110

1211
@classmethod
13-
def register_checks(cls, **kwargs):
12+
def register_checks(cls):
1413
cls.checks = {}
1514
SIMPLE_HEALTH_CHECKS = getattr(settings, 'SIMPLE_HEALTH_CHECKS', None)
1615
if SIMPLE_HEALTH_CHECKS is None:
@@ -44,6 +43,3 @@ def check_all(cls):
4443

4544
def ready(self):
4645
self.register_checks()
47-
48-
49-
setting_changed.connect(SimpleHealthCheckConfig.register_checks)

simple_health_check/settings.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from django.conf import settings
2+
from django.core.signals import setting_changed
3+
4+
from .apps import SimpleHealthCheckConfig
5+
6+
7+
ERROR_CODE = getattr(settings, 'SIMPLE_HEALTH_CHECK_ERROR_CODE', 503) or 503
8+
9+
10+
def reload_settings(setting, value, **kwargs):
11+
if setting == 'SIMPLE_HEALTH_CHECKS':
12+
SimpleHealthCheckConfig.register_checks()
13+
elif setting == 'SIMPLE_HEALTH_CHECK_ERROR_CODE':
14+
global ERROR_CODE
15+
ERROR_CODE = value
16+
17+
18+
setting_changed.connect(reload_settings)

simple_health_check/tests.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@ def test_readiness(self):
1212

1313
@override_settings(SIMPLE_HEALTH_CHECKS={'simple_health_check.checks.dummy.DummyFalse': None})
1414
def test_no_readiness(self):
15+
response = self.client.get('/readiness/')
16+
self.assertContains(response, b'down', status_code=503)
17+
18+
@override_settings(
19+
SIMPLE_HEALTH_CHECK_ERROR_CODE=500,
20+
SIMPLE_HEALTH_CHECKS={'simple_health_check.checks.dummy.DummyFalse': None},
21+
)
22+
def test_other_error_code(self):
1523
response = self.client.get('/readiness/')
1624
self.assertContains(response, b'down', status_code=500)
1725

@@ -54,7 +62,7 @@ def test_cache_aliases(self):
5462
)
5563
def test_cache_no_rediness(self):
5664
response = self.client.get('/readiness/')
57-
self.assertContains(response, b'down', status_code=500)
65+
self.assertContains(response, b'down', status_code=503)
5866

5967
@override_settings(
6068
SIMPLE_HEALTH_CHECKS={
@@ -81,7 +89,7 @@ def test_ps_disk_usage(self):
8189
)
8290
def test_ps_disk_usage_no_rediness(self):
8391
response = self.client.get('/readiness/')
84-
self.assertContains(response, b'down', status_code=500)
92+
self.assertContains(response, b'down', status_code=503)
8593

8694
@override_settings(
8795
SIMPLE_HEALTH_CHECKS={
@@ -108,4 +116,4 @@ def test_ps_memory_usage(self):
108116
)
109117
def test_ps_memory_usage_no_rediness(self):
110118
response = self.client.get('/readiness/')
111-
self.assertContains(response, b'down', status_code=500)
119+
self.assertContains(response, b'down', status_code=503)

simple_health_check/views.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import logging
22

33
from django.views import View
4-
from django.http import HttpResponse, JsonResponse, HttpResponseServerError
4+
from django.http import HttpResponse, HttpResponseServerError
55

66
from .apps import SimpleHealthCheckConfig
7+
from . import settings
78

89

910
logger = logging.getLogger(__name__)
@@ -23,6 +24,6 @@ def get(self, request, *args, **kwargs):
2324
try:
2425
SimpleHealthCheckConfig.check_all()
2526
except Exception as e:
26-
logger.error(e)
27-
return HttpResponseServerError('down')
27+
logger.exception(e)
28+
return HttpResponseServerError('down', status=settings.ERROR_CODE)
2829
return HttpResponse('ok')

tests/conf/settings.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,5 @@
127127
# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field
128128

129129
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
130+
131+
SIMPLE_HEALTH_CHECK_ERROR_CODE = 503

0 commit comments

Comments
 (0)